import { renderHook } from '@testing-library/react-hooks'
import { mount } from 'enzyme'

import useFormHandler from '../forms'

describe('form hook return values', () => {

  const Component = () => useFormHandler({ var: 'val' }, values => { return {} })
  const { result } = renderHook(Component)

  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.', () => {
    const wrapper = mount(<form {...result.current.submitProps()} />)
    const form = wrapper.find('form')
    form.simulate('submit')
    expect(result.current.isSubmitting).toBe(true)
  })



})