Skip to content

Commit

Permalink
feat #133 - Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Nov 3, 2020
1 parent 76632d4 commit 0af3be5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
24 changes: 22 additions & 2 deletions src/components/Form/Form.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { act } from 'react-dom/test-utils'
import FieldContext from './FieldContext'
import FormSubmitButton from './FormSubmitButton'
import React from 'react'
import { UseFormMethods } from 'react-hook-form'
import { Form, FormProps } from './index'
import { mount, shallow, ShallowWrapper } from 'enzyme'
import React, { createRef } from 'react'

jest.mock('react-hook-form', () => ({
...jest.requireActual('react-hook-form'),
...(jest.requireActual('react-hook-form') as {}),
useForm: () => ({
getValues: mockGetValues,
handleSubmit: jest.fn(),
reset: mockReset
}),
Expand All @@ -26,6 +29,7 @@ let wrapper: ShallowWrapper<FormProps<MockForm>>

const mockInitialValues = { foo: 'bar' }
const mockOnSubmit = jest.fn()
const mockGetValues = jest.fn()
const mockReset = jest.fn()

beforeEach(() => {
Expand Down Expand Up @@ -78,4 +82,20 @@ describe('Form', () => {

expect(mockReset).toHaveBeenCalledWith(mockInitialValues)
})

it('exposes form methods when a ref is passed', () => {
const formRef = createRef<UseFormMethods>()

mount(
<Form formRef={formRef} onSubmit={mockOnSubmit}>
<FormSubmitButton>Submit</FormSubmitButton>
</Form>
)

act(() => {
formRef.current?.getValues()
})

expect(mockGetValues).toHaveBeenCalled()
})
})
40 changes: 31 additions & 9 deletions src/components/Form/FormSubmitButton/FormSubmitButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as reactHookForm from 'react-hook-form'
import { act } from 'react-dom/test-utils'
import { Button } from 'components/Button'
import FieldContext from '../FieldContext'
import React from 'react'
Expand All @@ -17,11 +18,15 @@ const getMockFormContext = (isDirty = true) =>
handleSubmit: (onSubmit: any) => onSubmit()
} as reactHookForm.UseFormMethods)

const getWrapper = () => (
const getWrapper = (additionalButtonProps = {}) => (
<FieldContext.Provider
value={{ initialValues: {}, loading: true, onSubmit: mockOnSubmit }}
value={{
initialValues: {},
loading: false,
onSubmit: (_: any) => mockOnSubmit as any
}}
>
<FormSubmitButton>Submit</FormSubmitButton>
<FormSubmitButton {...additionalButtonProps}>Submit</FormSubmitButton>
</FieldContext.Provider>
)

Expand All @@ -34,7 +39,7 @@ beforeEach(() => {
})

afterEach(() => {
jest.resetAllMocks()
jest.clearAllMocks()
})

describe('FormButton', () => {
Expand All @@ -43,22 +48,33 @@ describe('FormButton', () => {
})

it('calls onSubmit when clicked', () => {
wrapper.simulate('click')
wrapper.find(Button).simulate('click')

expect(mockOnSubmit).toHaveBeenCalledTimes(1)
expect(mockOnSubmit).toHaveBeenCalled()
})

it('calls onSubmit when the Enter key is pressed', () => {
act(() => {
dispatchEvent(
new KeyboardEvent('keydown', {
code: 'Enter',
key: 'Enter'
})
)
})

expect(mockOnSubmit).toHaveBeenCalled()
})

it('correctly passes loading from field context', () => {
expect(wrapper.find(Button).props().loading).toBe(true)
expect(wrapper.find(Button).props().loading).toBe(false)
})

it('enables the submit button if form is dirty', () => {
expect(wrapper.find(Button).props().disabled).toBe(false)
})

it('disables the submit button if form is pristine', () => {
jest.clearAllMocks()

jest.spyOn(reactHookForm, 'useFormContext').mockImplementation(() =>
getMockFormContext(false)
)
Expand All @@ -67,4 +83,10 @@ describe('FormButton', () => {

expect(wrapper.find(Button).props().disabled).toBe(true)
})

it('disables the submit button if isDisabled evaluates to true', () => {
wrapper = mount(getWrapper({ isDisabled: () => true }))

expect(wrapper.find(Button).props().disabled).toBe(true)
})
})

0 comments on commit 0af3be5

Please sign in to comment.