-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
Autocomplete.test.js
3476 lines (3036 loc) · 113 KB
/
Autocomplete.test.js
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as React from 'react';
import PropTypes from 'prop-types';
import { expect } from 'chai';
import {
act,
createRenderer,
fireEvent,
screen,
strictModeDoubleLoggingSuppressed,
reactMajor,
} from '@mui/internal-test-utils';
import { spy } from 'sinon';
import userEvent from '@testing-library/user-event';
import Box from '@mui/system/Box';
import { ThemeProvider, createTheme } from '@mui/material/styles';
import TextField from '@mui/material/TextField';
import Chip, { chipClasses } from '@mui/material/Chip';
import Autocomplete, {
autocompleteClasses as classes,
createFilterOptions,
} from '@mui/material/Autocomplete';
import { paperClasses } from '@mui/material/Paper';
import { iconButtonClasses } from '@mui/material/IconButton';
import InputAdornment from '@mui/material/InputAdornment';
import Tooltip from '@mui/material/Tooltip';
import describeConformance from '../../test/describeConformance';
function checkHighlightIs(listbox, expected) {
const focused = listbox.querySelector(`.${classes.focused}`);
if (expected) {
if (focused) {
expect(focused).to.have.text(expected);
} else {
// No options selected
expect(null).to.equal(expected);
}
} else {
expect(focused).to.equal(null);
}
}
describe('<Autocomplete />', () => {
const { render } = createRenderer();
describeConformance(
<Autocomplete
options={['one', 'two']}
defaultValue="one"
open
renderInput={(params) => <TextField {...params} />}
/>,
() => ({
classes,
inheritComponent: 'div',
render,
muiName: 'MuiAutocomplete',
testVariantProps: { variant: 'foo' },
testDeepOverrides: { slotName: 'endAdornment', slotClassName: classes.endAdornment },
testStateOverrides: { prop: 'fullWidth', value: true, styleKey: 'fullWidth' },
refInstanceof: window.HTMLDivElement,
testComponentPropWith: 'div',
slots: {
listbox: { expectedClassName: classes.listbox },
paper: { expectedClassName: classes.paper },
popper: { expectedClassName: classes.popper, testWithElement: null },
},
skip: ['componentProp', 'componentsProp'],
}),
);
describeConformance(
<Autocomplete
options={['one', 'two']}
defaultValue="one"
open
renderInput={(params) => <TextField {...params} />}
/>,
() => ({
classes,
render,
muiName: 'MuiAutocomplete',
slots: {
clearIndicator: { expectedClassName: classes.clearIndicator },
popupIndicator: { expectedClassName: classes.popupIndicator },
},
only: ['slotPropsProp'],
}),
);
describeConformance(
<Autocomplete
options={['one', 'two']}
defaultValue={['one']}
multiple
open
renderInput={(params) => <TextField {...params} />}
/>,
() => ({
classes,
render,
muiName: 'MuiAutocomplete',
slots: {
chip: {},
},
only: ['slotPropsProp'],
}),
);
it('should be customizable in the theme', () => {
const theme = createTheme({
components: {
MuiAutocomplete: {
styleOverrides: {
paper: {
mixBlendMode: 'darken',
},
},
},
},
});
render(
<ThemeProvider theme={theme}>
<Autocomplete options={[]} open renderInput={(params) => <TextField {...params} />} />
</ThemeProvider>,
);
expect(document.querySelector(`.${classes.paper}`)).to.toHaveComputedStyle({
mixBlendMode: 'darken',
});
});
it('should not throw error when accessing ownerState in styleOverrides', () => {
const theme = createTheme({
components: {
MuiAutocomplete: {
styleOverrides: {
root: ({ ownerState }) => {
return {
outlineColor: ownerState.size === 'small' ? 'magenta' : 'crimson',
};
},
},
},
},
});
expect(() => {
render(
<ThemeProvider theme={theme}>
<Autocomplete
open
options={['one', 'two', 'three']}
renderInput={(params) => <TextField {...params} />}
/>
</ThemeProvider>,
);
}).not.to.throw();
});
describe('combobox', () => {
it('should clear the input when blur', () => {
const { getByRole } = render(
<Autocomplete options={[]} renderInput={(params) => <TextField {...params} />} />,
);
const input = getByRole('combobox');
act(() => {
input.focus();
fireEvent.change(document.activeElement, { target: { value: 'a' } });
});
expect(input.value).to.equal('a');
act(() => {
document.activeElement.blur();
});
expect(input.value).to.equal('');
});
it('should apply the icon classes', () => {
const { container } = render(
<Autocomplete
value="one"
options={['one', 'two', 'three']}
renderInput={(params) => <TextField {...params} />}
/>,
);
expect(container.querySelector(`.${classes.root}`)).to.have.class(classes.hasClearIcon);
expect(container.querySelector(`.${classes.root}`)).to.have.class(classes.hasPopupIcon);
});
});
describe('prop: loading', () => {
it('should show a loading message when open', () => {
render(
<Autocomplete
options={[]}
freeSolo
loading
renderInput={(params) => <TextField {...params} autoFocus />}
/>,
);
fireEvent.keyDown(screen.getByRole('combobox'), { key: 'ArrowDown' });
expect(document.querySelector(`.${classes.paper}`).textContent).to.equal('Loading…');
});
it('should show supplied options to the "options" prop even when loading', () => {
render(
<Autocomplete
options={['one', 'two']}
loading
renderInput={(params) => <TextField {...params} autoFocus />}
/>,
);
fireEvent.keyDown(screen.getByRole('combobox'), { key: 'ArrowDown' });
expect(document.querySelector(`.${classes.paper}`).textContent).not.to.equal('Loading…');
const listbox = screen.getByRole('listbox');
const htmlOptions = listbox.querySelectorAll('li');
expect(htmlOptions[0].innerHTML).to.equal('one');
});
});
describe('prop: autoHighlight', () => {
it('should set the focus on the first item', () => {
const options = ['one', 'two'];
const { getByRole } = render(
<Autocomplete
freeSolo
autoHighlight
open
options={options}
renderInput={(params) => <TextField {...params} autoFocus />}
/>,
);
checkHighlightIs(getByRole('listbox'), 'one');
fireEvent.change(document.activeElement, { target: { value: 'oo' } });
fireEvent.change(document.activeElement, { target: { value: 'o' } });
checkHighlightIs(getByRole('listbox'), 'one');
});
it('should keep the highlight on the first item', () => {
const options = ['one', 'two'];
const { getByRole } = render(
<Autocomplete
value="one"
autoHighlight
open
options={options}
renderInput={(params) => <TextField {...params} autoFocus />}
/>,
);
checkHighlightIs(getByRole('listbox'), 'one');
fireEvent.change(document.activeElement, { target: { value: 'two' } });
checkHighlightIs(getByRole('listbox'), 'two');
});
it('should set the focus on the first item when possible', () => {
const options = ['one', 'two'];
const { getByRole, setProps } = render(
<Autocomplete
open
options={[]}
autoHighlight
loading
renderInput={(params) => <TextField {...params} autoFocus />}
/>,
);
const textbox = getByRole('combobox');
expect(textbox).not.to.have.attribute('aria-activedescendant');
setProps({ options, loading: false });
expect(textbox).to.have.attribute(
'aria-activedescendant',
screen.getAllByRole('option')[0].getAttribute('id'),
);
});
it('should set the highlight on selected item when dropdown is expanded', () => {
const { getByRole, setProps } = render(
<Autocomplete
value="one"
open
options={['one', 'two', 'three']}
renderInput={(params) => <TextField {...params} autoFocus />}
/>,
);
checkHighlightIs(getByRole('listbox'), 'one');
setProps({ value: 'two' });
checkHighlightIs(getByRole('listbox'), 'two');
});
// https://github.com/mui/material-ui/issues/34998
it('should scroll the listbox to the top when keyboard highlight wraps around after the last item is highlighted', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
const { getByRole } = render(
<Autocomplete
open
options={['one', 'two', 'three', 'four', 'five']}
renderInput={(params) => <TextField {...params} />}
ListboxProps={{ style: { padding: 0, maxHeight: '100px' } }}
PopperComponent={(props) => {
const { disablePortal, anchorEl, open, ...other } = props;
return <Box {...other} />;
}}
/>,
);
const textbox = getByRole('combobox');
act(() => {
textbox.focus();
});
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
checkHighlightIs(getByRole('listbox'), 'one');
expect(getByRole('listbox')).to.have.property('scrollTop', 0);
});
it('should keep the current highlight if possible', () => {
const { getByRole } = render(
<Autocomplete
multiple
defaultValue={['one']}
open
options={['one', 'two', 'three']}
disableCloseOnSelect
renderInput={(params) => <TextField {...params} autoFocus />}
/>,
);
const textbox = getByRole('combobox');
checkHighlightIs(getByRole('listbox'), 'one');
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
checkHighlightIs(getByRole('listbox'), 'two');
fireEvent.keyDown(textbox, { key: 'Enter' });
checkHighlightIs(getByRole('listbox'), 'two');
});
it('should work with filterSelectedOptions too', () => {
const options = ['Foo', 'Bar', 'Baz'];
const { getByRole } = render(
<Autocomplete
multiple
filterSelectedOptions
autoHighlight
value={options.slice(0, 1)}
options={options}
renderInput={(params) => <TextField {...params} autoFocus />}
/>,
);
const textbox = getByRole('combobox');
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
checkHighlightIs(getByRole('listbox'), 'Bar');
fireEvent.change(textbox, { target: { value: 'a' } });
checkHighlightIs(getByRole('listbox'), 'Bar');
fireEvent.change(textbox, { target: { value: 'aa' } });
fireEvent.change(textbox, { target: { value: 'a' } });
checkHighlightIs(getByRole('listbox'), 'Bar');
});
});
describe('highlight synchronisation', () => {
it('should not update the highlight when multiple open and value change', () => {
const { setProps, getByRole } = render(
<Autocomplete
value={['two']}
multiple
open
options={['one', 'two', 'three']}
renderInput={(params) => <TextField {...params} autoFocus />}
/>,
);
checkHighlightIs(getByRole('listbox'), 'two');
setProps({
value: [],
});
checkHighlightIs(getByRole('listbox'), 'two');
});
});
describe('prop: limitTags', () => {
it('show all items on focus', () => {
const { container, getAllByRole, getByRole } = render(
<Autocomplete
multiple
limitTags={2}
options={['one', 'two', 'three']}
defaultValue={['one', 'two', 'three']}
renderInput={(params) => <TextField {...params} variant="standard" />}
/>,
);
expect(container.textContent).to.equal('onetwo+1');
// include hidden clear button because JSDOM thinks it's visible
expect(getAllByRole('button', { hidden: true })).to.have.lengthOf(4);
act(() => {
getByRole('combobox').focus();
});
expect(container.textContent).to.equal('onetwothree');
// Depending on the subset of components used in this test run the computed `visibility` changes in JSDOM.
if (!/jsdom/.test(window.navigator.userAgent)) {
expect(getAllByRole('button', { hidden: false })).to.have.lengthOf(5);
}
});
it('show 0 item on close when set 0 to limitTags', () => {
const { container, getAllByRole, getByRole } = render(
<Autocomplete
multiple
limitTags={0}
options={['one', 'two', 'three']}
defaultValue={['one', 'two', 'three']}
renderInput={(params) => <TextField {...params} variant="standard" />}
/>,
);
expect(container.textContent).to.equal('+3');
// include hidden clear button because JSDOM thinks it's visible
expect(getAllByRole('button', { hidden: true })).to.have.lengthOf(2);
act(() => {
getByRole('combobox').focus();
});
expect(container.textContent).to.equal('onetwothree');
// Depending on the subset of components used in this test run the computed `visibility` changes in JSDOM.
if (!/jsdom/.test(window.navigator.userAgent)) {
expect(getAllByRole('button', { hidden: false })).to.have.lengthOf(5);
}
});
});
describe('prop: filterSelectedOptions', () => {
it('when the last item is selected, highlights the new last item', () => {
const { getByRole } = render(
<Autocomplete
filterSelectedOptions
openOnFocus
options={['one', 'two', 'three']}
renderInput={(params) => <TextField {...params} autoFocus />}
/>,
);
const textbox = getByRole('combobox');
fireEvent.keyDown(textbox, { key: 'ArrowUp' });
checkHighlightIs(getByRole('listbox'), 'three');
fireEvent.keyDown(textbox, { key: 'Enter' }); // selects the last option (three)
act(() => {
textbox.blur();
textbox.focus(); // opens the listbox again
});
checkHighlightIs(getByRole('listbox'), null);
});
});
describe('prop: autoSelect', () => {
it('should not clear on blur when value does not match any option', () => {
const handleChange = spy();
const options = ['one', 'two'];
render(
<Autocomplete
freeSolo
autoSelect
options={options}
onChange={handleChange}
renderInput={(params) => <TextField {...params} autoFocus />}
/>,
);
const textbox = screen.getByRole('combobox');
fireEvent.change(textbox, { target: { value: 'o' } });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.change(textbox, { target: { value: 'oo' } });
act(() => {
textbox.blur();
});
expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).to.deep.equal('oo');
});
it('should add new value when autoSelect & multiple on blur', () => {
const handleChange = spy();
const options = ['one', 'two'];
render(
<Autocomplete
autoSelect
multiple
value={[options[0]]}
openOnFocus
options={options}
onChange={handleChange}
renderInput={(params) => <TextField {...params} autoFocus />}
/>,
);
const textbox = screen.getByRole('combobox');
act(() => {
fireEvent.change(textbox, { target: { value: 't' } });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
textbox.blur();
});
expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).to.deep.equal(options);
});
it('should add new value when autoSelect & multiple & freeSolo on blur', () => {
const handleChange = spy();
render(
<Autocomplete
autoSelect
freeSolo
multiple
onChange={handleChange}
options={[]}
renderInput={(params) => <TextField {...params} autoFocus />}
/>,
);
fireEvent.change(document.activeElement, { target: { value: 'a' } });
act(() => {
document.activeElement.blur();
});
expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).to.deep.equal(['a']);
});
});
describe('prop: multiple', () => {
it('should not crash', () => {
const { getByRole } = render(
<Autocomplete
openOnFocus
options={[]}
renderInput={(params) => <TextField {...params} />}
multiple
/>,
);
const input = getByRole('combobox');
act(() => {
input.focus();
document.activeElement.blur();
input.focus();
});
});
it('should remove the last option', () => {
const handleChange = spy();
const options = ['one', 'two'];
const { getAllByTestId } = render(
<Autocomplete
options={[]}
defaultValue={options}
onChange={handleChange}
renderInput={(params) => <TextField {...params} />}
multiple
/>,
);
fireEvent.click(getAllByTestId('CancelIcon')[1]);
expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).to.deep.equal([options[0]]);
});
it('navigates between different tags', () => {
const handleChange = spy();
const options = ['one', 'two'];
render(
<Autocomplete
defaultValue={options}
options={options}
onChange={handleChange}
renderInput={(params) => <TextField {...params} autoFocus />}
multiple
/>,
);
const textbox = screen.getByRole('combobox');
const [firstSelectedValue, secondSelectedValue] = screen.getAllByRole('button');
fireEvent.keyDown(textbox, { key: 'ArrowLeft' });
expect(secondSelectedValue).toHaveFocus();
fireEvent.keyDown(secondSelectedValue, { key: 'ArrowLeft' });
expect(firstSelectedValue).toHaveFocus();
fireEvent.keyDown(firstSelectedValue, { key: 'Backspace' });
expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).to.deep.equal([options[1]]);
expect(textbox).toHaveFocus();
});
it('deletes a focused tag when pressing the delete key', () => {
const handleChange = spy();
const options = ['one', 'two'];
render(
<Autocomplete
defaultValue={options}
options={options}
onChange={handleChange}
renderInput={(params) => <TextField {...params} autoFocus />}
multiple
/>,
);
const textbox = screen.getByRole('combobox');
const [firstSelectedValue, secondSelectedValue] = screen.getAllByRole('button');
// check that no tags get deleted when the tag is not a focused tag
fireEvent.keyDown(textbox, { key: 'Delete' });
expect(handleChange.callCount).to.equal(0);
expect(textbox).toHaveFocus();
// expect on focused tag to delete when pressing delete key
fireEvent.keyDown(textbox, { key: 'ArrowLeft' });
expect(secondSelectedValue).toHaveFocus();
fireEvent.keyDown(secondSelectedValue, { key: 'ArrowLeft' });
expect(firstSelectedValue).toHaveFocus();
fireEvent.keyDown(firstSelectedValue, { key: 'Delete' });
expect(handleChange.callCount).to.equal(1);
expect(textbox).toHaveFocus();
});
it('should keep listbox open on pressing left or right keys when inputValue is not empty', () => {
const handleClose = spy();
const options = ['one', 'two', 'three'];
const { getByRole } = render(
<Autocomplete
options={options}
onClose={handleClose}
renderInput={(params) => <TextField {...params} autoFocus />}
multiple
inputValue="tw"
/>,
);
const textbox = getByRole('combobox');
fireEvent.mouseDown(textbox);
fireEvent.keyDown(textbox, { key: 'ArrowLeft' });
expect(handleClose.callCount).to.equal(0);
expect(textbox).to.have.attribute('aria-expanded', 'true');
});
it('should close listbox on pressing left or right keys when inputValue is empty', () => {
const handleClose = spy();
const options = ['one', 'two', 'three'];
const { getByRole } = render(
<Autocomplete
options={options}
onClose={handleClose}
renderInput={(params) => <TextField {...params} autoFocus />}
multiple
inputValue=""
/>,
);
const textbox = getByRole('combobox');
fireEvent.mouseDown(textbox);
fireEvent.keyDown(textbox, { key: 'ArrowLeft' });
expect(handleClose.callCount).to.equal(1);
expect(textbox).to.have.attribute('aria-expanded', 'false');
});
it('should not crash if a tag is missing', () => {
const handleChange = spy();
const options = ['one', 'two'];
render(
<Autocomplete
defaultValue={options}
options={options}
value={options}
renderTags={(value, getTagProps) =>
value
.filter((x, index) => index === 1)
.map((option, index) => {
const { key, ...tagProps } = getTagProps({ index });
return <Chip key={key} label={option.title} {...tagProps} />;
})
}
onChange={handleChange}
renderInput={(params) => <TextField {...params} autoFocus />}
multiple
/>,
);
const textbox = screen.getByRole('combobox');
const [firstSelectedValue] = screen.getAllByRole('button');
fireEvent.keyDown(textbox, { key: 'ArrowLeft' });
// skip value "two"
expect(firstSelectedValue).toHaveFocus();
fireEvent.keyDown(firstSelectedValue, { key: 'ArrowRight' });
expect(textbox).toHaveFocus();
});
it('should not call onChange function for duplicate values', () => {
const handleChange = spy();
const options = ['one', 'two'];
render(
<Autocomplete
freeSolo
defaultValue={options}
options={options}
onChange={handleChange}
renderInput={(params) => <TextField {...params} autoFocus />}
multiple
/>,
);
const textbox = screen.getByRole('combobox');
fireEvent.change(textbox, { target: { value: 'two' } });
fireEvent.keyDown(textbox, { key: 'Enter' });
expect(handleChange.callCount).to.equal(0);
fireEvent.change(textbox, { target: { value: 'three' } });
fireEvent.keyDown(textbox, { key: 'Enter' });
expect(handleChange.callCount).to.equal(1);
});
it('has no textbox value', () => {
render(
<Autocomplete
options={['one', 'two', 'three']}
renderInput={(params) => <TextField {...params} />}
multiple
value={['one', 'two']}
/>,
);
expect(screen.getByRole('combobox')).to.have.property('value', '');
});
it('should fail validation if a required field has no value', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
// Enable once https://github.com/jsdom/jsdom/issues/2898 is resolved
this.skip();
}
const handleSubmit = spy((event) => event.preventDefault());
render(
<form onSubmit={handleSubmit}>
<Autocomplete
multiple
options={['one', 'two']}
renderInput={(params) => <TextField {...params} required />}
value={[]}
/>
<button type="submit">Submit</button>
</form>,
);
screen.getByRole('button', { name: 'Submit' }).click();
expect(handleSubmit.callCount).to.equal(0);
});
it('should fail validation if a required field has a value', function test() {
// Unclear how native Constraint validation can be enabled for `multiple`
if (/jsdom/.test(window.navigator.userAgent)) {
// Enable once https://github.com/jsdom/jsdom/issues/2898 is resolved
// The test is passing in JSDOM but form validation is buggy in JSDOM so we rather skip than have false confidence
this.skip();
}
const handleSubmit = spy((event) => event.preventDefault());
render(
<form onSubmit={handleSubmit}>
<Autocomplete
multiple
options={['one', 'two']}
renderInput={(params) => <TextField {...params} required />}
value={['one']}
/>
<button type="submit">Submit</button>
</form>,
);
screen.getByRole('button', { name: 'Submit' }).click();
expect(handleSubmit.callCount).to.equal(0);
});
});
it('should trigger a form expectedly', () => {
const handleSubmit = spy();
function Test(props) {
const { key, ...others } = props;
return (
<div
onKeyDown={(event) => {
if (!event.defaultPrevented && event.key === 'Enter') {
handleSubmit();
}
}}
>
<Autocomplete
options={['one', 'two']}
renderInput={(props2) => <TextField {...props2} autoFocus />}
key={key}
{...others}
/>
</div>
);
}
const { setProps } = render(<Test />);
let textbox = screen.getByRole('combobox');
fireEvent.keyDown(textbox, { key: 'Enter' });
expect(handleSubmit.callCount).to.equal(1);
fireEvent.change(textbox, { target: { value: 'o' } });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.keyDown(textbox, { key: 'Enter' });
expect(handleSubmit.callCount).to.equal(1);
fireEvent.keyDown(textbox, { key: 'Enter' });
expect(handleSubmit.callCount).to.equal(2);
setProps({ key: 'test-2', multiple: true, freeSolo: true });
textbox = screen.getByRole('combobox');
fireEvent.change(textbox, { target: { value: 'o' } });
fireEvent.keyDown(textbox, { key: 'Enter' });
expect(handleSubmit.callCount).to.equal(2);
fireEvent.keyDown(textbox, { key: 'Enter' });
expect(handleSubmit.callCount).to.equal(3);
setProps({ key: 'test-3', freeSolo: true });
textbox = screen.getByRole('combobox');
fireEvent.change(textbox, { target: { value: 'o' } });
fireEvent.keyDown(textbox, { key: 'Enter' });
expect(handleSubmit.callCount).to.equal(4);
});
it('should not open the autocomplete popup when deleting chips', async () => {
const { queryByRole, queryByText, user } = render(
<Autocomplete
multiple
options={['one', 'two', 'three']}
defaultValue={['one']}
renderInput={(params) => <TextField {...params} autoFocus />}
/>,
);
expect(queryByRole('listbox')).to.equal(null);
const chip = queryByText('one').parentElement;
expect(chip).not.to.equal(null);
// Delete the chip
await user.click(chip.getElementsByClassName(chipClasses.deleteIcon)[0]);
expect(queryByText('one')).to.equal(null);
expect(queryByRole('listbox')).to.equal(null);
});
it('should toggle the autocomplete popup when clicking the popup indicator', async () => {
const { queryByRole, getByRole, user } = render(
<Autocomplete
multiple
options={['One', 'Two', 'Three']}
renderInput={(params) => <TextField {...params} autoFocus />}
/>,
);
expect(queryByRole('listbox')).to.equal(null);
const popupIndicator = getByRole('button', { name: 'Open' });
await user.click(popupIndicator);
expect(queryByRole('listbox')).not.to.equal(null);
await user.click(popupIndicator);
expect(queryByRole('listbox')).to.equal(null);
});
describe('prop: getOptionDisabled', () => {
it('should prevent the disabled option to trigger actions but allow focus with disabledItemsFocusable', () => {
const handleSubmit = spy();
const handleChange = spy();
const { getAllByRole } = render(
<div
onKeyDown={(event) => {
if (!event.defaultPrevented && event.key === 'Enter') {
handleSubmit();
}
}}
>
<Autocomplete
disabledItemsFocusable
getOptionDisabled={(option) => option === 'two'}
onChange={handleChange}
openOnFocus
options={['one', 'two', 'three']}
renderInput={(props2) => <TextField {...props2} autoFocus />}
/>
</div>,
);
let options;
const textbox = screen.getByRole('combobox');
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
options = getAllByRole('option');
expect(textbox).to.have.attribute('aria-activedescendant', options[1].getAttribute('id'));
fireEvent.keyDown(textbox, { key: 'Enter' });
expect(handleSubmit.callCount).to.equal(0);
expect(handleChange.callCount).to.equal(0);
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
options = getAllByRole('option');
expect(textbox).to.have.attribute('aria-activedescendant', options[0].getAttribute('id'));
fireEvent.keyDown(textbox, { key: 'Enter' });
expect(handleSubmit.callCount).to.equal(0);
expect(handleChange.callCount).to.equal(1);
});
it('should skip disabled options when navigating via keyboard', () => {
const { getByRole } = render(
<Autocomplete
getOptionDisabled={(option) => option === 'two'}
openOnFocus
options={['one', 'two', 'three']}
renderInput={(props) => <TextField {...props} autoFocus />}
/>,
);
const textbox = getByRole('combobox');
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
checkHighlightIs(getByRole('listbox'), 'one');
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
checkHighlightIs(getByRole('listbox'), 'three');
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
checkHighlightIs(getByRole('listbox'), 'one');
});
it('should skip disabled options at the end of the list when navigating via keyboard', () => {
const { getByRole } = render(
<Autocomplete
getOptionDisabled={(option) => option === 'three' || option === 'four'}
openOnFocus
options={['one', 'two', 'three', 'four']}
renderInput={(props) => <TextField {...props} autoFocus />}
/>,
);
const textbox = getByRole('combobox');
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
checkHighlightIs(getByRole('listbox'), 'one');
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
checkHighlightIs(getByRole('listbox'), 'two');
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
checkHighlightIs(getByRole('listbox'), 'one');
});
it('should skip the first and last disabled options in the list when navigating via keyboard', () => {
const { getByRole } = render(
<Autocomplete
getOptionDisabled={(option) => option === 'one' || option === 'five'}
openOnFocus
options={['one', 'two', 'three', 'four', 'five']}
renderInput={(props) => <TextField {...props} autoFocus />}
/>,
);
const textbox = getByRole('combobox');
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
checkHighlightIs(getByRole('listbox'), 'two');