This repository has been archived on 2024-11-11. You can view files and clone it, but cannot push or open issues or pull requests.
react-component-form/test/index.test.tsx

42 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-10-03 19:07:09 +02:00
import React from 'react'
import { render, cleanup, fireEvent } from '@testing-library/react'
2021-06-17 18:48:19 +02:00
import Form, { HandleForm } from '../src'
2020-10-03 19:07:09 +02:00
afterEach(cleanup)
2021-06-17 18:48:19 +02:00
describe('<Form />', () => {
2020-10-03 19:07:09 +02:00
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()
})
})