forms.test.tsx 1.2 KB

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