-
-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathCheckbox.spec.tsx
More file actions
148 lines (120 loc) · 4.52 KB
/
Copy pathCheckbox.spec.tsx
File metadata and controls
148 lines (120 loc) · 4.52 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import React from 'react';
import { renderWithTheme } from '../../test/utils';
import { Checkbox } from './Checkbox';
describe('<Checkbox />', () => {
describe('label', () => {
it('renders', () => {
const labelText = 'Swag';
const { getByLabelText } = renderWithTheme(
<Checkbox label={labelText} />
);
expect(getByLabelText(labelText)).toBeInTheDocument();
});
});
describe('prop: onChange', () => {
it('should call onChange when uncontrolled', () => {
const handleChange = jest.fn(event => event.target.checked);
const { getByRole } = renderWithTheme(
<Checkbox onChange={handleChange} />
);
getByRole('checkbox').click();
expect(handleChange).toHaveBeenCalledTimes(1);
// event.target.check is true
expect(handleChange.mock.results[0].value).toBe(true);
});
it('should call onChange when controlled', () => {
const checked = true;
const handleChange = jest.fn(event => event.target.checked);
const { getByRole } = renderWithTheme(
<Checkbox onChange={handleChange} checked={checked} />
);
getByRole('checkbox').click();
expect(handleChange).toHaveBeenCalledTimes(1);
expect(handleChange.mock.results[0].value).toBe(!checked);
});
});
describe('prop: disabled', () => {
it('should disable checkbox', () => {
const handleChange = jest.fn();
const { getByRole } = renderWithTheme(
<Checkbox disabled onChange={handleChange} />
);
const checkbox = getByRole('checkbox');
expect(checkbox).toHaveAttribute('disabled');
checkbox.click();
expect(handleChange).not.toHaveBeenCalled();
});
it('should be overridden by props', () => {
const { getByRole, rerender } = renderWithTheme(<Checkbox disabled />);
rerender(<Checkbox disabled={false} />);
const checkbox = getByRole('checkbox');
expect(checkbox).not.toHaveAttribute('disabled');
});
});
describe('prop: indeterminate', () => {
it('renders indeterminate state', () => {
const { getByRole } = renderWithTheme(<Checkbox indeterminate />);
const checkbox = getByRole('checkbox');
// don't set native 'indeterminate' attribute because
// different browsers treat it differently
// instead we're setting 'data-indeterminate' attribute
expect(checkbox).toHaveAttribute('data-indeterminate');
expect(checkbox).not.toHaveAttribute('indeterminate');
expect(getByRole('presentation').firstChild).toHaveAttribute(
'data-testid',
'indeterminateIcon'
);
});
it('replaces checked icon', () => {
const { getByRole, rerender } = renderWithTheme(<Checkbox checked />);
expect(getByRole('checkbox')).toHaveAttribute(
'data-indeterminate',
'false'
);
rerender(<Checkbox checked indeterminate />);
expect(getByRole('checkbox')).toHaveAttribute(
'data-indeterminate',
'true'
);
expect(getByRole('presentation').firstChild).toHaveAttribute(
'data-testid',
'indeterminateIcon'
);
});
});
describe('controlled', () => {
it('should check the checkbox', () => {
const { getByRole, rerender } = renderWithTheme(
<Checkbox checked={false} />
);
rerender(<Checkbox checked />);
const checkbox = getByRole('checkbox') as HTMLInputElement;
expect(checkbox.checked).toBe(true);
expect(getByRole('checkbox')).toHaveAttribute('checked');
expect(getByRole('presentation').firstChild).toHaveAttribute(
'data-testid',
'checkmarkIcon'
);
});
it('should uncheck the checkbox', () => {
const { getByRole, rerender } = renderWithTheme(<Checkbox checked />);
rerender(<Checkbox checked={false} />);
const checkbox = getByRole('checkbox') as HTMLInputElement;
expect(checkbox.checked).toBe(false);
expect(getByRole('checkbox')).not.toHaveAttribute('checked');
expect(getByRole('presentation').firstChild).toBeNull();
// check if proper icon was rendered
});
});
describe('uncontrolled', () => {
it('can change checked state uncontrolled starting from defaultChecked', () => {
const { getByRole } = renderWithTheme(<Checkbox defaultChecked />);
const checkbox = getByRole('checkbox') as HTMLInputElement;
expect(checkbox.checked).toBe(true);
checkbox.click();
expect(checkbox.checked).toBe(false);
checkbox.click();
expect(checkbox.checked).toBe(true);
});
});
});