-
-
Notifications
You must be signed in to change notification settings - Fork 489
Expand file tree
/
Copy pathCombobox.test.tsx
More file actions
691 lines (595 loc) · 21.5 KB
/
Combobox.test.tsx
File metadata and controls
691 lines (595 loc) · 21.5 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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/* eslint-disable max-classes-per-file */
import '@testing-library/jest-dom';
import { createEvent, fireEvent, render } from '@testing-library/react';
import KeyCode from '@rc-component/util/lib/KeyCode';
import { resetWarned } from '@rc-component/util/lib/warning';
import React, { act } from 'react';
import type { SelectProps } from '../src';
import Select, { Option } from '../src';
import allowClearTest from './shared/allowClearTest';
import focusTest from './shared/focusTest';
import keyDownTest from './shared/keyDownTest';
import openControlledTest from './shared/openControlledTest';
import throwOptionValue from './shared/throwOptionValue';
import { expectOpen, injectRunAllTimers, keyDown, selectItem, toggleOpen } from './utils/common';
async function delay(timeout = 0) {
await act(async () => {
await new Promise((resolve) => {
setTimeout(resolve, timeout);
});
});
}
describe('Select.Combobox', () => {
injectRunAllTimers(jest);
allowClearTest('combobox', '2333');
throwOptionValue('combobox');
focusTest('combobox');
keyDownTest('combobox');
openControlledTest('combobox');
it('renders correctly', () => {
const { container } = render(
<Select mode="combobox" placeholder="Search">
<Option value="1">1</Option>
<Option value="2">2</Option>
</Select>,
);
expect(container.firstChild).toMatchSnapshot();
});
it('renders controlled correctly', () => {
const { container } = render(
<Select value="" mode="combobox" placeholder="Search">
<Option value="1">1</Option>
<Option value="2">2</Option>
</Select>,
);
expect(container.firstChild).toMatchSnapshot();
});
it('set inputValue based on value', () => {
const { container } = render(
<Select mode="combobox" value="1">
<Option value="1">1</Option>
<Option value="2">2</Option>
</Select>,
);
// expect(wrapper.find('input').props().value).toEqual('1');
expect(container.querySelector('input').value).toEqual('1');
});
it('placeholder', () => {
const { container } = render(
<Select mode="combobox" placeholder="placeholder">
<Option value="1">1</Option>
<Option value="2">2</Option>
</Select>,
);
expect(container.querySelector('input').value).toBe('');
expect(container.querySelector('.rc-select-selection-placeholder')!.textContent).toEqual(
'placeholder',
);
fireEvent.change(container.querySelector('input')!, { target: { value: '1' } });
expect(container.querySelector('.rc-select-selection-placeholder')).toBeFalsy();
expect(container.querySelector('input')!.value).toBe('1');
});
it('fire change event immediately when user inputing', () => {
const handleChange = jest.fn();
const { container } = render(
<Select mode="combobox" onChange={handleChange}>
<Option value="11">11</Option>
<Option value="22">22</Option>
</Select>,
);
fireEvent.change(container.querySelector('input')!, { target: { value: '1' } });
expect(handleChange).toHaveBeenCalledWith('1', {});
fireEvent.change(container.querySelector('input')!, { target: { value: '22' } });
expect(handleChange).toHaveBeenCalledWith(
'22',
expect.objectContaining({
children: '22',
value: '22',
}),
);
});
it('set inputValue when user select a option', () => {
const { container } = render(
<Select mode="combobox">
<Option value="1">1</Option>
<Option value="2">2</Option>
</Select>,
);
toggleOpen(container);
selectItem(container);
expect(container.querySelector('input').value).toEqual('1');
});
it('activeOptionFilter should work', async () => {
const { container } = render(
<Select
mode="combobox"
showSearch
activeOptionFilter={(searchValue, option) => {
return String(option.value).includes(searchValue);
}}
>
<Option value="apple">apple</Option>
<Option value="banana">banana</Option>
<Option value="orange">orange</Option>
</Select>,
);
const input = container.querySelector('input')!;
fireEvent.change(input, { target: { value: 'an' } });
await delay();
expect(
container.querySelector('.rc-select-item-option-active .rc-select-item-option-content'),
).toHaveTextContent('banana');
fireEvent.change(input, { target: { value: 'ora' } });
await delay();
expect(
container.querySelector('.rc-select-item-option-active .rc-select-item-option-content'),
).toHaveTextContent('orange');
});
it('activeOptionFilter with empty function should not activate any option', async () => {
const { container } = render(
<Select mode="combobox" showSearch activeOptionFilter={() => false}>
<Option value="apple">apple</Option>
<Option value="banana">banana</Option>
<Option value="orange">orange</Option>
</Select>,
);
const input = container.querySelector('input')!;
fireEvent.change(input, { target: { value: 'an' } });
await delay();
expect(container.querySelector('.rc-select-item-option-active')).toBeNull();
});
describe('input value', () => {
const createSelect = (props?: Partial<SelectProps>) => (
<Select mode="combobox" {...props}>
<Option value="1">One</Option>
<Option value="2">Two</Option>
</Select>
);
it('displays correct input value for defaultValue', () => {
const { container } = render(
createSelect({
defaultValue: '1',
}),
);
expect(container.querySelector('input')!.value).toBe('1');
});
it('displays correct input value for value', () => {
const { container } = render(
createSelect({
value: '1',
}),
);
expect(container.querySelector('input')!.value).toBe('1');
});
it('displays correct input value when value changes', () => {
const { container, rerender } = render(createSelect());
rerender(createSelect({ value: '1' }));
expect(container.querySelector('input')!.value).toBe('1');
});
});
describe('hidden when filtered options is empty', () => {
// https://github.com/ant-design/ant-design/issues/3958
it('should popup when input with async data', () => {
jest.useFakeTimers();
class AsyncCombobox extends React.Component {
public state = {
data: [],
};
public handleChange = () => {
setTimeout(() => {
this.setState({
data: [
{ key: '1', label: '1' },
{ key: '2', label: '2' },
],
});
}, 500);
};
public render() {
const options = this.state.data.map((item) => (
<Option key={item.key} value={item.key}>
{item.label}
</Option>
));
return (
<Select
mode="combobox"
onChange={this.handleChange}
filterOption={false}
notFoundContent=""
>
{options}
</Select>
);
}
}
const { container } = render(<AsyncCombobox />);
fireEvent.focus(container.querySelector('input')!);
fireEvent.change(container.querySelector('input')!, { target: { value: '0' } });
jest.runAllTimers();
expectOpen(container);
jest.useRealTimers();
});
// https://github.com/ant-design/ant-design/issues/6600
it('should not re-open after select', () => {
jest.useFakeTimers();
class AsyncCombobox extends React.Component {
public state = {
data: [
{ key: '1', label: '1' },
{ key: '2', label: '2' },
],
};
public onSelect = () => {
setTimeout(() => {
this.setState({
data: [
{ key: '3', label: '3' },
{ key: '4', label: '4' },
],
});
}, 500);
};
public render() {
const options = this.state.data.map((item) => (
<Option key={item.key} value={item.key}>
{item.label}
</Option>
));
return (
<Select
mode="combobox"
onSelect={this.onSelect}
filterOption={false}
notFoundContent=""
>
{options}
</Select>
);
}
}
const { container } = render(<AsyncCombobox />);
fireEvent.focus(container.querySelector('input')!);
fireEvent.change(container.querySelector('input')!, { target: { value: '0' } });
expectOpen(container);
selectItem(container);
jest.runAllTimers();
expectOpen(container, false);
jest.useRealTimers();
});
});
describe('backfill', () => {
it('basic', () => {
const handleChange = jest.fn();
const handleSelect = jest.fn();
const { container } = render(
<Select mode="combobox" backfill open onChange={handleChange} onSelect={handleSelect}>
<Option value="One">One</Option>
<Option value="Two">Two</Option>
</Select>,
);
const input = container.querySelector('input')!;
keyDown(input, KeyCode.DOWN);
expect(input.value).toEqual('One');
expect(handleChange).not.toHaveBeenCalled();
expect(handleSelect).not.toHaveBeenCalled();
keyDown(input, KeyCode.ENTER);
expect(input.value).toEqual('One');
expect(handleChange).toHaveBeenCalledWith('One', expect.objectContaining({ value: 'One' }));
expect(handleSelect).toHaveBeenCalledWith('One', expect.objectContaining({ value: 'One' }));
});
it('mouse should not trigger', () => {
const { container } = render(
<Select mode="combobox" backfill open>
<Option value="One">One</Option>
<Option value="Two">Two</Option>
</Select>,
);
fireEvent.mouseMove(container.querySelector('.rc-select-item-option'));
expect(container.querySelector('input')!.value).toBeFalsy();
});
// https://github.com/ant-design/ant-design/issues/25345
it('dynamic options', () => {
const onChange = jest.fn();
const Test = () => {
const [options, setOptions] = React.useState([]);
const onSearch = (value: string) => {
let res = [];
if (!value || value.indexOf('@') >= 0) {
res = [];
} else {
const email = `${value}@gmail.com`;
res = [{ value: email, label: email }];
}
setOptions(res);
};
return (
<Select
backfill
mode="combobox"
onChange={onChange}
onSearch={onSearch}
options={options}
/>
);
};
const { container } = render(<Test />);
const inputEle = container.querySelector('input')!;
fireEvent.change(inputEle, { target: { value: 'light' } });
expectOpen(container);
expect(onChange).toHaveBeenCalledWith('light', expect.anything());
onChange.mockReset();
keyDown(inputEle, KeyCode.DOWN);
expect(inputEle.value).toEqual('light@gmail.com');
expect(onChange).not.toHaveBeenCalled();
keyDown(inputEle, KeyCode.ENTER);
expect(onChange).toHaveBeenCalledWith('light@gmail.com', expect.anything());
});
});
it("should hide clear icon when value is ''", () => {
const { container } = render(
<Select mode="combobox" value="" allowClear>
<Option value="One">One</Option>
<Option value="Two">Two</Option>
</Select>,
);
expect(container.querySelector('.rc-select-clear-icon')).toBeFalsy();
});
it("should show clear icon when inputValue is not ''", () => {
const { container } = render(
<Select mode="combobox" value="One" allowClear>
<Option value="One">One</Option>
<Option value="Two">Two</Option>
</Select>,
);
expect(container.querySelector('.rc-select-clear-icon')).toBeTruthy();
});
it("should hide clear icon when inputValue is ''", () => {
const { container } = render(
<Select mode="combobox" allowClear>
<Option value="One">One</Option>
<Option value="Two">Two</Option>
</Select>,
);
fireEvent.change(container.querySelector('input')!, { target: { value: '1' } });
expect(container.querySelector('.rc-select-clear-icon')).toBeTruthy();
fireEvent.change(container.querySelector('input')!, { target: { value: '' } });
expect(container.querySelector('.rc-select-clear-icon')).toBeFalsy();
});
it('autocomplete - option update when input change', () => {
class App extends React.Component {
public state = {
options: [],
};
public updateOptions = (value) => {
const options = [value, value + value, value + value + value];
this.setState({
options,
});
};
public render() {
return (
<Select mode="combobox" onChange={this.updateOptions}>
{this.state.options.map((opt) => (
<Option key={opt} value={opt}>
{opt}
</Option>
))}
</Select>
);
}
}
const { container } = render(<App />);
fireEvent.change(container.querySelector('input')!, { target: { value: 'a' } });
fireEvent.change(container.querySelector('input')!, { target: { value: 'ab' } });
expect(container.querySelector('input')!.value).toBe('ab');
selectItem(container, 1);
expect(container.querySelector('input')!.value).toBe('abab');
});
it("when value change to '', searchValue will change to '' ", () => {
class App extends React.Component {
public state = {
value: '2',
};
public render() {
return (
<div>
<button onClick={() => this.setState({ value: '' })}>value to 0</button>
<Select
value={this.state.value}
open
mode="combobox"
onChange={(value) => this.setState({ value })}
filterOption={(inputValue, option) => {
if (!inputValue) {
return true;
}
return (option.value as string).includes(inputValue);
}}
>
{['1'].map((i) => (
<Option value={i} key={i}>
{i}
</Option>
))}
</Select>
</div>
);
}
}
const { container } = render(<App />);
expect(container.querySelector('.rc-select-item-option')).toBeFalsy();
fireEvent.click(container.querySelector('button')!);
expect(container.querySelector('.rc-select-item-option')).toBeTruthy();
});
// [Legacy] `optionLabelProp` should not work on `combobox`
// https://github.com/ant-design/ant-design/issues/10367
// origin test: https://github.com/react-component/select/blob/e5fa4959336f6a423b6e30652b9047510bb6f78f/tests/Select.combobox.spec.tsx#L362
it('should autocomplete with correct option value', () => {
resetWarned();
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
render(<Select mode="combobox" optionLabelProp="children" />);
expect(errorSpy).toHaveBeenCalledWith(
'Warning: `combobox` mode not support `optionLabelProp`. Please set `value` on Option directly.',
);
errorSpy.mockRestore();
});
// https://github.com/ant-design/ant-design/issues/16572
it('close when enter press without active option', () => {
jest.useFakeTimers();
const onPopupVisibleChange = jest.fn();
const { container } = render(
<Select mode="combobox" open onPopupVisibleChange={onPopupVisibleChange}>
<Option value="One">One</Option>
<Option value="Two">Two</Option>
</Select>,
);
keyDown(container.querySelector('input')!, KeyCode.ENTER);
jest.runAllTimers();
expect(onPopupVisibleChange).toHaveBeenCalledWith(false);
jest.useRealTimers();
});
it('should reset value by control', () => {
const onChange = jest.fn();
const { container } = render(
<Select mode="combobox" value="" onChange={onChange}>
<Option value="One">One</Option>
<Option value="Two">Two</Option>
</Select>,
);
toggleOpen(container);
selectItem(container);
expect(onChange).toHaveBeenCalled();
expectOpen(container, false);
expect(container.querySelector('input')!.value).toEqual('');
});
it('should keep close after blur', async () => {
const { container } = render(
<Select mode="combobox" notFoundContent={null}>
<Option value="One">One</Option>
</Select>,
);
toggleOpen(container);
expectOpen(container);
// Click again should not close popup
for (let i = 0; i < 10; i += 1) {
fireEvent.mouseDown(container.querySelector('input')!);
expectOpen(container);
}
fireEvent.blur(container.querySelector('input')!);
await delay(100);
expectOpen(container, false);
});
it('expect null value display empty string', () => {
const { container } = render(<Select mode="combobox" value={null} />);
expect(container.querySelector('input')!.value).toBe('');
});
describe('maxLength', () => {
it('should support', () => {
const { container } = render(<Select maxLength={6} />);
expect(container.querySelector('input')).not.toHaveAttribute('maxLength');
});
it('normal should not support', () => {
const { container } = render(<Select mode="combobox" maxLength={6} />);
expect(container.querySelector('input')!.getAttribute('maxLength')).toBe('6');
});
});
it('typewriting should not trigger onChange multiple times', () => {
const onChange = jest.fn();
const { container } = render(<Select mode="combobox" onChange={onChange} />);
fireEvent.change(container.querySelector('input')!, { target: { value: '' } });
fireEvent.change(container.querySelector('input')!, { target: { value: 'a' } });
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenLastCalledWith('a', expect.anything());
fireEvent.change(container.querySelector('input')!, { target: { value: '啊' } });
expect(onChange).toHaveBeenCalledTimes(2);
expect(onChange).toHaveBeenLastCalledWith('啊', expect.anything());
fireEvent.change(container.querySelector('input')!, { target: { value: '啊' } });
expect(onChange).toHaveBeenCalledTimes(2);
});
it('default filterOption is false', () => {
const { container } = render(
<Select
mode="combobox"
open
searchValue="not exist"
options={[
{
label: 'Light',
value: 'light',
},
{
label: 'Bamboo',
value: 'bamboo',
},
]}
/>,
);
expect(container.querySelectorAll('.rc-select-item-option')).toHaveLength(2);
});
// https://github.com/ant-design/ant-design/issues/34975
it('should not contain selected className in combobox mode', () => {
const onChange = jest.fn();
const { container } = render(
<Select mode="combobox" onChange={onChange}>
<Option value="One">One</Option>
<Option value="Two">Two</Option>
</Select>,
);
toggleOpen(container);
selectItem(container);
expect(container.querySelector('.rc-select-item-option-selected')).toBeFalsy();
});
// https://github.com/ant-design/ant-design/issues/38844
it('not show dropdown when options changed', () => {
jest.useFakeTimers();
const { container, rerender } = render(<Select mode="combobox" options={[]} />);
fireEvent.mouseDown(container.querySelector('input'));
act(() => {
jest.runAllTimers();
});
fireEvent.blur(container.querySelector('input'));
act(() => {
jest.runAllTimers();
});
rerender(<Select mode="combobox" options={[{ value: 'shouldHide' }]} />);
expect(document.body.querySelector('.rc-select-dropdown-hidden')).toBeTruthy();
jest.useRealTimers();
});
// https://github.com/ant-design/ant-design/issues/43936
it('combobox mode not show 0 value', () => {
const { container } = render(<Select mode="combobox" value={0} />);
expect(container.querySelector('input')!.value).toBe('0');
});
// https://github.com/ant-design/ant-design/issues/48281
describe('combobox mode onMouseDown event default behavior', () => {
it('should prevent default behavior when mode is combobox', () => {
const preventDefault = jest.fn();
const { container } = render(
<Select mode="combobox" value="1">
<Option value="1">1</Option>
<Option value="2">2</Option>
</Select>,
);
const selectorEle = container.querySelector('.rc-select-selector');
const mouseDownEvent = createEvent.mouseDown(selectorEle);
mouseDownEvent.preventDefault = preventDefault;
fireEvent(selectorEle, mouseDownEvent);
expect(preventDefault).toHaveBeenCalled();
});
it('should not prevent default behavior when mode is combobox and it is disabled', () => {
const preventDefault = jest.fn();
const { container } = render(
<Select mode="combobox" value="1" disabled>
<Option value="1">1</Option>
<Option value="2">2</Option>
</Select>,
);
const selectorEle = container.querySelector('.rc-select-selector');
const mouseDownEvent = createEvent.mouseDown(selectorEle);
mouseDownEvent.preventDefault = preventDefault;
fireEvent(selectorEle, mouseDownEvent);
expect(preventDefault).not.toHaveBeenCalled();
});
});
});