forms.test.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { renderHook, act } from '@testing-library/react-hooks'
  2. import useFormHandler from '../forms'
  3. describe('form hook return values', () => {
  4. const { result } = renderHook(() => useFormHandler({ var: 'val' }, values => { return {} }))
  5. it('returns correct initial states.', () => {
  6. expect(result.current.values.var).toBe('val')
  7. expect(result.current.errors).toEqual({})
  8. expect(result.current.isSubmitting).toBe(false)
  9. })
  10. it('returns input element properties for valid input names.', () => {
  11. expect(result.current.inputProps('var')).toMatchObject({
  12. name: 'var', id: 'var', onBlur: expect.anything(), onChange: expect.anything()
  13. })
  14. })
  15. it('throws error for invalid input names.', () => {
  16. expect(() => result.current.inputProps('doh!')).toThrow()
  17. })
  18. it('returns form submission properties.', () => {
  19. expect(result.current.submitProps()).toMatchObject({
  20. onSubmit: expect.anything()
  21. })
  22. })
  23. it('sets the isSubmitting flag.', () => {
  24. act(() => result.current.handleSubmit({ preventDefault: () => { } }))
  25. expect(result.current.isSubmitting).toBe(true)
  26. })
  27. })