-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathSelect.test.js
1755 lines (1485 loc) · 56.6 KB
/
Select.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 { expect } from 'chai';
import { spy, stub } from 'sinon';
import {
ErrorBoundary,
act,
createRenderer,
fireEvent,
screen,
reactMajor,
} from '@mui/internal-test-utils';
import { createTheme, ThemeProvider } from '@mui/material/styles';
import MenuItem, { menuItemClasses } from '@mui/material/MenuItem';
import ListSubheader from '@mui/material/ListSubheader';
import InputBase from '@mui/material/InputBase';
import OutlinedInput from '@mui/material/OutlinedInput';
import InputLabel from '@mui/material/InputLabel';
import Select from '@mui/material/Select';
import Divider from '@mui/material/Divider';
import classes from './selectClasses';
import { nativeSelectClasses } from '../NativeSelect';
import describeConformance from '../../test/describeConformance';
describe('<Select />', () => {
const { clock, render } = createRenderer({ clock: 'fake' });
describeConformance(<Select value="" />, () => ({
classes,
inheritComponent: OutlinedInput,
render,
refInstanceof: window.HTMLDivElement,
muiName: 'MuiSelect',
skip: ['componentProp', 'componentsProp', 'themeVariants', 'themeStyleOverrides'],
}));
describe('prop: inputProps', () => {
it('should be able to provide a custom classes property', () => {
render(
<Select
inputProps={{
classes: { select: 'select' },
}}
value=""
/>,
);
expect(document.querySelector(`.${classes.select}`)).to.have.class('select');
});
});
it('should be able to mount the component', () => {
const { container } = render(
<Select value={10}>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>,
);
expect(container.querySelector('input')).to.have.property('value', '10');
});
specify('the trigger is in tab order', () => {
const { getByRole } = render(
<Select value="">
<MenuItem value="">None</MenuItem>
</Select>,
);
expect(getByRole('combobox')).to.have.property('tabIndex', 0);
});
it('should accept null child', () => {
render(
<Select open value={10}>
{null}
<MenuItem value={10}>Ten</MenuItem>
</Select>,
);
});
['', 0, false, undefined, NaN].forEach((value) =>
it(`should support conditional rendering with "${value}"`, () => {
render(
<Select open value={2}>
{value && <MenuItem value={1}>One</MenuItem>}
<MenuItem value={2}>Two</MenuItem>
</Select>,
);
}),
);
it('should have an input with [aria-hidden] by default', () => {
const { container } = render(
<Select value="10">
<MenuItem value="10">Ten</MenuItem>
</Select>,
);
expect(container.querySelector('input')).to.have.attribute('aria-hidden', 'true');
});
it('should ignore onBlur when the menu opens', async () => {
// mousedown calls focus while click opens moving the focus to an item
// this means the trigger is blurred immediately
const handleBlur = spy();
const { getByRole, getAllByRole, queryByRole } = render(
<Select
onBlur={handleBlur}
value=""
onMouseDown={(event) => {
// simulating certain platforms that focus on mousedown
if (event.defaultPrevented === false) {
event.currentTarget.focus();
}
}}
>
<MenuItem value="">none</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
</Select>,
);
const trigger = getByRole('combobox');
fireEvent.mouseDown(trigger);
expect(handleBlur.callCount).to.equal(0);
expect(getByRole('listbox')).not.to.equal(null);
const options = getAllByRole('option');
fireEvent.mouseDown(options[0]);
await act(async () => {
options[0].click();
});
expect(handleBlur.callCount).to.equal(0);
expect(queryByRole('listbox', { hidden: false })).to.equal(null);
});
it('options should have a data-value attribute', () => {
render(
<Select open value={10}>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
</Select>,
);
const options = screen.getAllByRole('option');
expect(options[0]).to.have.attribute('data-value', '10');
expect(options[1]).to.have.attribute('data-value', '20');
});
[' ', 'ArrowUp', 'ArrowDown', 'Enter'].forEach((key) => {
it(`should open menu when pressed ${key} key on select`, async () => {
render(
<Select value="">
<MenuItem value="">none</MenuItem>
</Select>,
);
const trigger = screen.getByRole('combobox');
await act(async () => {
trigger.focus();
});
fireEvent.keyDown(trigger, { key });
expect(screen.getByRole('listbox', { hidden: false })).not.to.equal(null);
fireEvent.keyUp(screen.getAllByRole('option')[0], { key });
expect(screen.getByRole('listbox', { hidden: false })).not.to.equal(null);
});
});
it('should pass "name" as part of the event.target for onBlur', async () => {
const handleBlur = stub().callsFake((event) => event.target.name);
const { getByRole } = render(
<Select onBlur={handleBlur} name="blur-testing" value="">
<MenuItem value="">none</MenuItem>
</Select>,
);
const button = getByRole('combobox');
await act(async () => {
button.focus();
button.blur();
});
expect(handleBlur.callCount).to.equal(1);
expect(handleBlur.firstCall.returnValue).to.equal('blur-testing');
});
it('should call onClose when the backdrop is clicked', async () => {
const handleClose = spy();
const { getByTestId } = render(
<Select
MenuProps={{ BackdropProps: { 'data-testid': 'backdrop' } }}
onClose={handleClose}
open
value=""
>
<MenuItem value="">none</MenuItem>
</Select>,
);
await act(async () => {
getByTestId('backdrop').click();
});
expect(handleClose.callCount).to.equal(1);
});
it('should call onClose when the same option is selected', () => {
const handleChange = spy();
const handleClose = spy();
render(
<Select open onChange={handleChange} onClose={handleClose} value="second">
<MenuItem value="first" />
<MenuItem value="second" />
</Select>,
);
screen.getByRole('option', { selected: true }).click();
expect(handleChange.callCount).to.equal(0);
expect(handleClose.callCount).to.equal(1);
});
it('should focus select when its label is clicked', () => {
const { getByRole, getByTestId } = render(
<React.Fragment>
<InputLabel id="my$label" data-testid="label" />
<Select value="" labelId="my$label" />
</React.Fragment>,
);
fireEvent.click(getByTestId('label'));
expect(getByRole('combobox')).toHaveFocus();
});
it('should focus list if no selection', () => {
const { getByRole } = render(<Select value="" autoFocus />);
fireEvent.mouseDown(getByRole('combobox'));
// TODO not matching WAI-ARIA authoring practices. It should focus the first (or selected) item.
expect(getByRole('listbox')).toHaveFocus();
});
describe('prop: onChange', () => {
it('should get selected element from arguments', async () => {
const onChangeHandler = spy();
const { getAllByRole, getByRole } = render(
<Select onChange={onChangeHandler} value="0">
<MenuItem value="0" />
<MenuItem value="1" />
<MenuItem value="2" />
</Select>,
);
fireEvent.mouseDown(getByRole('combobox'));
await act(async () => {
getAllByRole('option')[1].click();
});
expect(onChangeHandler.calledOnce).to.equal(true);
const selected = onChangeHandler.args[0][1];
expect(React.isValidElement(selected)).to.equal(true);
});
it('should call onChange before onClose', async () => {
const eventLog = [];
const onChangeHandler = spy(() => eventLog.push('CHANGE_EVENT'));
const onCloseHandler = spy(() => eventLog.push('CLOSE_EVENT'));
const { getAllByRole, getByRole } = render(
<Select onChange={onChangeHandler} onClose={onCloseHandler} value="0">
<MenuItem value="0" />
<MenuItem value="1" />
</Select>,
);
fireEvent.mouseDown(getByRole('combobox'));
await act(async () => {
getAllByRole('option')[1].click();
});
expect(eventLog).to.deep.equal(['CHANGE_EVENT', 'CLOSE_EVENT']);
});
it('should not be called if selected element has the current value (value did not change)', async () => {
const onChangeHandler = spy();
const { getAllByRole, getByRole } = render(
<Select onChange={onChangeHandler} value="1">
<MenuItem value="0" />
<MenuItem value="1" />
<MenuItem value="2" />
</Select>,
);
fireEvent.mouseDown(getByRole('combobox'));
await act(async () => {
getAllByRole('option')[1].click();
});
expect(onChangeHandler.callCount).to.equal(0);
});
});
describe('prop: defaultOpen', () => {
it('should be open on mount', () => {
const { getByRole } = render(<Select defaultOpen value="" />);
expect(getByRole('combobox', { hidden: true })).to.have.attribute('aria-expanded', 'true');
});
});
describe('prop: value', () => {
it('should select the option based on the number value', () => {
render(
<Select open value={20}>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>,
);
const options = screen.getAllByRole('option');
expect(options[0]).not.to.have.attribute('aria-selected', 'true');
expect(options[1]).to.have.attribute('aria-selected', 'true');
expect(options[2]).not.to.have.attribute('aria-selected', 'true');
});
it('should select the option based on the string value', () => {
render(
<Select open value="20">
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>,
);
const options = screen.getAllByRole('option');
expect(options[0]).not.to.have.attribute('aria-selected', 'true');
expect(options[1]).to.have.attribute('aria-selected', 'true');
expect(options[2]).not.to.have.attribute('aria-selected', 'true');
});
it('should select only the option that matches the object', () => {
const obj1 = { id: 1 };
const obj2 = { id: 2 };
render(
<Select open value={obj1}>
<MenuItem value={obj1}>1</MenuItem>
<MenuItem value={obj2}>2</MenuItem>
</Select>,
);
const options = screen.getAllByRole('option');
expect(options[0]).to.have.attribute('aria-selected', 'true');
expect(options[1]).not.to.have.attribute('aria-selected', 'true');
});
it('should be able to use an object', () => {
const value = {};
const { getByRole } = render(
<Select value={value}>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={value}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>,
);
expect(getByRole('combobox')).to.have.text('Twenty');
});
describe('warnings', () => {
it('warns when the value is not present in any option', () => {
const errorMessage =
'MUI: You have provided an out-of-range value `20` for the select component.';
let expectedOccurrences = 2;
if (reactMajor === 18) {
expectedOccurrences = 3;
}
expect(() =>
render(
<Select value={20}>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>,
),
).toWarnDev(Array(expectedOccurrences).fill(errorMessage));
});
});
});
it('should not have the selectable option selected when inital value provided is empty string on Select with ListSubHeader item', () => {
render(
<Select open value="">
<ListSubheader>Category 1</ListSubheader>
<MenuItem value={10}>Ten</MenuItem>
<ListSubheader>Category 2</ListSubheader>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>,
);
const options = screen.getAllByRole('option');
expect(options[1]).not.to.have.class(menuItemClasses.selected);
});
describe('SVG icon', () => {
it('should not present an SVG icon when native and multiple are specified', () => {
const { container } = render(
<Select native multiple value={[0, 1]}>
<option value={0}>Zero</option>
<option value={1}>One</option>
<option value={2}>Two</option>
</Select>,
);
expect(container.querySelector('svg')).to.equal(null);
});
it('should present an SVG icon', () => {
const { container } = render(
<Select native value={1}>
<option value={0}>Zero</option>
<option value={1}>One</option>
<option value={2}>Two</option>
</Select>,
);
expect(container.querySelector('svg')).toBeVisible();
});
});
describe('accessibility', () => {
it('sets aria-expanded="true" when the listbox is displayed', () => {
// since we make the rest of the UI inaccessible when open this doesn't
// technically matter. This is only here in case we keep the rest accessible
const { getByRole } = render(<Select open value="" />);
expect(getByRole('combobox', { hidden: true })).to.have.attribute('aria-expanded', 'true');
});
specify('ARIA 1.2: aria-expanded="false" if the listbox isn\'t displayed', () => {
const { getByRole } = render(<Select value="" />);
expect(getByRole('combobox')).to.have.attribute('aria-expanded', 'false');
});
it('sets aria-disabled="true" when component is disabled', () => {
const { getByRole } = render(<Select disabled value="" />);
expect(getByRole('combobox')).to.have.attribute('aria-disabled', 'true');
});
it('sets disabled attribute in input when component is disabled', () => {
const { container } = render(<Select disabled value="" />);
expect(container.querySelector('input')).to.have.property('disabled', true);
});
specify('aria-disabled is not present if component is not disabled', () => {
const { getByRole } = render(<Select disabled={false} value="" />);
expect(getByRole('combobox')).not.to.have.attribute('aria-disabled');
});
it('indicates that activating the button displays a listbox', () => {
const { getByRole } = render(<Select value="" />);
expect(getByRole('combobox')).to.have.attribute('aria-haspopup', 'listbox');
});
it('renders an element with listbox behavior', () => {
const { getByRole } = render(<Select open value="" />);
expect(getByRole('listbox')).toBeVisible();
});
it('indicates that input element has combobox role and aria-controls set to id of listbox', () => {
const { getByRole } = render(<Select open value="" />);
const listboxId = getByRole('listbox').id;
expect(getByRole('combobox', { hidden: true })).to.have.attribute('aria-controls', listboxId);
});
specify('the listbox is focusable', async () => {
const { getByRole } = render(<Select open value="" />);
await act(async () => {
getByRole('listbox').focus();
});
expect(getByRole('listbox')).toHaveFocus();
});
it('identifies each selectable element containing an option', () => {
const { getAllByRole } = render(
<Select open value="">
<MenuItem value="1">First</MenuItem>
<MenuItem value="2">Second</MenuItem>
</Select>,
);
const options = getAllByRole('option');
expect(options[0]).to.have.text('First');
expect(options[1]).to.have.text('Second');
});
it('indicates the selected option', () => {
const { getAllByRole } = render(
<Select open value="2">
<MenuItem value="1">First</MenuItem>
<MenuItem value="2">Second</MenuItem>
</Select>,
);
expect(getAllByRole('option')[1]).to.have.attribute('aria-selected', 'true');
});
describe('when the first child is a ListSubheader', () => {
it('first selectable option is focused to use the arrow', () => {
const { getAllByRole } = render(
<Select defaultValue="" open>
<ListSubheader>Category 1</ListSubheader>
<MenuItem value={1}>Option 1</MenuItem>
<MenuItem value={2}>Option 2</MenuItem>
<ListSubheader>Category 2</ListSubheader>
<MenuItem value={3}>Option 3</MenuItem>
<MenuItem value={4}>Option 4</MenuItem>
</Select>,
);
const options = getAllByRole('option');
expect(options[1]).to.have.attribute('tabindex', '0');
fireEvent.keyDown(options[1], { key: 'ArrowDown' });
fireEvent.keyDown(options[2], { key: 'ArrowDown' });
fireEvent.keyDown(options[4], { key: 'Enter' });
expect(options[4]).to.have.attribute('aria-selected', 'true');
});
describe('when also the second child is a ListSubheader', () => {
it('first selectable option is focused to use the arrow', () => {
const { getAllByRole } = render(
<Select defaultValue="" open>
<ListSubheader>Empty category</ListSubheader>
<ListSubheader>Category 1</ListSubheader>
<MenuItem value={1}>Option 1</MenuItem>
<MenuItem value={2}>Option 2</MenuItem>
<ListSubheader>Category 2</ListSubheader>
<MenuItem value={3}>Option 3</MenuItem>
<MenuItem value={4}>Option 4</MenuItem>
</Select>,
);
const options = getAllByRole('option');
expect(options[2]).to.have.attribute('tabindex', '0');
fireEvent.keyDown(options[2], { key: 'ArrowDown' });
fireEvent.keyDown(options[3], { key: 'ArrowDown' });
fireEvent.keyDown(options[5], { key: 'Enter' });
expect(options[5]).to.have.attribute('aria-selected', 'true');
});
});
describe('when the second child is null', () => {
it('first selectable option is focused to use the arrow', () => {
const { getAllByRole } = render(
<Select defaultValue="" open>
<ListSubheader>Category 1</ListSubheader>
{null}
<MenuItem value={1}>Option 1</MenuItem>
<MenuItem value={2}>Option 2</MenuItem>
<ListSubheader>Category 2</ListSubheader>
<MenuItem value={3}>Option 3</MenuItem>
<MenuItem value={4}>Option 4</MenuItem>
</Select>,
);
const options = getAllByRole('option');
expect(options[1]).to.have.attribute('tabindex', '0');
fireEvent.keyDown(options[1], { key: 'ArrowDown' });
fireEvent.keyDown(options[2], { key: 'ArrowDown' });
fireEvent.keyDown(options[4], { key: 'Enter' });
expect(options[4]).to.have.attribute('aria-selected', 'true');
});
});
['', 0, false, undefined, NaN].forEach((value) =>
describe(`when the second child is conditionally rendering with "${value}"`, () => {
it('first selectable option is focused to use the arrow', () => {
const { getAllByRole } = render(
<Select defaultValue="" open>
<ListSubheader>Category 1</ListSubheader>
{value && <MenuItem value={1}>One</MenuItem>}
<MenuItem value={1}>Option 1</MenuItem>
<MenuItem value={2}>Option 2</MenuItem>
<ListSubheader>Category 2</ListSubheader>
<MenuItem value={3}>Option 3</MenuItem>
<MenuItem value={4}>Option 4</MenuItem>
</Select>,
);
const options = getAllByRole('option');
expect(options[1]).to.have.attribute('tabindex', '0');
fireEvent.keyDown(options[1], { key: 'ArrowDown' });
fireEvent.keyDown(options[2], { key: 'ArrowDown' });
fireEvent.keyDown(options[4], { key: 'Enter' });
expect(options[4]).to.have.attribute('aria-selected', 'true');
});
}),
);
});
describe('when the first child is a ListSubheader wrapped in a custom component', () => {
describe('with the `muiSkipListHighlight` static field', () => {
function WrappedListSubheader(props) {
return <ListSubheader {...props} />;
}
WrappedListSubheader.muiSkipListHighlight = true;
it('highlights the first selectable option below the header', () => {
const { getByText } = render(
<Select defaultValue="" open>
<WrappedListSubheader>Category 1</WrappedListSubheader>
<MenuItem value={1}>Option 1</MenuItem>
<MenuItem value={2}>Option 2</MenuItem>
<WrappedListSubheader>Category 2</WrappedListSubheader>
<MenuItem value={3}>Option 3</MenuItem>
<MenuItem value={4}>Option 4</MenuItem>
</Select>,
);
const expectedHighlightedOption = getByText('Option 1');
expect(expectedHighlightedOption).to.have.attribute('tabindex', '0');
});
});
describe('with the `muiSkipListHighlight` prop', () => {
function WrappedListSubheader(props) {
const { muiSkipListHighlight, ...other } = props;
return <ListSubheader {...other} />;
}
it('highlights the first selectable option below the header', () => {
const { getByText } = render(
<Select defaultValue="" open>
<WrappedListSubheader muiSkipListHighlight>Category 1</WrappedListSubheader>
<MenuItem value={1}>Option 1</MenuItem>
<MenuItem value={2}>Option 2</MenuItem>
<WrappedListSubheader muiSkipListHighlight>Category 2</WrappedListSubheader>
<MenuItem value={3}>Option 3</MenuItem>
<MenuItem value={4}>Option 4</MenuItem>
</Select>,
);
const expectedHighlightedOption = getByText('Option 1');
expect(expectedHighlightedOption).to.have.attribute('tabindex', '0');
});
});
});
describe('when the first child is a MenuItem disabled', () => {
it('highlights the first selectable option below the header', () => {
const { getAllByRole } = render(
<Select defaultValue="" open>
<MenuItem value="" disabled>
<em>None</em>
</MenuItem>
<ListSubheader>Category 1</ListSubheader>
<MenuItem value={1}>Option 1</MenuItem>
<MenuItem value={2}>Option 2</MenuItem>
<ListSubheader>Category 2</ListSubheader>
<MenuItem value={3}>Option 3</MenuItem>
<MenuItem value={4}>Option 4</MenuItem>
</Select>,
);
const options = getAllByRole('option');
expect(options[2]).to.have.attribute('tabindex', '0');
fireEvent.keyDown(options[2], { key: 'ArrowDown' });
fireEvent.keyDown(options[3], { key: 'ArrowDown' });
fireEvent.keyDown(options[5], { key: 'Enter' });
expect(options[5]).to.have.attribute('aria-selected', 'true');
});
});
it('it will fallback to its content for the accessible name when it has no name', () => {
const { getByRole } = render(<Select value="" />);
// TODO what is the accessible name actually?
expect(getByRole('combobox')).not.to.have.attribute('aria-labelledby');
});
it('is labelled by itself when it has a name', () => {
const { getByRole } = render(<Select name="select" value="" />);
expect(getByRole('combobox')).to.have.attribute(
'aria-labelledby',
getByRole('combobox').getAttribute('id'),
);
});
it('is labelled by itself when it has an id which is preferred over name', () => {
const { getAllByRole } = render(
<React.Fragment>
<span id="select-1-label">Chose first option:</span>
<Select id="select-1" labelId="select-1-label" name="select" value="" />
<span id="select-2-label">Chose second option:</span>
<Select id="select-2" labelId="select-2-label" name="select" value="" />
</React.Fragment>,
);
const triggers = getAllByRole('combobox');
expect(triggers[0]).to.have.attribute(
'aria-labelledby',
`select-1-label ${triggers[0].getAttribute('id')}`,
);
expect(triggers[1]).to.have.attribute(
'aria-labelledby',
`select-2-label ${triggers[1].getAttribute('id')}`,
);
});
it('can be labelled by an additional element if its id is provided in `labelId`', () => {
const { getByRole } = render(
<React.Fragment>
<span id="select-label">Choose one:</span>
<Select labelId="select-label" name="select" value="" />
</React.Fragment>,
);
expect(getByRole('combobox')).to.have.attribute(
'aria-labelledby',
`select-label ${getByRole('combobox').getAttribute('id')}`,
);
});
specify('the list of options is not labelled by default', () => {
const { getByRole } = render(<Select open value="" />);
expect(getByRole('listbox')).not.to.have.attribute('aria-labelledby');
});
specify('the list of options can be labelled by providing `labelId`', () => {
const { getByRole } = render(
<React.Fragment>
<span id="select-label">Choose one:</span>
<Select labelId="select-label" open value="" />
</React.Fragment>,
);
expect(getByRole('listbox')).to.have.attribute('aria-labelledby', 'select-label');
});
it('should have appropriate accessible description when provided in props', () => {
const { getByRole } = render(
<React.Fragment>
<Select aria-describedby="select-helper-text" value="" />
<span id="select-helper-text">Helper text content</span>
</React.Fragment>,
);
const target = getByRole('combobox');
expect(target).to.have.attribute('aria-describedby', 'select-helper-text');
expect(target).toHaveAccessibleDescription('Helper text content');
});
});
describe('prop: readOnly', () => {
it('should not trigger any event with readOnly', async () => {
render(
<Select readOnly value="10">
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
</Select>,
);
const trigger = screen.getByRole('combobox');
await act(async () => {
trigger.focus();
});
fireEvent.keyDown(trigger, { key: 'ArrowDown' });
expect(screen.queryByRole('listbox')).to.equal(null);
fireEvent.keyUp(trigger, { key: 'ArrowDown' });
expect(screen.queryByRole('listbox')).to.equal(null);
});
});
describe('prop: MenuProps', () => {
it('should apply additional props to the Menu component', () => {
const onEntered = spy();
const { getByRole } = render(
<Select MenuProps={{ TransitionProps: { onEntered }, transitionDuration: 100 }} value="10">
<MenuItem value="10">Ten</MenuItem>
</Select>,
);
fireEvent.mouseDown(getByRole('combobox'));
clock.tick(99);
expect(onEntered.callCount).to.equal(0);
clock.tick(1);
expect(onEntered.callCount).to.equal(1);
});
it('should be able to override PaperProps minWidth', () => {
const { getByTestId } = render(
<Select
MenuProps={{ PaperProps: { 'data-testid': 'paper', style: { minWidth: 12 } } }}
open
value="10"
>
<MenuItem value="10">Ten</MenuItem>
</Select>,
);
expect(getByTestId('paper').style).to.have.property('minWidth', '12px');
});
// https://github.com/mui/material-ui/issues/38700
it('should merge `slotProps.paper` with the default Paper props', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
const { getByTestId, getByRole } = render(
<Select MenuProps={{ slotProps: { paper: { 'data-testid': 'paper' } } }} open value="10">
<MenuItem value="10">Ten</MenuItem>
</Select>,
);
const paper = getByTestId('paper');
const selectButton = getByRole('combobox', { hidden: true });
expect(paper.style).to.have.property('minWidth', `${selectButton.clientWidth}px`);
});
// https://github.com/mui/material-ui/issues/38949
it('should forward `slotProps` to menu', function test() {
const { getByTestId } = render(
<Select
MenuProps={{
slotProps: {
root: {
slotProps: {
backdrop: { 'data-testid': 'backdrop', style: { backgroundColor: 'red' } },
},
},
},
}}
open
value="10"
>
<MenuItem value="10">Ten</MenuItem>
</Select>,
);
const backdrop = getByTestId('backdrop');
expect(backdrop.style).to.have.property('backgroundColor', 'red');
});
});
describe('prop: SelectDisplayProps', () => {
it('should apply additional props to trigger element', () => {
const { getByRole } = render(
<Select SelectDisplayProps={{ 'data-test': 'SelectDisplay' }} value="10">
<MenuItem value="10">Ten</MenuItem>
</Select>,
);
expect(getByRole('combobox')).to.have.attribute('data-test', 'SelectDisplay');
});
});
describe('prop: displayEmpty', () => {
it('should display the selected item even if its value is empty', () => {
const { getByRole } = render(
<Select value="" displayEmpty>
<MenuItem value="">Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>,
);
expect(getByRole('combobox')).to.have.text('Ten');
});
it('should notch the outline to accommodate the label when displayEmpty', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
const { container } = render(
<Select value="" label="Age" displayEmpty>
<MenuItem value="">None</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
</Select>,
);
expect(container.querySelector('legend')).toHaveComputedStyle({
maxWidth: '100%',
});
});
});
describe('prop: renderValue', () => {
it('should use the prop to render the value', () => {
const renderValue = (x) => `0b${x.toString(2)}`;
const { getByRole } = render(
<Select renderValue={renderValue} value={4}>
<MenuItem value={2}>2</MenuItem>
<MenuItem value={4}>4</MenuItem>
</Select>,
);
expect(getByRole('combobox')).to.have.text('0b100');
});
});
describe('prop: open (controlled)', () => {
it('should not focus on close controlled select', async () => {
function ControlledWrapper() {
const [open, setOpen] = React.useState(false);
return (
<div>
<button type="button" id="open-select" onClick={() => setOpen(true)}>
Open select
</button>
<Select
MenuProps={{ transitionDuration: 0 }}
open={open}
onClose={() => setOpen(false)}
value=""
>
<MenuItem onClick={() => setOpen(false)}>close</MenuItem>
</Select>
</div>
);
}
const { container, getByRole } = render(<ControlledWrapper />);
const openSelect = container.querySelector('#open-select');
await act(async () => {
openSelect.focus();
});
fireEvent.click(openSelect);
const option = getByRole('option');
expect(option).toHaveFocus();
fireEvent.click(option);
expect(container.querySelectorAll(classes.focused).length).to.equal(0);
expect(openSelect).toHaveFocus();
});
it('should allow to control closing by passing onClose props', async () => {
function ControlledWrapper() {
const [open, setOpen] = React.useState(false);
return (
<Select
MenuProps={{ transitionDuration: 0 }}
open={open}
onClose={() => setOpen(false)}
onOpen={() => setOpen(true)}
value=""
>
<MenuItem onClick={() => setOpen(false)}>close</MenuItem>
</Select>
);
}
const { getByRole, queryByRole } = render(<ControlledWrapper />);
fireEvent.mouseDown(getByRole('combobox'));
expect(getByRole('listbox')).not.to.equal(null);
await act(async () => {
getByRole('option').click();
});
// react-transition-group uses one extra commit for exit to completely remove