This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 485
/
Check.spec.tsx
98 lines (80 loc) · 2.4 KB
/
Check.spec.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import React from 'react';
import { fireEvent, wait } from 'react-testing-library';
import * as Yup from 'yup';
import { Check } from '../../lib';
import render from '../../lib/RenderTest';
describe('Form', () => {
it('should display label', () => {
const { getByText } = render(<Check name="check" label="Check" />);
expect(!!getByText('Check')).toBe(true);
});
it('should display error', async () => {
const schema = Yup.object().shape({
check: Yup.boolean().oneOf([true], 'Check is required'),
});
const { getByText, getByTestId } = render(
<Check name="check" label="Check" />,
{ schema },
);
fireEvent.submit(getByTestId('form'));
await wait(() => expect(!!getByText('Check is required')).toBe(true));
});
it('should return true if default value is true', () => {
const submitMock = jest.fn();
const { getByTestId } = render(<Check name="check" label="Check" />, {
initialData: { check: true },
onSubmit: submitMock,
});
fireEvent.submit(getByTestId('form'));
expect(submitMock).toHaveBeenCalledWith(
{
check: true,
},
{
resetForm: expect.any(Function),
},
);
});
it('should return true if selected', () => {
const submitMock = jest.fn();
const { getByTestId, getByLabelText } = render(
<Check name="check" label="Check" />,
{ onSubmit: submitMock },
);
fireEvent.click(getByLabelText('Check'));
fireEvent.submit(getByTestId('form'));
expect(submitMock).toHaveBeenCalledWith(
{
check: true,
},
{
resetForm: expect.any(Function),
},
);
});
it('should return false unless selected', () => {
const submitMock = jest.fn();
const { getByTestId } = render(<Check name="check" label="Check" />, {
onSubmit: submitMock,
});
fireEvent.submit(getByTestId('form'));
expect(submitMock).toHaveBeenCalledWith(
{
check: false,
},
{
resetForm: expect.any(Function),
},
);
});
it('should reset state after submit', () => {
const { getByTestId, getByLabelText } = render(
<Check name="check" label="Check" />,
{
onSubmit: (_: any, { resetForm }: { resetForm: any }) => resetForm(),
},
);
fireEvent.submit(getByTestId('form'));
expect((getByLabelText('check') as HTMLInputElement).checked).toBe(false);
});
});