perf: reduce drastically build size
BREAKING CHANGE: This package is now pure ESM
This commit is contained in:
40
src/__test__/index.test.tsx
Normal file
40
src/__test__/index.test.tsx
Normal file
@ -0,0 +1,40 @@
|
||||
import { render, cleanup, fireEvent } from '@testing-library/react'
|
||||
|
||||
import { Form, HandleForm } from '..'
|
||||
|
||||
afterEach(cleanup)
|
||||
|
||||
describe('<Form />', () => {
|
||||
it('should get the formData and formElement onSubmit and onChange', () => {
|
||||
let formData: { [k: string]: any } = {}
|
||||
let formElement: any = null
|
||||
const handleSubmitChange: HandleForm = (data, element) => {
|
||||
formData = data
|
||||
formElement = element
|
||||
}
|
||||
const formComponent = render(
|
||||
<Form onSubmit={handleSubmitChange} onChange={handleSubmitChange}>
|
||||
<input data-testid='input-form' type='text' name='inputName' />
|
||||
<button data-testid='button-submit' type='submit'>
|
||||
Submit
|
||||
</button>
|
||||
</Form>
|
||||
)
|
||||
const inputForm = formComponent.getByTestId(
|
||||
'input-form'
|
||||
) as HTMLInputElement
|
||||
const buttonSubmit = formComponent.getByTestId('button-submit')
|
||||
const text = 'some random text'
|
||||
|
||||
fireEvent.change(inputForm, { target: { value: text } })
|
||||
expect(formData.inputName).toEqual(text)
|
||||
expect(formElement instanceof HTMLFormElement).toBeTruthy()
|
||||
formData = {}
|
||||
formElement = null
|
||||
|
||||
fireEvent.click(buttonSubmit)
|
||||
expect(Object.keys(formData).length).toEqual(1)
|
||||
expect(formData.inputName).toEqual(text)
|
||||
expect(formElement instanceof HTMLFormElement).toBeTruthy()
|
||||
})
|
||||
})
|
3
src/__test__/setup.ts
Normal file
3
src/__test__/setup.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import React from 'react'
|
||||
|
||||
global.React = React
|
@ -1,4 +1,4 @@
|
||||
import React, { useRef } from 'react'
|
||||
import { useRef } from 'react'
|
||||
|
||||
export interface FormDataObject {
|
||||
[key: string]: FormDataEntryValue
|
||||
@ -23,7 +23,7 @@ export const getFormDataObject = (
|
||||
return Object.fromEntries<FormDataEntryValue>(new FormData(formElement))
|
||||
}
|
||||
|
||||
export const Form = (props: FormProps): JSX.Element => {
|
||||
export const Form: React.FC<FormProps> = (props) => {
|
||||
const { onSubmit, onChange, children, ...rest } = props
|
||||
const formRef = useRef<HTMLFormElement>(null)
|
||||
|
||||
|
Reference in New Issue
Block a user