-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathCombobox.test.tsx
380 lines (303 loc) · 11.5 KB
/
Combobox.test.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import { render as renderRtl, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import type * as React from 'react';
import { act } from 'react';
import type { ComboboxProps } from './Combobox';
import { Combobox } from '.';
const PLACES = [
{
name: 'Leikanger',
value: 'leikanger',
description: 'Vestland',
},
{
name: 'Oslo',
value: 'oslo',
description: 'Oslo',
},
{
name: 'Brønnøysund',
value: 'bronnoysund',
description: 'Nordland',
},
];
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const Comp = (args: Partial<ComboboxProps>) => {
return (
<>
<Combobox {...args}>
<Combobox.Empty>Fant ingen treff</Combobox.Empty>
{PLACES.map((option, index) => (
<Combobox.Option
key={index}
value={option.value}
displayValue={option.name}
>
{option.name}
</Combobox.Option>
))}
</Combobox>
</>
);
};
const render = async (props: Partial<ComboboxProps> = {}) => {
/* Flush microtasks */
await act(async () => {});
const user = userEvent.setup();
return {
user,
...renderRtl(<Comp {...props} />),
};
};
describe('Combobox', () => {
afterEach(() => {
vi.restoreAllMocks();
});
it('should render combobox', async () => {
await render();
const combobox = screen.getByRole('combobox');
expect(combobox).toBeInTheDocument();
});
it('should render children when we click on the combobox', async () => {
const { user } = await render();
const combobox = screen.getByRole('combobox');
await act(async () => await user.click(combobox));
expect(screen.getByText('Leikanger')).toBeInTheDocument();
});
it('should close the dropdown when we click outside', async () => {
const { user } = await render();
const combobox = screen.getByRole('combobox');
await act(async () => await user.click(combobox));
expect(screen.getByText('Leikanger')).toBeInTheDocument();
await act(async () => await user.click(document.body));
expect(screen.queryByText('Leikanger')).not.toBeInTheDocument();
});
it('should close when we click Escape', async () => {
const { user } = await render({ label: 'closeOnEscape' });
const combobox = screen.getByRole('combobox');
await act(async () => await user.click(combobox));
expect(screen.getByText('Leikanger')).toBeInTheDocument();
await act(async () => await user.type(combobox, '{Escape}'));
expect(screen.queryByText('Leikanger')).not.toBeInTheDocument();
});
it('should select when we click Enter', async () => {
const onValueChange = vi.fn();
const { user } = await render({ onValueChange });
const combobox = screen.getByRole('combobox');
expect(screen.queryByText('Leikanger')).not.toBeInTheDocument();
await act(async () => await user.click(combobox));
expect(screen.getByText('Leikanger')).toBeInTheDocument();
await act(async () => await user.type(combobox, '{Enter}'));
await vi.waitFor(() => {
expect(onValueChange).toHaveBeenCalledWith(['leikanger']);
});
});
it('should set call `onValueChange` on the Combobox when we click and option', async () => {
const onValueChange = vi.fn();
const { user } = await render({ onValueChange });
const combobox = screen.getByRole('combobox');
await act(async () => await user.click(combobox));
await act(async () => await user.click(screen.getByText('Leikanger')));
await vi.waitFor(() => {
expect(onValueChange).toHaveBeenCalledWith(['leikanger']);
});
});
it('should call `onValueChange` with multiple values when we click multiple options', async () => {
const onValueChange = vi.fn();
const { user } = await render({ onValueChange, multiple: true });
const combobox = screen.getByRole('combobox');
await act(async () => await user.click(combobox));
await act(async () => await user.click(screen.getByText('Leikanger')));
await vi.waitFor(() => {
expect(onValueChange).toHaveBeenCalledWith(['leikanger']);
});
await act(async () => await user.click(screen.getByText('Oslo')));
await vi.waitFor(() => {
expect(onValueChange).toHaveBeenCalledWith(['leikanger', 'oslo']);
});
});
it('should send `onChange` event when value changes', async () => {
const onChange = vi.fn();
const { user } = await render({ onChange });
const combobox = screen.getByRole('combobox');
await act(async () => await user.click(combobox));
await act(async () => await user.click(screen.getByText('Oslo')));
await vi.waitFor(() => {
/* we expect a change event with (e) => e.target.value === 'Oslo' */
expect(onChange).toHaveBeenCalledWith(
expect.objectContaining({
target: expect.objectContaining({
value: 'Oslo',
}),
}),
);
});
});
it('should show a chip of a selected option in multiple mode', async () => {
await render({ multiple: true, value: ['leikanger'] });
expect(screen.getByText('Leikanger')).toBeInTheDocument();
});
it('should remove a chip when we click on it', async () => {
const { user } = await render({
multiple: true,
initialValue: ['oslo'],
});
await act(async () => await user.click(screen.getByText('Oslo')));
await vi.waitFor(async () => {
await user.click(document.body);
});
await vi.waitFor(() => {
expect(screen.queryByText('Oslo')).not.toBeInTheDocument();
});
});
it('should remove all values when we click on the clear button', async () => {
const onValueChange = vi.fn();
const { user } = await render({ multiple: true, onValueChange });
const combobox = screen.getByRole('combobox');
await act(async () => await user.click(combobox));
await act(async () => await user.click(screen.getByText('Leikanger')));
await vi.waitFor(() => {
expect(onValueChange).toHaveBeenCalledWith(['leikanger']);
});
await act(async () => await user.click(screen.getByText('Oslo')));
await vi.waitFor(() => {
expect(onValueChange).toHaveBeenCalledWith(['leikanger', 'oslo']);
});
await act(async () => await user.click(document.body));
expect(screen.getByText('Leikanger')).toBeInTheDocument();
expect(screen.getByText('Oslo')).toBeInTheDocument();
// get clear button by its classname .clearButton
const buttons = screen.getAllByRole('button');
const clearButton = buttons.find((button) =>
button.className.includes('ds-combobox__clear-button'),
);
if (!clearButton) {
throw new Error('Could not find clear button');
}
await act(async () => await user.click(clearButton));
await act(async () => await user.click(document.body));
await vi.waitFor(() => {
expect(screen.queryByText('Leikanger')).not.toBeInTheDocument();
expect(screen.queryByText('Oslo')).not.toBeInTheDocument();
expect(onValueChange).toHaveBeenCalledWith([]);
});
});
it('should show "Fant ingen treff", when input does not match any values', async () => {
const { user } = await render();
const combobox = screen.getByRole('combobox');
await act(async () => await user.click(combobox));
await act(async () => await user.type(combobox, 'test'));
expect(screen.getByText('Fant ingen treff')).toBeInTheDocument();
});
it('should work in a form if we pass a name', async () => {
const user = userEvent.setup();
const formSubmitPromise = new Promise<FormData>((resolve) => {
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
resolve(new FormData(event.currentTarget));
};
renderRtl(
<form onSubmit={handleSubmit}>
<Combobox name='test'>
<Combobox.Empty>Fant ingen treff</Combobox.Empty>
{PLACES.map((option, index) => (
<Combobox.Option
key={index}
value={option.value}
displayValue={option.name}
>
{option.name}
</Combobox.Option>
))}
</Combobox>
<button type='submit'>Submit</button>
</form>,
);
});
const combobox = screen.getByRole('combobox');
await user.click(combobox);
await user.click(screen.getByText('Leikanger'));
await wait(1000);
const submitButton = screen.getByRole('button', { name: 'Submit' });
await user.click(submitButton);
const formData = await formSubmitPromise;
expect(formData.get('test')).toBe('leikanger');
});
it('should work in a form if we pass a name, and we click multiple', async () => {
const user = userEvent.setup();
const formSubmitPromise = new Promise<FormData>((resolve) => {
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
resolve(new FormData(event.currentTarget));
};
renderRtl(
<form onSubmit={handleSubmit}>
<Combobox name='test' multiple={true}>
<Combobox.Empty>Fant ingen treff</Combobox.Empty>
{PLACES.map((option, index) => (
<Combobox.Option
key={index}
value={option.value}
displayValue={option.name}
>
{option.name}
</Combobox.Option>
))}
</Combobox>
<button data-testid='submit' type='submit'>
Submit
</button>
</form>,
);
});
const combobox = screen.getByRole('combobox');
await user.click(combobox);
await user.click(screen.getByText('Leikanger'));
await wait(100);
await user.click(screen.getByText('Oslo'));
await wait(100);
const submitButton = screen.getAllByTestId('submit')[0];
await user.click(submitButton);
const formData = await formSubmitPromise;
expect(formData.getAll('test')).toEqual(['leikanger', 'oslo']);
});
it('should show all options when we are in single mode, and have a value selected', async () => {
const { user } = await render();
const combobox = screen.getByRole('combobox');
await act(async () => await user.click(combobox));
await act(async () => await user.click(screen.getByText('Leikanger')));
expect(screen.getByText('Leikanger')).toBeInTheDocument();
expect(screen.getByText('Oslo')).toBeInTheDocument();
expect(screen.getByText('Brønnøysund')).toBeInTheDocument();
});
it('should only call onValueChange once when we click the same option fast twice', async () => {
const onValueChange = vi.fn();
const { user } = await render({ onValueChange, multiple: true });
const combobox = screen.getByRole('combobox');
await user.click(combobox);
await user.click(screen.getByText('Leikanger'));
await user.click(screen.getByText('Leikanger'));
await vi.waitFor(() => {
expect(onValueChange).toHaveBeenCalledTimes(1);
});
});
it('should add aria-busy="true" when loading', async () => {
await render({ loading: true });
const combobox = screen.getByRole('combobox');
expect(combobox).toHaveAttribute('aria-busy', 'true');
});
it('should have correct label when used as ReactNode', async () => {
await render({
label: (
<>
<strong>
<abbr>CSS</abbr>
</strong>
(Cascading Style Sheets)
</>
),
});
const combobox = screen.getByRole('combobox');
expect(combobox).toHaveAccessibleName('CSS (Cascading Style Sheets)');
});
});