Skip to content

Commit

Permalink
feat #65 - Update failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Aug 26, 2020
1 parent 76626f8 commit 395c879
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 37 deletions.
40 changes: 18 additions & 22 deletions src/__snapshots__/storybook.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -61,35 +61,31 @@ exports[`Storyshots Button Google 1`] = `

exports[`Storyshots Button Loading 1`] = `
<button
className="ant-btn ant-btn-default ant-btn-loading"
className="ant-btn ant-btn-default"
disabled={false}
onClick={[Function]}
type="button"
>
<span
className="ant-btn-loading-icon"
<div
className="ant-spin ant-spin-spinning"
>
<span
aria-label="loading"
className="anticon anticon-loading"
role="img"
className="ant-spin-dot ant-spin-dot-spin"
>
<svg
aria-hidden="true"
className="anticon-spin"
data-icon="loading"
fill="currentColor"
focusable="false"
height="1em"
viewBox="0 0 1024 1024"
width="1em"
>
<path
d="M988 548c-19.9 0-36-16.1-36-36 0-59.4-11.6-117-34.6-171.3a440.45 440.45 0 00-94.3-139.9 437.71 437.71 0 00-139.9-94.3C629 83.6 571.4 72 512 72c-19.9 0-36-16.1-36-36s16.1-36 36-36c69.1 0 136.2 13.5 199.3 40.3C772.3 66 827 103 874 150c47 47 83.9 101.8 109.7 162.7 26.7 63.1 40.2 130.2 40.2 199.3.1 19.9-16 36-35.9 36z"
/>
</svg>
<i
className="ant-spin-dot-item"
/>
<i
className="ant-spin-dot-item"
/>
<i
className="ant-spin-dot-item"
/>
<i
className="ant-spin-dot-item"
/>
</span>
</span>
</div>
<span>
Loading
</span>
Expand Down Expand Up @@ -147,7 +143,7 @@ exports[`Storyshots Form Default 1`] = `
onKeyDown={[Function]}
placeholder=""
type="text"
value=""
value="First Name"
/>
</div>
</div>
Expand Down
28 changes: 17 additions & 11 deletions src/components/Form/Form.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@ import { mount, ReactWrapper, shallow, ShallowWrapper } from 'enzyme'
jest.mock('react-hook-form', () => ({
...jest.requireActual('react-hook-form'),
useForm: () => ({
handleSubmit: jest.fn(),
reset: mockReset
handleSubmit: jest.fn()
})
}))

let wrapper: ReactWrapper<FormProps> | ShallowWrapper<FormProps>
interface MockForm {
foo: string
}

let wrapper:
| ReactWrapper<FormProps<MockForm>>
| ShallowWrapper<FormProps<MockForm>>

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

beforeEach(() => {
wrapper = shallow(
<Form onSubmit={mockOnSubmit}>
<Form initialValues={mockInitialValues} onSubmit={mockOnSubmit}>
<FormButton />
</Form>
)
Expand All @@ -37,21 +42,22 @@ describe('Form', () => {
it('passes the correct props to FieldContext provider', () => {
expect(wrapper.find(FieldContext.Provider).props().value).toMatchObject(
{
initialValues: mockInitialValues,
loading: false,
onSubmit: mockOnSubmit
}
)
})

it('sets initial values into the form', () => {
const mockInitialValues = { foo: 'bar' }
wrapper = mount(
<Form initialValues={mockInitialValues} onSubmit={mockOnSubmit}>
it('correctly defaults initial values if there are none', () => {
wrapper = shallow(
<Form onSubmit={mockOnSubmit}>
<FormButton />
</Form>
)

expect(mockReset).toHaveBeenCalledTimes(1)
expect(mockReset).toHaveBeenCalledWith(mockInitialValues)
expect(
wrapper.find(FieldContext.Provider).props().value
).toMatchObject({ initialValues: {} })
})
})
18 changes: 15 additions & 3 deletions src/components/Form/FormInput/FormInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jest.mock('react-hook-form', () => ({
Controller: () => <div />,
useFormContext: () => ({
control: jest.fn(),
handleSubmit: (onSubmit: Function) => onSubmit()
errors: () => ({ foo: true })
})
}))

Expand All @@ -20,7 +20,11 @@ const mockOnSubmit = jest.fn()
beforeEach(() => {
wrapper = mount(
<FieldContext.Provider
value={{ loading: true, onSubmit: mockOnSubmit }}
value={{
initialValues: { foo: 'bar' },
loading: true,
onSubmit: mockOnSubmit
}}
>
<FormInput name='foo' />
</FieldContext.Provider>
Expand All @@ -36,10 +40,18 @@ describe('FormInput', () => {
expect(wrapper).toHaveLength(1)
})

it('correctly passes a default value from initial values if it exists', () => {
expect(wrapper.find(Controller).props().defaultValue).toEqual('bar')
})

it('correctly passes validation rules if required', () => {
wrapper = mount(
<FieldContext.Provider
value={{ loading: true, onSubmit: mockOnSubmit }}
value={{
initialValues: {},
loading: true,
onSubmit: mockOnSubmit
}}
>
<FormInput name='foo' required />
</FieldContext.Provider>
Expand Down
4 changes: 3 additions & 1 deletion src/components/Form/FormInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ interface BaseFieldProps {
rules?: ValidationRules
}

export interface FormInputProps extends BaseFieldProps, InputProps {}
export interface FormInputProps
extends BaseFieldProps,
Omit<InputProps, 'onChange' | 'value'> {}

const FormInput: FC<FormInputProps> = ({
label,
Expand Down

0 comments on commit 395c879

Please sign in to comment.