123456789101112131415161718192021222324252627282930313233343536373839 |
- import { renderHook, act } from '@testing-library/react-hooks'
- import useFormHandler from '../forms'
- describe('form hook return values', () => {
- const { result } = renderHook(() => useFormHandler({ var: 'val' }, values => { return {} }))
- it('returns correct initial states.', () => {
- expect(result.current.values.var).toBe('val')
- expect(result.current.errors).toEqual({})
- expect(result.current.isSubmitting).toBe(false)
- })
- it('returns input element properties for valid input names.', () => {
- expect(result.current.inputProps('var')).toMatchObject({
- name: 'var', id: 'var', onBlur: expect.anything(), onChange: expect.anything()
- })
- })
- it('throws error for invalid input names.', () => {
- expect(() => result.current.inputProps('doh!')).toThrow()
- })
- it('returns form submission properties.', () => {
- expect(result.current.submitProps()).toMatchObject({
- onSubmit: expect.anything()
- })
- })
- it('sets the isSubmitting flag.', () => {
- act(() => result.current.handleSubmit({ preventDefault: () => { } }))
- expect(result.current.isSubmitting).toBe(true)
- })
- })
|