-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat #76 - Refactor Form.Button to be Form.SubmitButton
Closes #76
- Loading branch information
github-actions
committed
Sep 8, 2020
1 parent
2e5d713
commit 2f474d7
Showing
9 changed files
with
114 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
70 changes: 70 additions & 0 deletions
70
src/components/Form/FormSubmitButton/FormSubmitButton.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import * as reactHookForm from 'react-hook-form' | ||
import Button from '../../Button' | ||
import FieldContext from '../FieldContext' | ||
import React from 'react' | ||
import FormSubmitButton, { FormButtonProps } from './index' | ||
import { mount, ReactWrapper } from 'enzyme' | ||
|
||
let wrapper: ReactWrapper<FormButtonProps> | ||
|
||
const mockOnSubmit = jest.fn() | ||
|
||
const getMockFormContext = (isDirty = true) => | ||
({ | ||
formState: { | ||
isDirty | ||
}, | ||
handleSubmit: (onSubmit: any) => onSubmit() | ||
} as reactHookForm.UseFormMethods) | ||
|
||
const getWrapper = () => ( | ||
<FieldContext.Provider | ||
value={{ initialValues: {}, loading: true, onSubmit: mockOnSubmit }} | ||
> | ||
<FormSubmitButton>Submit</FormSubmitButton> | ||
</FieldContext.Provider> | ||
) | ||
|
||
beforeEach(() => { | ||
jest.spyOn(reactHookForm, 'useFormContext').mockImplementation(() => | ||
getMockFormContext() | ||
) | ||
|
||
wrapper = mount(getWrapper()) | ||
}) | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
describe('FormButton', () => { | ||
it('renders', () => { | ||
expect(wrapper).toHaveLength(1) | ||
}) | ||
|
||
it('calls onSubmit when clicked', () => { | ||
wrapper.simulate('click') | ||
|
||
expect(mockOnSubmit).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('correctly passes loading from field context', () => { | ||
expect(wrapper.find(Button).props().loading).toBe(true) | ||
}) | ||
|
||
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) | ||
) | ||
|
||
wrapper = mount(getWrapper()) | ||
|
||
expect(wrapper.find(Button).props().disabled).toBe(true) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import FieldContext from '../FieldContext' | ||
import { useFormContext } from 'react-hook-form' | ||
import Button, { ButtonProps } from '../../Button' | ||
import React, { FC, useContext } from 'react' | ||
|
||
export type FormButtonProps = Omit<ButtonProps, 'loading' | 'onClick'> | ||
|
||
const FormSubmitButton: FC<FormButtonProps> = (props: FormButtonProps) => { | ||
const { handleSubmit, formState } = useFormContext() | ||
const { loading, onSubmit } = useContext(FieldContext) | ||
const { isDirty } = formState | ||
|
||
return ( | ||
<Button | ||
disabled={!isDirty} | ||
loading={loading} | ||
onClick={handleSubmit(onSubmit)} | ||
{...props} | ||
/> | ||
) | ||
} | ||
|
||
export default FormSubmitButton |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters