-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathRadioGroup.unit.test.jsx
202 lines (172 loc) · 6.4 KB
/
RadioGroup.unit.test.jsx
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { fireEvent, render, screen } from '@testing-library/react';
import RadioGroup from './RadioGroup';
const _childrenTest = (RadioItem) => {
describe(`<${RadioItem.displayName} />`, () => {
it(`should render three <${RadioItem.displayName} />`, () => {
render(
<RadioGroup name="foo">
<RadioItem value="Foo">Foo</RadioItem>
<RadioItem value="Bar">Bar</RadioItem>
<RadioItem value="Baz">Baz</RadioItem>
</RadioGroup>,
);
expect(screen.getAllByRole('radio')).toHaveLength(3);
});
it(`every <${RadioItem.displayName} /> should have the <RadioGroup /> name`, () => {
render(
<RadioGroup name="foo">
<RadioItem value="Foo">Foo</RadioItem>
<RadioItem value="Bar">Bar</RadioItem>
<RadioItem value="Baz">Baz</RadioItem>
</RadioGroup>,
);
const radios = screen.getAllByRole('radio');
radios.map((radio) => expect(radio.getAttribute('name')).toEqual('foo'));
});
if (RadioItem.displayName === 'RadioGroup.Radio') {
it(`should have <${RadioItem.displayName} /> defaultChecked matching <RadioGroup /> defaultValue`, () => {
render(
<RadioGroup name="foo" defaultValue="Bar">
<RadioItem value="Foo">Foo</RadioItem>
<RadioItem value="Bar">Bar</RadioItem>
<RadioItem value="Baz">Baz</RadioItem>
</RadioGroup>,
);
const radios = screen.getByRole('radio', { name: 'Bar' });
expect(radios).toHaveAttribute('checked');
});
}
it(`should call onChange on every <${RadioItem.displayName} />`, () => {
const onChangeMock = jest.fn();
render(
<RadioGroup name="foo" onChange={onChangeMock}>
<RadioItem value="Foo">Foo</RadioItem>
<RadioItem value="Bar">Bar</RadioItem>
<RadioItem value="Baz">Baz</RadioItem>
</RadioGroup>,
);
const radios = screen.getAllByRole('radio');
radios.map((radio) => fireEvent.click(radio));
expect(onChangeMock).toBeCalledTimes(3);
});
});
};
describe('<RadioGroup />', () => {
describe('snapshot', () => {
it('simple with options', () => {
const options = [
{ value: 'Foo', label: 'Foo' },
{ value: 'Bar', label: 'Bar' },
{ value: 'Baz', label: 'Baz' },
];
const { container } = render(<RadioGroup name="foo" options={options} />);
expect(container.firstChild).toMatchSnapshot();
});
it('simple with button options', () => {
const options = [
{ value: 'Foo', label: 'Foo' },
{ value: 'Bar', label: 'Bar' },
{ value: 'Baz', label: 'Baz' },
];
const { container } = render(
<RadioGroup name="foo" options={options} type="button" />,
);
expect(container.firstChild).toMatchSnapshot();
});
it('simple with <RadioGroup.Radio />', () => {
const { container } = render(
<RadioGroup name="groceries">
<RadioGroup.Radio value="Foo">Foo</RadioGroup.Radio>
<RadioGroup.Radio value="Bar">Bar</RadioGroup.Radio>
<RadioGroup.Radio value="Baz">Baz</RadioGroup.Radio>
</RadioGroup>,
);
expect(container.firstChild).toMatchSnapshot();
});
it('simple with <RadioGroup.Button />', () => {
const { container } = render(
<RadioGroup name="groceries">
<RadioGroup.Button value="Foo">Foo</RadioGroup.Button>
<RadioGroup.Button value="Bar">Bar</RadioGroup.Button>
<RadioGroup.Button value="Baz">Baz</RadioGroup.Button>
</RadioGroup>,
);
expect(container.firstChild).toMatchSnapshot();
});
it('simple with <RadioGroup.Button /> inline', () => {
const { container } = render(
<RadioGroup name="groceries" inline>
<RadioGroup.Button value="Foo">Foo</RadioGroup.Button>
<RadioGroup.Button value="Bar">Bar</RadioGroup.Button>
<RadioGroup.Button value="Baz">Baz</RadioGroup.Button>
</RadioGroup>,
);
expect(container.firstChild).toMatchSnapshot();
});
it('with an error', () => {
const { container } = render(
<RadioGroup name="groceries" error="Error text">
<RadioGroup.Radio value="Foo">Foo</RadioGroup.Radio>
</RadioGroup>,
);
expect(container.firstChild).toMatchSnapshot();
});
it('should render the same', () => {
const options = [
{ value: 'Foo', label: 'Foo' },
{ value: 'Bar', label: 'Bar' },
{ value: 'Baz', label: 'Baz' },
];
const { container: snapOptions } = render(
<RadioGroup name="foo" options={options} />,
);
const { container: snapComposable } = render(
<RadioGroup name="foo">
<RadioGroup.Radio value="Foo">Foo</RadioGroup.Radio>
<RadioGroup.Radio value="Bar">Bar</RadioGroup.Radio>
<RadioGroup.Radio value="Baz">Baz</RadioGroup.Radio>
</RadioGroup>,
);
expect(snapOptions).toMatchDiffSnapshot(snapComposable);
});
it('should render the same, with <RadioGroup.Button />', () => {
const options = [
{ value: 'Foo', label: 'Foo' },
{ value: 'Bar', label: 'Bar' },
{ value: 'Baz', label: 'Baz' },
];
const { container: snapOptions } = render(
<RadioGroup name="foo" options={options} type="button" />,
);
const { container: snapComposable } = render(
<RadioGroup name="foo">
<RadioGroup.Button value="Foo">Foo</RadioGroup.Button>
<RadioGroup.Button value="Bar">Bar</RadioGroup.Button>
<RadioGroup.Button value="Baz">Baz</RadioGroup.Button>
</RadioGroup>,
);
expect(snapOptions).toMatchDiffSnapshot(snapComposable);
});
it('with required', () => {
const { container } = render(
<RadioGroup name="groceries" required>
<RadioGroup.Radio value="Foo">Foo</RadioGroup.Radio>
</RadioGroup>,
);
expect(container.firstChild).toMatchSnapshot();
});
it('with inline', () => {
const options = [
{ value: 'Foo', label: 'Foo' },
{ value: 'Bar', label: 'Bar' },
{ value: 'Baz', label: 'Baz' },
];
const { container } = render(
<RadioGroup inline name="foo" options={options} />,
);
expect(container.firstChild).toMatchSnapshot();
});
});
_childrenTest(RadioGroup.Radio);
_childrenTest(RadioGroup.Button);
});