forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselection-list.spec.ts
2056 lines (1646 loc) · 72.5 KB
/
selection-list.spec.ts
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 {A, D, DOWN_ARROW, END, ENTER, HOME, SPACE, UP_ARROW} from '@angular/cdk/keycodes';
import {
createKeyboardEvent,
dispatchEvent,
dispatchFakeEvent,
dispatchKeyboardEvent,
dispatchMouseEvent,
} from '@angular/cdk/testing/private';
import {
ChangeDetectionStrategy,
Component,
DebugElement,
QueryList,
ViewChildren,
} from '@angular/core';
import {
ComponentFixture,
TestBed,
fakeAsync,
flush,
tick,
waitForAsync,
} from '@angular/core/testing';
import {FormControl, FormsModule, NgModel, ReactiveFormsModule} from '@angular/forms';
import {ThemePalette} from '../core';
import {By} from '@angular/platform-browser';
import {
MAT_LIST_CONFIG,
MatListConfig,
MatListModule,
MatListOption,
MatListOptionTogglePosition,
MatSelectionList,
MatSelectionListChange,
} from './index';
describe('MatSelectionList without forms', () => {
const typeaheadInterval = 200;
describe('with list option', () => {
let fixture: ComponentFixture<SelectionListWithListOptions>;
let listOptions: DebugElement[];
let selectionList: DebugElement;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [
MatListModule,
SelectionListWithListOptions,
SelectionListWithCheckboxPositionAfter,
SelectionListWithListDisabled,
SelectionListWithOnlyOneOption,
SelectionListWithIndirectChildOptions,
SelectionListWithSelectedOptionAndValue,
],
});
}));
beforeEach(waitForAsync(() => {
fixture = TestBed.createComponent(SelectionListWithListOptions);
fixture.detectChanges();
listOptions = fixture.debugElement.queryAll(By.directive(MatListOption));
selectionList = fixture.debugElement.query(By.directive(MatSelectionList))!;
}));
function getFocusIndex() {
return listOptions.findIndex(o => document.activeElement === o.nativeElement);
}
it('should be able to set a value on a list option', () => {
const optionValues = ['inbox', 'starred', 'sent-mail', 'archive', 'drafts'];
optionValues.forEach((optionValue, index) => {
expect(listOptions[index].componentInstance.value).toBe(optionValue);
});
});
it('should not emit a selectionChange event if an option changed programmatically', () => {
spyOn(fixture.componentInstance, 'onSelectionChange');
expect(fixture.componentInstance.onSelectionChange).toHaveBeenCalledTimes(0);
listOptions[2].componentInstance.toggle();
fixture.detectChanges();
expect(fixture.componentInstance.onSelectionChange).toHaveBeenCalledTimes(0);
});
it('should emit a selectionChange event if an option got clicked', () => {
spyOn(fixture.componentInstance, 'onSelectionChange');
expect(fixture.componentInstance.onSelectionChange).toHaveBeenCalledTimes(0);
dispatchMouseEvent(listOptions[2].nativeElement, 'click');
fixture.detectChanges();
expect(fixture.componentInstance.onSelectionChange).toHaveBeenCalledTimes(1);
});
it('should be able to dispatch one selected item', () => {
let testListItem = listOptions[2].injector.get<MatListOption>(MatListOption);
let selectList =
selectionList.injector.get<MatSelectionList>(MatSelectionList).selectedOptions;
expect(selectList.selected.length).toBe(0);
expect(listOptions[2].nativeElement.getAttribute('aria-selected')).toBe('false');
testListItem.toggle();
fixture.detectChanges();
expect(listOptions[2].nativeElement.getAttribute('aria-selected')).toBe('true');
expect(listOptions[2].nativeElement.getAttribute('aria-disabled')).toBe('false');
expect(selectList.selected.length).toBe(1);
});
it('should be able to dispatch multiple selected items', () => {
let testListItem = listOptions[2].injector.get<MatListOption>(MatListOption);
let testListItem2 = listOptions[1].injector.get<MatListOption>(MatListOption);
let selectList =
selectionList.injector.get<MatSelectionList>(MatSelectionList).selectedOptions;
expect(selectList.selected.length).toBe(0);
expect(listOptions[2].nativeElement.getAttribute('aria-selected')).toBe('false');
expect(listOptions[1].nativeElement.getAttribute('aria-selected')).toBe('false');
testListItem.toggle();
fixture.detectChanges();
testListItem2.toggle();
fixture.detectChanges();
expect(selectList.selected.length).toBe(2);
expect(listOptions[2].nativeElement.getAttribute('aria-selected')).toBe('true');
expect(listOptions[1].nativeElement.getAttribute('aria-selected')).toBe('true');
expect(listOptions[1].nativeElement.getAttribute('aria-disabled')).toBe('false');
expect(listOptions[2].nativeElement.getAttribute('aria-disabled')).toBe('false');
});
it('should be able to specify a color for list options', () => {
const optionNativeElements = listOptions.map(option => option.nativeElement);
expect(optionNativeElements.every(option => !option.classList.contains('mat-primary'))).toBe(
true,
);
expect(optionNativeElements.every(option => !option.classList.contains('mat-warn'))).toBe(
true,
);
// All options will be set to the "warn" color.
fixture.componentInstance.selectionListColor = 'warn';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(optionNativeElements.every(option => !option.classList.contains('mat-primary'))).toBe(
true,
);
expect(optionNativeElements.every(option => option.classList.contains('mat-warn'))).toBe(
true,
);
// Color will be set explicitly for an option and should take precedence.
fixture.componentInstance.firstOptionColor = 'primary';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(optionNativeElements[0].classList).not.toContain('mat-accent');
expect(optionNativeElements[0].classList).not.toContain('mat-warn');
expect(
optionNativeElements.slice(1).every(option => option.classList.contains('mat-warn')),
).toBe(true);
});
it('should explicitly set the `accent` color', () => {
const classList = listOptions[0].nativeElement.classList;
fixture.componentInstance.firstOptionColor = 'primary';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(classList).not.toContain('mat-accent');
expect(classList).not.toContain('mat-warn');
fixture.componentInstance.firstOptionColor = 'accent';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(classList).not.toContain('mat-primary');
expect(classList).toContain('mat-accent');
expect(classList).not.toContain('mat-warn');
});
it('should be able to deselect an option', () => {
let testListItem = listOptions[2].injector.get<MatListOption>(MatListOption);
let selectList =
selectionList.injector.get<MatSelectionList>(MatSelectionList).selectedOptions;
expect(selectList.selected.length).toBe(0);
testListItem.toggle();
fixture.detectChanges();
expect(selectList.selected.length).toBe(1);
testListItem.toggle();
fixture.detectChanges();
expect(selectList.selected.length).toBe(0);
});
it('should not add the mdc-list-item--selected class (in multiple mode)', () => {
let testListItem = listOptions[2].injector.get<MatListOption>(MatListOption);
dispatchMouseEvent(testListItem._hostElement, 'click');
fixture.detectChanges();
expect(listOptions[2].nativeElement.classList.contains('mdc-list-item--selected')).toBe(
false,
);
});
it('should not allow selection of disabled items', () => {
let testListItem = listOptions[0].injector.get<MatListOption>(MatListOption);
let selectList =
selectionList.injector.get<MatSelectionList>(MatSelectionList).selectedOptions;
expect(selectList.selected.length).withContext('before click').toBe(0);
expect(listOptions[0].nativeElement.getAttribute('aria-disabled')).toBe('true');
dispatchMouseEvent(testListItem._hostElement, 'click');
fixture.detectChanges();
expect(selectList.selected.length).withContext('after click').toBe(0);
});
it('should be able to un-disable disabled items', () => {
let testListItem = listOptions[0].injector.get<MatListOption>(MatListOption);
expect(listOptions[0].nativeElement.getAttribute('aria-disabled')).toBe('true');
testListItem.disabled = false;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(listOptions[0].nativeElement.getAttribute('aria-disabled')).toBe('false');
});
it('should be able to use keyboard select with SPACE', () => {
const testListItem = listOptions[1].nativeElement as HTMLElement;
const selectList =
selectionList.injector.get<MatSelectionList>(MatSelectionList).selectedOptions;
expect(selectList.selected.length).toBe(0);
testListItem.focus();
expect(getFocusIndex()).toBe(1);
const event = dispatchKeyboardEvent(testListItem, 'keydown', SPACE);
fixture.detectChanges();
expect(selectList.selected.length).toBe(1);
expect(event.defaultPrevented).toBe(true);
});
it('should be able to select an item using ENTER', () => {
const testListItem = listOptions[1].nativeElement as HTMLElement;
const selectList =
selectionList.injector.get<MatSelectionList>(MatSelectionList).selectedOptions;
expect(selectList.selected.length).toBe(0);
testListItem.focus();
expect(getFocusIndex()).toBe(1);
const event = dispatchKeyboardEvent(testListItem, 'keydown', ENTER);
fixture.detectChanges();
expect(selectList.selected.length).toBe(1);
expect(event.defaultPrevented).toBe(true);
});
it('should not be able to toggle a disabled option using SPACE', () => {
const testListItem = listOptions[1].nativeElement as HTMLElement;
const selectionModel = selectionList.componentInstance.selectedOptions;
expect(selectionModel.selected.length).toBe(0);
listOptions[1].componentInstance.disabled = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
testListItem.focus();
expect(getFocusIndex()).toBe(1);
dispatchKeyboardEvent(testListItem, 'keydown', SPACE);
fixture.detectChanges();
expect(selectionModel.selected.length).toBe(0);
});
it('should focus the first option when the list takes focus for the first time', () => {
expect(listOptions[0].nativeElement.tabIndex).toBe(0);
expect(listOptions.slice(1).every(o => o.nativeElement.tabIndex === -1)).toBe(true);
});
it('should focus the first selected option when list receives focus', fakeAsync(() => {
dispatchMouseEvent(listOptions[2].nativeElement, 'click');
fixture.detectChanges();
expect(listOptions.map(o => o.nativeElement.tabIndex)).toEqual([-1, -1, 0, -1, -1]);
dispatchMouseEvent(listOptions[1].nativeElement, 'click');
fixture.detectChanges();
expect(listOptions.map(o => o.nativeElement.tabIndex)).toEqual([-1, 0, -1, -1, -1]);
// De-select both options to ensure that the first item in the list-item
// becomes the designated option for focus.
dispatchMouseEvent(listOptions[1].nativeElement, 'click');
dispatchMouseEvent(listOptions[2].nativeElement, 'click');
fixture.detectChanges();
expect(listOptions.map(o => o.nativeElement.tabIndex)).toEqual([0, -1, -1, -1, -1]);
}));
it('should focus previous item when press UP ARROW', () => {
listOptions[2].nativeElement.focus();
expect(getFocusIndex()).toEqual(2);
dispatchKeyboardEvent(listOptions[2].nativeElement, 'keydown', UP_ARROW);
fixture.detectChanges();
expect(getFocusIndex()).toEqual(1);
});
it('should focus next item when press DOWN ARROW', () => {
listOptions[2].nativeElement.focus();
expect(getFocusIndex()).toEqual(2);
dispatchKeyboardEvent(listOptions[2].nativeElement, 'keydown', DOWN_ARROW);
fixture.detectChanges();
expect(getFocusIndex()).toEqual(3);
});
it('should be able to focus the first item when pressing HOME', () => {
listOptions[2].nativeElement.focus();
expect(getFocusIndex()).toBe(2);
const event = dispatchKeyboardEvent(listOptions[2].nativeElement, 'keydown', HOME);
fixture.detectChanges();
expect(getFocusIndex()).toBe(0);
expect(event.defaultPrevented).toBe(true);
});
it('should focus the last item when pressing END', () => {
listOptions[2].nativeElement.focus();
expect(getFocusIndex()).toBe(2);
const event = dispatchKeyboardEvent(listOptions[2].nativeElement, 'keydown', END);
fixture.detectChanges();
expect(getFocusIndex()).toBe(4);
expect(event.defaultPrevented).toBe(true);
});
it('should select and deselect all items using ctrl + a', () => {
listOptions.forEach(option => (option.componentInstance.disabled = false));
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(listOptions.some(option => option.componentInstance.selected)).toBe(false);
listOptions[2].nativeElement.focus();
dispatchKeyboardEvent(listOptions[2].nativeElement, 'keydown', A, 'A', {control: true});
fixture.detectChanges();
expect(listOptions.every(option => option.componentInstance.selected)).toBe(true);
dispatchKeyboardEvent(listOptions[2].nativeElement, 'keydown', A, 'A', {control: true});
fixture.detectChanges();
expect(listOptions.every(option => option.componentInstance.selected)).toBe(false);
});
it('should select and deselect all items using meta + a', () => {
listOptions.forEach(option => (option.componentInstance.disabled = false));
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(listOptions.some(option => option.componentInstance.selected)).toBe(false);
listOptions[2].nativeElement.focus();
dispatchKeyboardEvent(listOptions[2].nativeElement, 'keydown', A, 'A', {meta: true});
fixture.detectChanges();
expect(listOptions.every(option => option.componentInstance.selected)).toBe(true);
dispatchKeyboardEvent(listOptions[2].nativeElement, 'keydown', A, 'A', {meta: true});
fixture.detectChanges();
expect(listOptions.every(option => option.componentInstance.selected)).toBe(false);
});
it('should not select disabled items when pressing ctrl + a', () => {
listOptions.slice(0, 2).forEach(option => (option.componentInstance.disabled = true));
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(listOptions.map(option => option.componentInstance.selected)).toEqual([
false,
false,
false,
false,
false,
]);
listOptions[3].nativeElement.focus();
dispatchKeyboardEvent(listOptions[3].nativeElement, 'keydown', A, 'A', {control: true});
fixture.detectChanges();
expect(listOptions.map(option => option.componentInstance.selected)).toEqual([
false,
false,
true,
true,
true,
]);
});
it('should select all items using ctrl + a if some items are selected', () => {
listOptions.slice(0, 2).forEach(option => (option.componentInstance.selected = true));
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(listOptions.some(option => option.componentInstance.selected)).toBe(true);
listOptions[2].nativeElement.focus();
dispatchKeyboardEvent(listOptions[2].nativeElement, 'keydown', A, 'A', {control: true});
fixture.detectChanges();
expect(listOptions.every(option => option.componentInstance.selected)).toBe(true);
});
it('should deselect all with ctrl + a if all options are selected', () => {
listOptions.forEach(option => (option.componentInstance.selected = true));
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(listOptions.every(option => option.componentInstance.selected)).toBe(true);
listOptions[2].nativeElement.focus();
dispatchKeyboardEvent(listOptions[2].nativeElement, 'keydown', A, 'A', {control: true});
fixture.detectChanges();
expect(listOptions.every(option => option.componentInstance.selected)).toBe(false);
});
it('should dispatch the selectionChange event when selecting via ctrl + a', () => {
const spy = spyOn(fixture.componentInstance, 'onSelectionChange');
listOptions.forEach(option => (option.componentInstance.disabled = false));
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
listOptions[2].nativeElement.focus();
dispatchKeyboardEvent(listOptions[2].nativeElement, 'keydown', A, 'A', {control: true});
fixture.detectChanges();
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith(
jasmine.objectContaining({
options: listOptions.map(option => option.componentInstance),
}),
);
});
it('should be able to jump focus down to an item by typing', fakeAsync(() => {
const firstOption = listOptions[0].nativeElement;
firstOption.focus();
expect(getFocusIndex()).toBe(0);
dispatchEvent(firstOption, createKeyboardEvent('keydown', 83, 's'));
fixture.detectChanges();
tick(typeaheadInterval);
expect(getFocusIndex()).toBe(1);
dispatchEvent(firstOption, createKeyboardEvent('keydown', 68, 'd'));
fixture.detectChanges();
tick(typeaheadInterval);
expect(getFocusIndex()).toBe(4);
}));
it('should be able to skip to an item by typing', fakeAsync(() => {
listOptions[0].nativeElement.focus();
expect(getFocusIndex()).toBe(0);
dispatchKeyboardEvent(listOptions[0].nativeElement, 'keydown', D, 'd');
fixture.detectChanges();
tick(typeaheadInterval);
expect(getFocusIndex()).toBe(4);
}));
// Test for "A" specifically, because it's a special case that can be used
// to select all values.
it('should be able to skip to an item by typing the letter "A"', fakeAsync(() => {
listOptions[0].nativeElement.focus();
expect(getFocusIndex()).toBe(0);
dispatchKeyboardEvent(listOptions[0].nativeElement, 'keydown', A, 'a');
fixture.detectChanges();
tick(typeaheadInterval);
expect(getFocusIndex()).toBe(3);
}));
it('should not select items while using the typeahead', fakeAsync(() => {
const testListItem = listOptions[1].nativeElement as HTMLElement;
const model = selectionList.injector.get<MatSelectionList>(MatSelectionList).selectedOptions;
testListItem.focus();
dispatchFakeEvent(testListItem, 'focus');
fixture.detectChanges();
expect(getFocusIndex()).toBe(1);
expect(model.isEmpty()).toBe(true);
dispatchKeyboardEvent(testListItem, 'keydown', D, 'd');
fixture.detectChanges();
tick(typeaheadInterval / 2); // Tick only half the typeahead timeout.
dispatchKeyboardEvent(testListItem, 'keydown', SPACE);
fixture.detectChanges();
// Tick the buffer timeout again as a new key has been pressed that resets
// the buffer timeout.
tick(typeaheadInterval);
expect(getFocusIndex()).toBe(4);
expect(model.isEmpty()).toBe(true);
}));
it('should be able to select all options', () => {
const list: MatSelectionList = selectionList.componentInstance;
expect(list.options.toArray().every(option => option.selected)).toBe(false);
const result = list.selectAll();
fixture.detectChanges();
expect(list.options.toArray().every(option => option.selected)).toBe(true);
expect(result).toEqual(list.options.toArray());
});
it('should be able to select all options, even if they are disabled', () => {
const list: MatSelectionList = selectionList.componentInstance;
list.options.forEach(option => (option.disabled = true));
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(list.options.toArray().every(option => option.selected)).toBe(false);
list.selectAll();
fixture.detectChanges();
expect(list.options.toArray().every(option => option.selected)).toBe(true);
});
it('should be able to deselect all options', () => {
const list: MatSelectionList = selectionList.componentInstance;
list.options.forEach(option => option.toggle());
expect(list.options.toArray().every(option => option.selected)).toBe(true);
const result = list.deselectAll();
fixture.detectChanges();
expect(list.options.toArray().every(option => option.selected)).toBe(false);
expect(result).toEqual(list.options.toArray());
});
it('should be able to deselect all options, even if they are disabled', () => {
const list: MatSelectionList = selectionList.componentInstance;
list.options.forEach(option => option.toggle());
expect(list.options.toArray().every(option => option.selected)).toBe(true);
list.options.forEach(option => (option.disabled = true));
fixture.detectChanges();
list.deselectAll();
fixture.detectChanges();
expect(list.options.toArray().every(option => option.selected)).toBe(false);
});
it('should update the list value when an item is selected programmatically', () => {
const list: MatSelectionList = selectionList.componentInstance;
expect(list.selectedOptions.isEmpty()).toBe(true);
listOptions[0].componentInstance.selected = true;
listOptions[2].componentInstance.selected = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(list.selectedOptions.isEmpty()).toBe(false);
expect(list.selectedOptions.isSelected(listOptions[0].componentInstance)).toBe(true);
expect(list.selectedOptions.isSelected(listOptions[2].componentInstance)).toBe(true);
});
it('should update the item selected state when it is selected via the model', () => {
const list: MatSelectionList = selectionList.componentInstance;
const item: MatListOption = listOptions[0].componentInstance;
expect(item.selected).toBe(false);
list.selectedOptions.select(item);
fixture.detectChanges();
expect(item.selected).toBe(true);
});
it('should set aria-multiselectable to true on the selection list element', () => {
expect(selectionList.nativeElement.getAttribute('aria-multiselectable')).toBe('true');
});
it('should be able to reach list options that are indirect descendants', () => {
const descendatsFixture = TestBed.createComponent(SelectionListWithIndirectChildOptions);
descendatsFixture.detectChanges();
listOptions = descendatsFixture.debugElement.queryAll(By.directive(MatListOption));
selectionList = descendatsFixture.debugElement.query(By.directive(MatSelectionList))!;
const list: MatSelectionList = selectionList.componentInstance;
expect(list.options.toArray().every(option => option.selected)).toBe(false);
list.selectAll();
descendatsFixture.detectChanges();
expect(list.options.toArray().every(option => option.selected)).toBe(true);
});
it('should disable list item ripples when the ripples on the list have been disabled', fakeAsync(() => {
const rippleTarget = fixture.nativeElement.querySelector(
'.mat-mdc-list-option:not(.mdc-list-item--disabled)',
);
dispatchMouseEvent(rippleTarget, 'mousedown');
dispatchMouseEvent(rippleTarget, 'mouseup');
// Flush the ripple enter animation.
dispatchFakeEvent(rippleTarget.querySelector('.mat-ripple-element')!, 'transitionend');
expect(rippleTarget.querySelectorAll('.mat-ripple-element').length)
.withContext('Expected ripples to be enabled by default.')
.toBe(1);
// Flush the ripple exit animation.
dispatchFakeEvent(rippleTarget.querySelector('.mat-ripple-element')!, 'transitionend');
expect(rippleTarget.querySelectorAll('.mat-ripple-element').length)
.withContext('Expected ripples to go away.')
.toBe(0);
fixture.componentInstance.listRippleDisabled = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
dispatchMouseEvent(rippleTarget, 'mousedown');
dispatchMouseEvent(rippleTarget, 'mouseup');
expect(rippleTarget.querySelectorAll('.mat-ripple-element').length)
.withContext('Expected no ripples after list ripples are disabled.')
.toBe(0);
}));
it('can bind both selected and value at the same time', () => {
const componentFixture = TestBed.createComponent(SelectionListWithSelectedOptionAndValue);
componentFixture.detectChanges();
const listItemEl = componentFixture.debugElement.query(By.directive(MatListOption))!;
expect(listItemEl.componentInstance.selected).toBe(true);
expect(listItemEl.componentInstance.value).toBe(componentFixture.componentInstance.itemValue);
});
it('should have a focus indicator', () => {
const optionNativeElements = listOptions.map(option => option.nativeElement as HTMLElement);
expect(
optionNativeElements.every(
element => element.querySelector('.mat-focus-indicator') !== null,
),
).toBe(true);
});
it('should hide the internal SVG', () => {
listOptions.forEach(option => {
const svg = option.nativeElement.querySelector('.mdc-checkbox svg');
expect(svg.getAttribute('aria-hidden')).toBe('true');
});
});
});
describe('multiple-selection with list option selected', () => {
let fixture: ComponentFixture<SelectionListWithSelectedOption>;
let listOptionElements: DebugElement[];
let selectionList: DebugElement;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [MatListModule, SelectionListWithSelectedOption],
});
}));
beforeEach(waitForAsync(() => {
fixture = TestBed.createComponent(SelectionListWithSelectedOption);
listOptionElements = fixture.debugElement.queryAll(By.directive(MatListOption))!;
selectionList = fixture.debugElement.query(By.directive(MatSelectionList))!;
fixture.detectChanges();
}));
it('should set its initial selected state in the selectedOptions', () => {
let options = listOptionElements.map(optionEl =>
optionEl.injector.get<MatListOption>(MatListOption),
);
let selectedOptions = selectionList.componentInstance.selectedOptions;
expect(selectedOptions.isSelected(options[0])).toBeFalse();
expect(selectedOptions.isSelected(options[1])).toBeTrue();
expect(selectedOptions.isSelected(options[2])).toBeTrue();
expect(selectedOptions.isSelected(options[3])).toBeFalse();
});
it('should focus the first selected option on first focus if an item is pre-selected', fakeAsync(() => {
// MDC manages the focus through setting a `tabindex` on the designated list item. We
// assert that the proper tabindex is set on the pre-selected option at index 1, and
// ensure that other options are not reachable through tab.
expect(listOptionElements.map(el => el.nativeElement.tabIndex)).toEqual([-1, 0, -1, -1]);
}));
});
describe('single-selection with list option selected', () => {
let fixture: ComponentFixture<SingleSelectionListWithSelectedOption>;
let listOptionElements: DebugElement[];
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [MatListModule, SingleSelectionListWithSelectedOption],
});
}));
beforeEach(waitForAsync(() => {
fixture = TestBed.createComponent(SingleSelectionListWithSelectedOption);
listOptionElements = fixture.debugElement.queryAll(By.directive(MatListOption))!;
fixture.detectChanges();
}));
it('displays radio indicators by default', () => {
expect(
listOptionElements[0].nativeElement.querySelector('input[type="radio"]'),
).not.toBeNull();
expect(
listOptionElements[1].nativeElement.querySelector('input[type="radio"]'),
).not.toBeNull();
expect(listOptionElements[0].nativeElement.classList).not.toContain(
'mdc-list-item--selected',
);
expect(listOptionElements[1].nativeElement.classList).not.toContain(
'mdc-list-item--selected',
);
});
});
describe('with token to hide radio indicators', () => {
let fixture: ComponentFixture<SingleSelectionListWithSelectedOption>;
let listOptionElements: DebugElement[];
beforeEach(waitForAsync(() => {
const matListConfig: MatListConfig = {hideSingleSelectionIndicator: true};
TestBed.configureTestingModule({
imports: [MatListModule, SingleSelectionListWithSelectedOption],
providers: [{provide: MAT_LIST_CONFIG, useValue: matListConfig}],
});
}));
beforeEach(waitForAsync(() => {
fixture = TestBed.createComponent(SingleSelectionListWithSelectedOption);
listOptionElements = fixture.debugElement.queryAll(By.directive(MatListOption))!;
fixture.detectChanges();
}));
it('does not display radio indicators', () => {
expect(listOptionElements[0].nativeElement.querySelector('input[type="radio"]')).toBeNull();
expect(listOptionElements[1].nativeElement.querySelector('input[type="radio"]')).toBeNull();
expect(listOptionElements[0].nativeElement.classList).not.toContain(
'mdc-list-item--selected',
);
expect(listOptionElements[1].nativeElement.getAttribute('aria-selected'))
.withContext('Expected second option to be selected')
.toBe('true');
expect(listOptionElements[1].nativeElement.classList).toContain('mdc-list-item--selected');
});
});
describe('with option disabled', () => {
let fixture: ComponentFixture<SelectionListWithDisabledOption>;
let listOptionEl: HTMLElement;
let listOption: MatListOption;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [MatListModule, SelectionListWithDisabledOption],
});
}));
beforeEach(waitForAsync(() => {
fixture = TestBed.createComponent(SelectionListWithDisabledOption);
const listOptionDebug = fixture.debugElement.query(By.directive(MatListOption))!;
listOption = listOptionDebug.componentInstance;
listOptionEl = listOptionDebug.nativeElement;
fixture.detectChanges();
}));
it('should disable ripples for disabled option', () => {
expect(listOption.rippleDisabled)
.withContext('Expected ripples to be enabled by default')
.toBe(false);
fixture.componentInstance.disableItem = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(listOption.rippleDisabled)
.withContext('Expected ripples to be disabled if option is disabled')
.toBe(true);
});
it('should apply the "mat-list-item-disabled" class properly', () => {
expect(listOptionEl.classList).not.toContain('mdc-list-item--disabled');
fixture.componentInstance.disableItem = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(listOptionEl.classList).toContain('mdc-list-item--disabled');
});
});
describe('with list disabled', () => {
let fixture: ComponentFixture<SelectionListWithListDisabled>;
let listOption: DebugElement[];
let selectionList: DebugElement;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [
MatListModule,
SelectionListWithListOptions,
SelectionListWithCheckboxPositionAfter,
SelectionListWithListDisabled,
SelectionListWithOnlyOneOption,
],
});
}));
beforeEach(waitForAsync(() => {
fixture = TestBed.createComponent(SelectionListWithListDisabled);
listOption = fixture.debugElement.queryAll(By.directive(MatListOption));
selectionList = fixture.debugElement.query(By.directive(MatSelectionList))!;
fixture.detectChanges();
}));
it('should not allow selection on disabled selection-list', () => {
let testListItem = listOption[2].injector.get<MatListOption>(MatListOption);
let selectList =
selectionList.injector.get<MatSelectionList>(MatSelectionList).selectedOptions;
expect(selectList.selected.length).toBe(0);
dispatchMouseEvent(testListItem._hostElement, 'click');
fixture.detectChanges();
expect(selectList.selected.length).toBe(0);
});
it('should update state of options if list state has changed', () => {
const testOption = listOption[2].componentInstance as MatListOption;
expect(testOption.rippleDisabled)
.withContext('Expected ripples of list option to be disabled')
.toBe(true);
fixture.componentInstance.disabled = false;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(testOption.rippleDisabled)
.withContext('Expected ripples of list option to be enabled')
.toBe(false);
});
// when the entire list is disabled, its listitems should always have tabindex="-1"
it('should remove all listitems from the tab order when disabled state is enabled', () => {
fixture.componentInstance.disabled = false;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
let testListItem = listOption[2].injector.get<MatListOption>(MatListOption);
testListItem.focus();
fixture.detectChanges();
expect(
listOption.filter(option => option.nativeElement.getAttribute('tabindex') === '0').length,
)
.withContext('Expected at least one list option to be in the tab order')
.toBeGreaterThanOrEqual(1);
fixture.componentInstance.disabled = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(
listOption.filter(option => option.nativeElement.getAttribute('tabindex') !== '-1').length,
)
.withContext('Expected all list options to be excluded from the tab order')
.toBe(0);
});
it('should not allow focusin event to change the tabindex', () => {
fixture.componentInstance.disabled = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(
listOption.filter(option => option.nativeElement.getAttribute('tabindex') !== '-1').length,
)
.withContext('Expected all list options to be excluded from the tab order')
.toBe(0);
listOption[1].triggerEventHandler('focusin', {target: listOption[1].nativeElement});
fixture.detectChanges();
expect(
listOption.filter(option => option.nativeElement.getAttribute('tabindex') !== '-1').length,
)
.withContext('Expected all list options to be excluded from the tab order')
.toBe(0);
});
});
describe('with checkbox position after', () => {
let fixture: ComponentFixture<SelectionListWithCheckboxPositionAfter>;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [
MatListModule,
SelectionListWithListOptions,
SelectionListWithCheckboxPositionAfter,
SelectionListWithListDisabled,
SelectionListWithOnlyOneOption,
],
});
}));
beforeEach(waitForAsync(() => {
fixture = TestBed.createComponent(SelectionListWithCheckboxPositionAfter);
fixture.detectChanges();
}));
it('should be able to customize checkbox position', () => {
expect(fixture.nativeElement.querySelector('.mdc-list-item__end .mdc-checkbox'))
.withContext('Expected checkbox to show up after content.')
.toBeTruthy();
expect(fixture.nativeElement.querySelector('.mdc-list-item__start .mdc-checkbox'))
.withContext('Expected no checkbox to show up before content.')
.toBeFalsy();
});
});
describe('with list item elements', () => {
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [MatListModule, SelectionListWithAvatar, SelectionListWithIcon],
});
}));
function expectCheckboxAtPosition(
listItemElement: HTMLElement,
position: MatListOptionTogglePosition,
) {
const containerSelector =
position === 'before' ? '.mdc-list-item__start' : 'mdc-list-item__end';
const checkboxPositionClass =
position === 'before'
? 'mdc-list-item--with-leading-checkbox'