-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathlistbox.spec.ts
More file actions
1005 lines (876 loc) · 39.9 KB
/
Copy pathlistbox.spec.ts
File metadata and controls
1005 lines (876 loc) · 39.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
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 {Component, DebugElement, signal, ChangeDetectionStrategy} from '@angular/core';
import {Listbox} from './listbox';
import {Option} from './option';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
import {Direction} from '@angular/cdk/bidi';
import {provideFakeDirectionality, runAccessibilityChecks} from '@angular/cdk/testing/private';
import {waitForMicrotasks} from '../private/testing/test-helpers';
interface ModifierKeys {
ctrlKey?: boolean;
shiftKey?: boolean;
altKey?: boolean;
metaKey?: boolean;
}
describe('Listbox', () => {
let fixture: ComponentFixture<ListboxExample | DefaultListboxExample>;
let listboxDebugElement: DebugElement;
let optionDebugElements: DebugElement[];
let listboxInstance: Listbox<unknown>;
let listboxElement: HTMLElement;
let optionElements: HTMLElement[];
const keydown = async (key: string, modifierKeys: ModifierKeys = {}) => {
listboxElement.dispatchEvent(
new KeyboardEvent('keydown', {
key,
bubbles: true,
...modifierKeys,
}),
);
await fixture.whenStable();
};
const click = async (index: number, eventInit?: PointerEventInit, targets?: HTMLElement[]) => {
(targets || optionElements)[index].dispatchEvent(
new PointerEvent('click', {
bubbles: true,
...eventInit,
}),
);
await fixture.whenStable();
};
const space = async (modifierKeys?: ModifierKeys) => await keydown(' ', modifierKeys);
const enter = async (modifierKeys?: ModifierKeys) => await keydown('Enter', modifierKeys);
const up = async (modifierKeys?: ModifierKeys) => await keydown('ArrowUp', modifierKeys);
const down = async (modifierKeys?: ModifierKeys) => await keydown('ArrowDown', modifierKeys);
const left = async (modifierKeys?: ModifierKeys) => await keydown('ArrowLeft', modifierKeys);
const right = async (modifierKeys?: ModifierKeys) => await keydown('ArrowRight', modifierKeys);
const home = async (modifierKeys?: ModifierKeys) => await keydown('Home', modifierKeys);
const end = async (modifierKeys?: ModifierKeys) => await keydown('End', modifierKeys);
const type = async (char: string) => await keydown(char);
async function setupListbox(opts?: {
orientation?: 'horizontal' | 'vertical';
disabled?: boolean;
readonly?: boolean;
value?: number[];
softDisabled?: boolean;
focusMode?: 'roving' | 'activedescendant';
multi?: boolean;
wrap?: boolean;
selectionMode?: 'follow' | 'explicit';
typeaheadDelay?: number;
disabledOptions?: number[];
options?: TestOption[];
textDirection?: Direction;
tabIndex?: number;
}) {
TestBed.configureTestingModule({
providers: [provideFakeDirectionality(opts?.textDirection ?? 'ltr')],
});
fixture = TestBed.createComponent(ListboxExample);
const testComponent = fixture.componentInstance as ListboxExample;
if (opts?.orientation !== undefined) testComponent.orientation = opts.orientation;
if (opts?.disabled !== undefined) testComponent.disabled = opts.disabled;
if (opts?.readonly !== undefined) testComponent.readonly = opts.readonly;
if (opts?.value !== undefined) testComponent.value = opts.value;
if (opts?.softDisabled !== undefined) testComponent.softDisabled = opts.softDisabled;
if (opts?.focusMode !== undefined) testComponent.focusMode = opts.focusMode;
if (opts?.multi !== undefined) testComponent.multi = opts.multi;
if (opts?.wrap !== undefined) testComponent.wrap = opts.wrap;
if (opts?.selectionMode !== undefined) testComponent.selectionMode = opts.selectionMode;
if (opts?.typeaheadDelay !== undefined) testComponent.typeaheadDelay = opts.typeaheadDelay;
if (opts?.options !== undefined) testComponent.options.set(opts.options);
if (opts?.tabIndex !== undefined) testComponent.tabIndex = opts.tabIndex;
if (opts?.disabledOptions !== undefined) {
const currentOptions = testComponent.options();
opts.disabledOptions.forEach(index => {
if (currentOptions[index]) currentOptions[index].disabled = true;
});
testComponent.options.set([...currentOptions]);
}
await fixture.whenStable();
defineTestVariables(fixture);
}
async function setupDefaultListbox() {
TestBed.configureTestingModule({
providers: [provideFakeDirectionality('ltr')],
});
const defaultFixture = TestBed.createComponent(DefaultListboxExample);
defaultFixture.detectChanges();
defineTestVariables(defaultFixture);
}
function defineTestVariables(fixture: ComponentFixture<unknown>) {
listboxDebugElement = fixture.debugElement.query(By.directive(Listbox));
optionDebugElements = fixture.debugElement.queryAll(By.directive(Option));
listboxInstance = listboxDebugElement.injector.get<Listbox<unknown>>(Listbox);
listboxElement = listboxDebugElement.nativeElement;
optionElements = optionDebugElements.map(option => option.nativeElement);
}
afterEach(async () => await runAccessibilityChecks(listboxElement));
describe('ARIA attributes and roles', () => {
describe('default configuration', () => {
beforeEach(async () => await setupDefaultListbox());
it('should correctly set the role attribute to "listbox"', () => {
expect(listboxElement.getAttribute('role')).toBe('listbox');
});
it('should correctly set the role attribute to "option" for the listbox options', () => {
optionElements.forEach(optionElement => {
expect(optionElement.getAttribute('role')).toBe('option');
});
});
it('should set aria-orientation to "vertical"', () => {
expect(listboxElement.getAttribute('aria-orientation')).toBe('vertical');
});
it('should set aria-disabled to "false"', () => {
expect(listboxElement.getAttribute('aria-disabled')).toBe('false');
});
it('should set aria-readonly to "false"', () => {
expect(listboxElement.getAttribute('aria-readonly')).toBe('false');
});
it('should set aria-multiselectable to "false"', () => {
expect(listboxElement.getAttribute('aria-multiselectable')).toBe('false');
});
it('should set aria-selected to "true" for the first option and "false" for others by default', () => {
expect(optionElements[0].getAttribute('aria-selected')).toBe('true');
expect(optionElements[1].getAttribute('aria-selected')).toBe('false');
expect(optionElements[2].getAttribute('aria-selected')).toBe('false');
});
});
describe('custom configuration', () => {
it('should be able to set aria-orientation to "horizontal"', async () => {
await setupListbox({orientation: 'horizontal'});
expect(listboxElement.getAttribute('aria-orientation')).toBe('horizontal');
});
it('should be able to set aria-disabled to "true"', async () => {
await setupListbox({disabled: true});
expect(listboxElement.getAttribute('aria-disabled')).toBe('true');
});
it('should be able to set aria-readonly to "true"', async () => {
await setupListbox({readonly: true});
expect(listboxElement.getAttribute('aria-readonly')).toBe('true');
});
it('should be able to set aria-multiselectable to "true"', async () => {
await setupListbox({multi: true});
expect(listboxElement.getAttribute('aria-multiselectable')).toBe('true');
});
it('should be able to override tabindex', async () => {
await setupListbox({tabIndex: -1});
expect(listboxElement.getAttribute('tabindex')).toBe('-1');
});
it('should set aria-selected to "true" for selected options', async () => {
await setupListbox({multi: true, value: [1, 3]});
expect(optionElements[0].getAttribute('aria-selected')).toBe('false');
expect(optionElements[1].getAttribute('aria-selected')).toBe('true');
expect(optionElements[2].getAttribute('aria-selected')).toBe('false');
expect(optionElements[3].getAttribute('aria-selected')).toBe('true');
expect(optionElements[4].getAttribute('aria-selected')).toBe('false');
});
it('should set aria-disabled to "true" for disabled options', async () => {
await setupListbox({disabledOptions: [1]});
expect(optionElements[0].getAttribute('aria-disabled')).toBe('false');
expect(optionElements[1].getAttribute('aria-disabled')).toBe('true');
expect(optionElements[2].getAttribute('aria-disabled')).toBe('false');
});
});
describe('roving focus mode', () => {
it('should have tabindex="-1" for the listbox when focusMode is "roving"', async () => {
await setupListbox({focusMode: 'roving'});
expect(listboxElement.getAttribute('tabindex')).toBe('-1');
});
it('should set tabindex="0" for the listbox when disabled and focusMode is "roving"', async () => {
await setupListbox({disabled: true, focusMode: 'roving'});
expect(listboxElement.getAttribute('tabindex')).toBe('0');
});
it('should set initial focus (tabindex="0") on the first non-disabled option if no values are set', async () => {
await setupListbox({focusMode: 'roving'});
expect(optionElements[0].getAttribute('tabindex')).toBe('0');
expect(optionElements[1].getAttribute('tabindex')).toBe('-1');
expect(optionElements[2].getAttribute('tabindex')).toBe('-1');
expect(optionElements[3].getAttribute('tabindex')).toBe('-1');
expect(optionElements[4].getAttribute('tabindex')).toBe('-1');
});
it('should set initial focus (tabindex="0") on the first selected option', async () => {
await setupListbox({focusMode: 'roving', value: [2]});
expect(optionElements[0].getAttribute('tabindex')).toBe('-1');
expect(optionElements[1].getAttribute('tabindex')).toBe('-1');
expect(optionElements[2].getAttribute('tabindex')).toBe('0');
expect(optionElements[3].getAttribute('tabindex')).toBe('-1');
expect(optionElements[4].getAttribute('tabindex')).toBe('-1');
});
it('should set initial focus (tabindex="0") on the first non-disabled option if selected option is disabled when softDisabled is false', async () => {
await setupListbox({
focusMode: 'roving',
value: [1],
disabledOptions: [0],
softDisabled: false,
});
expect(optionElements[0].getAttribute('tabindex')).toBe('-1');
expect(optionElements[1].getAttribute('tabindex')).toBe('0');
});
it('should set initial focus (tabindex="0") on the first option if selected option is disabled', async () => {
await setupListbox({
focusMode: 'roving',
value: [0],
disabledOptions: [0],
});
expect(optionElements[0].getAttribute('tabindex')).toBe('0');
expect(optionElements[1].getAttribute('tabindex')).toBe('-1');
});
it('should not have aria-activedescendant when focusMode is "roving"', async () => {
await setupListbox({focusMode: 'roving'});
expect(listboxElement.hasAttribute('aria-activedescendant')).toBe(false);
});
});
describe('activedescendant focus mode', () => {
it('should have tabindex="0" for the listbox', async () => {
await setupListbox({focusMode: 'activedescendant'});
expect(listboxElement.getAttribute('tabindex')).toBe('0');
});
it('should set aria-activedescendant to the ID of the first non-disabled option if no value is set', async () => {
await setupListbox({focusMode: 'activedescendant'});
expect(listboxElement.getAttribute('aria-activedescendant')).toBe(optionElements[0].id);
});
it('should set aria-activedescendant to the ID of the first selected option', async () => {
await setupListbox({focusMode: 'activedescendant', value: [2]});
expect(listboxElement.getAttribute('aria-activedescendant')).toBe(optionElements[2].id);
});
it('should set aria-activedescendant to the ID of the first non-disabled option if selected option is disabled', async () => {
await setupListbox({focusMode: 'activedescendant', value: [0], disabledOptions: [0]});
expect(listboxElement.getAttribute('aria-activedescendant')).toBe(optionElements[0].id);
});
it('should set aria-activedescendant to the ID of the first non-disabled option if selected option is disabled when softDisabled is false', async () => {
await setupListbox({
focusMode: 'activedescendant',
value: [1],
disabledOptions: [0],
softDisabled: false,
});
expect(listboxElement.getAttribute('aria-activedescendant')).toBe(optionElements[1].id);
});
it('should set tabindex="-1" for all options', async () => {
await setupListbox({focusMode: 'activedescendant'});
expect(optionElements[0].getAttribute('tabindex')).toBe('-1');
expect(optionElements[1].getAttribute('tabindex')).toBe('-1');
expect(optionElements[2].getAttribute('tabindex')).toBe('-1');
expect(optionElements[3].getAttribute('tabindex')).toBe('-1');
expect(optionElements[4].getAttribute('tabindex')).toBe('-1');
});
});
});
describe('value and selection', () => {
it('should select the options corresponding to the value input', async () => {
await setupListbox({multi: true, value: [1, 3]});
expect(optionElements[1].getAttribute('aria-selected')).toBe('true');
expect(optionElements[3].getAttribute('aria-selected')).toBe('true');
expect(listboxInstance.value()).toEqual([1, 3]);
});
it('should update the value model when an option is selected via UI (single select)', async () => {
await setupListbox({multi: false});
await click(1);
expect(listboxInstance.value()).toEqual([1]);
await click(2);
expect(listboxInstance.value()).toEqual([2]);
});
it('should update the value model when options are selected via UI (multi select)', async () => {
await setupListbox({multi: true});
await click(1);
expect(listboxInstance.value()).toEqual([1]);
await click(3);
expect(listboxInstance.value()).toEqual([1, 3]);
await click(1);
expect(listboxInstance.value()).toEqual([3]);
});
describe('pointer interactions', () => {
describe('single select', () => {
it('should select an option on click', async () => {
await setupListbox({multi: false});
await click(1);
expect(listboxInstance.value()).toEqual([1]);
expect(optionElements[1].getAttribute('aria-selected')).toBe('true');
});
it('should select a new option and deselect the old one on click', async () => {
await setupListbox({multi: false, value: [0]});
await click(1);
expect(listboxInstance.value()).toEqual([1]);
expect(optionElements[0].getAttribute('aria-selected')).toBe('false');
expect(optionElements[1].getAttribute('aria-selected')).toBe('true');
});
});
describe('multi select', () => {
describe('selection follows focus', () => {
it('should select only the clicked option with a simple click', async () => {
await setupListbox({multi: true, selectionMode: 'follow', value: [0]});
await click(1);
expect(listboxInstance.value()).toEqual([1]);
expect(optionElements[0].getAttribute('aria-selected')).toBe('false');
expect(optionElements[1].getAttribute('aria-selected')).toBe('true');
});
it('should toggle the selected state of an option with ctrl + click', async () => {
await setupListbox({multi: true, selectionMode: 'follow', value: [0]});
await click(1, {ctrlKey: true});
expect(listboxInstance.value().sort()).toEqual([0, 1]);
expect(optionElements[0].getAttribute('aria-selected')).toBe('true');
expect(optionElements[1].getAttribute('aria-selected')).toBe('true');
await click(0, {ctrlKey: true});
expect(listboxInstance.value()).toEqual([1]);
expect(optionElements[0].getAttribute('aria-selected')).toBe('false');
expect(optionElements[1].getAttribute('aria-selected')).toBe('true');
});
it('should select a range starting from the first option on shift + click', async () => {
await setupListbox({multi: true, selectionMode: 'follow', value: [0]});
await click(2, {shiftKey: true});
expect(listboxInstance.value().sort()).toEqual([0, 1, 2]);
expect(optionElements[0].getAttribute('aria-selected')).toBe('true');
expect(optionElements[1].getAttribute('aria-selected')).toBe('true');
expect(optionElements[2].getAttribute('aria-selected')).toBe('true');
});
it('should select a range starting from the current active option on shift + click', async () => {
await setupListbox({multi: true, selectionMode: 'follow'});
await click(1);
await click(3, {shiftKey: true});
expect(listboxInstance.value().sort()).toEqual([1, 2, 3]);
});
it('should not select disabled options on shift + click', async () => {
await setupListbox({multi: true, selectionMode: 'follow', disabledOptions: [1]});
await click(2, {shiftKey: true});
expect(listboxInstance.value()).toEqual([0, 2]);
});
});
describe('explicit selection', () => {
it('should toggle selection of the clicked option with a simple click', async () => {
await setupListbox({multi: true, selectionMode: 'explicit', value: [0]});
await click(1);
expect(listboxInstance.value().sort()).toEqual([0, 1]);
expect(optionElements[0].getAttribute('aria-selected')).toBe('true');
expect(optionElements[1].getAttribute('aria-selected')).toBe('true');
await click(0);
expect(listboxInstance.value()).toEqual([1]);
expect(optionElements[0].getAttribute('aria-selected')).toBe('false');
});
it('should select a range starting from the first option on shift + click', async () => {
await setupListbox({multi: true, selectionMode: 'explicit', value: [0]});
await click(2, {shiftKey: true});
expect(listboxInstance.value().sort()).toEqual([0, 1, 2]);
});
it('should select a range starting from the current active option on shift + click', async () => {
await setupListbox({multi: true, selectionMode: 'explicit'});
await click(1);
await click(3, {shiftKey: true});
expect(listboxInstance.value().sort()).toEqual([1, 2, 3]);
});
it('should not select disabled options on shift + click', async () => {
await setupListbox({multi: true, selectionMode: 'follow', disabledOptions: [1]});
await click(2, {shiftKey: true});
expect(listboxInstance.value()).toEqual([0, 2]);
});
});
});
});
describe('with shuffled items', () => {
it('should update collection order when items are shuffled', async () => {
await setupListbox({
options: [
{value: 1, label: 'Item 1', disabled: false},
{value: 2, label: 'Item 2', disabled: false},
{value: 3, label: 'Item 3', disabled: false},
],
});
// Verify initial DOM order
expect(optionElements.length).toBe(3);
expect(optionElements[0].textContent?.trim()).toBe('Item 1');
expect(optionElements[2].textContent?.trim()).toBe('Item 3');
const testComponent = fixture.componentInstance as ListboxExample;
const items = testComponent.options().reverse();
testComponent.options.set([...items]);
await fixture.whenStable();
await waitForMicrotasks();
// Re-query elements to check new DOM order
defineTestVariables(fixture);
expect(optionElements.length).toBe(3);
expect(optionElements[0].textContent?.trim()).toBe('Item 3');
expect(optionElements[2].textContent?.trim()).toBe('Item 1');
});
});
describe('structural validations', () => {
let consoleSpy: jasmine.Spy;
beforeEach(() => {
consoleSpy = spyOn(console, 'warn');
});
afterEach(async () => {
TestBed.resetTestingModule();
await setupListbox();
});
it('should warn when duplicate option values are detected inside ngListbox', () => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
imports: [ListboxWithDuplicateValues],
});
const duplicateFixture = TestBed.createComponent(ListboxWithDuplicateValues);
duplicateFixture.detectChanges();
expect(consoleSpy).toHaveBeenCalledWith(
"Duplicate option value 'item0' detected inside ngListbox.",
);
});
it('should warn when duplicate option IDs are detected inside ngListbox', () => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
imports: [ListboxWithDuplicateIds],
});
const duplicateFixture = TestBed.createComponent(ListboxWithDuplicateIds);
duplicateFixture.detectChanges();
expect(consoleSpy).toHaveBeenCalledWith(
"Duplicate option ID 'option0' detected inside ngListbox.",
);
});
it('should warn when single-select listbox has multiple selected options', () => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
imports: [SingleSelectListboxWithMultipleValues],
});
const singleSelectFixture = TestBed.createComponent(SingleSelectListboxWithMultipleValues);
singleSelectFixture.detectChanges();
expect(consoleSpy).toHaveBeenCalledWith(
'A single-select listbox should not have multiple selected options. Selected options: item0, item1',
);
});
});
describe('keyboard interactions', () => {
describe('single select', () => {
describe('selection follows focus', () => {
it('should select the next option on ArrowDown', async () => {
await setupListbox({multi: false, selectionMode: 'follow'});
await down();
expect(listboxInstance.value()).toEqual([1]);
expect(optionElements[1].getAttribute('aria-selected')).toBe('true');
await down();
expect(listboxInstance.value()).toEqual([2]);
expect(optionElements[2].getAttribute('aria-selected')).toBe('true');
});
it('should select the previous option on ArrowUp', async () => {
await setupListbox({multi: false, selectionMode: 'follow', value: [2]});
await up();
expect(listboxInstance.value()).toEqual([1]);
expect(optionElements[1].getAttribute('aria-selected')).toBe('true');
});
it('should select the first option on Home', async () => {
await setupListbox({multi: false, selectionMode: 'follow', value: [2]});
await home();
expect(listboxInstance.value()).toEqual([0]);
});
it('should select the last option on End', async () => {
await setupListbox({multi: false, selectionMode: 'follow', value: [2]});
await end();
expect(listboxInstance.value()).toEqual([4]);
});
});
describe('explicit selection', () => {
it('should move focus but not select on navigation', async () => {
await setupListbox({multi: false, selectionMode: 'explicit'});
await down();
await up();
await home();
await end();
expect(listboxInstance.value()).toEqual([]);
expect(optionElements[1].getAttribute('aria-selected')).toBe('false');
});
it('should select the focused option on Space', async () => {
await setupListbox({multi: false, selectionMode: 'explicit'});
await down();
await space();
expect(listboxInstance.value()).toEqual([1]);
expect(optionElements[1].getAttribute('aria-selected')).toBe('true');
await down();
await down();
await space();
expect(listboxInstance.value()).toEqual([3]);
expect(optionElements[1].getAttribute('aria-selected')).toBe('false');
expect(optionElements[3].getAttribute('aria-selected')).toBe('true');
});
it('should select the focused option on Enter', async () => {
await setupListbox({multi: false, selectionMode: 'explicit'});
await down();
await enter();
expect(listboxInstance.value()).toEqual([1]);
expect(optionElements[1].getAttribute('aria-selected')).toBe('true');
});
});
});
describe('multi select', () => {
describe('selection follows focus', () => {
it('should select only the focused option on ArrowDown (no modifier)', async () => {
await setupListbox({multi: true, selectionMode: 'follow', value: [0]});
await down();
expect(listboxInstance.value()).toEqual([1]);
expect(optionElements[0].getAttribute('aria-selected')).toBe('false');
expect(optionElements[1].getAttribute('aria-selected')).toBe('true');
});
it('should move focus but not change selection on ctrl + ArrowDown, then toggle with ctrl + Space', async () => {
await setupListbox({multi: true, selectionMode: 'follow', value: [0]});
await down({ctrlKey: true});
expect(listboxInstance.value()).toEqual([0]);
await space({ctrlKey: true});
expect(listboxInstance.value().sort()).toEqual([0, 1]);
});
it('should toggle selection of the focused item on ctrl + Space', async () => {
await setupListbox({multi: true, selectionMode: 'follow', value: [0]});
await space({ctrlKey: true});
expect(listboxInstance.value()).toEqual([]);
await down();
expect(listboxInstance.value()).toEqual([1]);
await space({ctrlKey: true});
expect(listboxInstance.value()).toEqual([]);
});
it('should extend selection on shift + ArrowDown', async () => {
await setupListbox({multi: true, selectionMode: 'follow', value: [0]});
await down({shiftKey: true});
expect(listboxInstance.value().sort()).toEqual([0, 1]);
await down({shiftKey: true});
expect(listboxInstance.value().sort()).toEqual([0, 1, 2]);
});
it('should select all on Ctrl+A, then select active on second Ctrl+A', async () => {
await setupListbox({multi: true, selectionMode: 'follow', value: [0]});
await keydown('A', {ctrlKey: true});
expect(listboxInstance.value().sort()).toEqual([0, 1, 2, 3, 4]);
await keydown('A', {ctrlKey: true});
expect(listboxInstance.value()).toEqual([0]);
});
});
describe('explicit selection', () => {
it('should move focus but not select on ArrowDown', async () => {
await setupListbox({multi: true, selectionMode: 'explicit'});
await down();
expect(listboxInstance.value()).toEqual([]);
});
it('should toggle selection of the focused item on Space', async () => {
await setupListbox({multi: true, selectionMode: 'explicit'});
await down();
await space();
expect(listboxInstance.value()).toEqual([1]);
await down();
await space();
expect(listboxInstance.value().sort()).toEqual([1, 2]);
await space();
expect(listboxInstance.value()).toEqual([1]);
});
it('should toggle selection of the focused item on Enter', async () => {
await setupListbox({multi: true, selectionMode: 'explicit'});
await down();
await enter();
expect(listboxInstance.value()).toEqual([1]);
});
it('should extend selection on Shift+ArrowDown', async () => {
await setupListbox({multi: true, selectionMode: 'explicit'});
await down({shiftKey: true});
expect(listboxInstance.value().sort()).toEqual([0, 1]);
await down({shiftKey: true});
expect(listboxInstance.value().sort()).toEqual([0, 1, 2]);
});
it('should move selection anchor along with focus during normal non-shift navigation', async () => {
await setupListbox({multi: true, selectionMode: 'explicit'});
await down({shiftKey: true});
expect(listboxInstance.value().sort()).toEqual([0, 1]);
await down();
await down();
await down();
await up({shiftKey: true});
expect(listboxInstance.value().sort()).toEqual([0, 1, 3, 4]);
});
it('should toggle selection of all options on Ctrl+A', async () => {
await setupListbox({multi: true, selectionMode: 'explicit', value: [0]});
await keydown('A', {ctrlKey: true});
expect(listboxInstance.value().sort()).toEqual([0, 1, 2, 3, 4]);
await keydown('A', {ctrlKey: true});
expect(listboxInstance.value()).toEqual([]);
});
});
});
});
});
function runNavigationTests(
focusMode: 'roving' | 'activedescendant',
isFocused: (index: number) => boolean,
) {
describe(`keyboard navigation (focusMode="${focusMode}")`, () => {
it('should move focus to the last focusable option on End', async () => {
await setupListbox({focusMode, disabledOptions: [4]});
await end();
expect(isFocused(4)).toBe(true);
});
it('should move focus to the first focusable option on Home', async () => {
await setupListbox({focusMode, disabledOptions: [0]});
await end();
await home();
expect(isFocused(0)).toBe(true);
});
it('should allow keyboard navigation if the group is readonly', async () => {
await setupListbox({focusMode, orientation: 'horizontal', readonly: true});
await right();
expect(isFocused(1)).toBe(true);
});
it('should wrap focus from last to first with ArrowDown when wrap is true (vertical)', async () => {
await setupListbox({focusMode, orientation: 'vertical', wrap: true});
for (let i = 0; i < optionElements.length - 1; i++) await down();
await down();
expect(isFocused(0)).toBe(true);
});
it('should not wrap focus from last to first with ArrowDown when wrap is false (vertical)', async () => {
await setupListbox({focusMode, orientation: 'vertical', wrap: false});
for (let i = 0; i < optionElements.length - 1; i++) await down();
await down();
expect(isFocused(optionElements.length - 1)).toBe(true);
});
describe('vertical orientation', () => {
it('should move focus to the next option on ArrowDown', async () => {
await setupListbox({focusMode, orientation: 'vertical'});
await down();
expect(isFocused(1)).toBe(true);
});
it('should skip disabled options with ArrowDown (softDisabled="false")', async () => {
await setupListbox({
focusMode,
orientation: 'vertical',
softDisabled: false,
disabledOptions: [1, 2],
});
await down();
expect(isFocused(3)).toBe(true);
});
it('should not skip disabled options with ArrowDown (softDisabled="true")', async () => {
await setupListbox({
focusMode,
orientation: 'vertical',
softDisabled: true,
disabledOptions: [1, 2],
});
await down();
expect(isFocused(1)).toBe(true);
});
it('should not be focusable ArrowDown when completely disabled', async () => {
await setupListbox({
focusMode,
orientation: 'vertical',
softDisabled: true,
disabled: true,
});
await down();
expect(isFocused(0)).toBe(false);
});
});
describe('horizontal orientation', () => {
it('should move focus to the next option on ArrowRight', async () => {
await setupListbox({focusMode, orientation: 'horizontal'});
await right();
expect(isFocused(1)).toBe(true);
});
describe('text direction rtl', () => {
it('should move focus to the next option on ArrowLeft (rtl)', async () => {
await setupListbox({focusMode, textDirection: 'rtl', orientation: 'horizontal'});
await left();
expect(isFocused(1)).toBe(true);
});
});
});
});
describe(`pointer navigation (focusMode="${focusMode}")`, () => {
it('should move focus to the clicked option', async () => {
await setupListbox({focusMode});
await click(3);
expect(isFocused(3)).toBe(true);
});
it('should move focus to the clicked disabled option', async () => {
await setupListbox({focusMode, disabledOptions: [2], softDisabled: true});
await click(2);
expect(isFocused(2)).toBe(true);
});
it('should move focus if listbox is readonly', async () => {
await setupListbox({focusMode, readonly: true});
await click(3);
expect(isFocused(3)).toBe(true);
});
});
describe(`typeahead functionality (focusMode="${focusMode}")`, () => {
const getOptions = () => [
{value: 0, label: 'Apple', disabled: false},
{value: 1, label: 'Apricot', disabled: false},
{value: 2, label: 'Banana', disabled: false},
{value: 3, label: 'Blueberry', disabled: false},
{value: 4, label: 'Orange', disabled: false},
];
it('should focus the first matching option when typing characters', async () => {
await setupListbox({options: getOptions(), focusMode});
await type('B');
expect(isFocused(2)).toBe(true);
await type('l');
expect(isFocused(3)).toBe(true);
});
it('should select the focused option if selectionMode is "follow"', async () => {
await setupListbox({options: getOptions(), focusMode, selectionMode: 'follow'});
await type('O');
expect(isFocused(4)).toBe(true);
expect(listboxInstance.value()).toEqual([4]);
expect(optionElements[4].getAttribute('aria-selected')).toBe('true');
});
it('should not select the focused option if selectionMode is "explicit"', async () => {
await setupListbox({options: getOptions(), focusMode, selectionMode: 'explicit'});
await type('O');
expect(isFocused(4)).toBe(true);
expect(listboxInstance.value()).toEqual([]);
expect(optionElements[4].getAttribute('aria-selected')).toBe('false');
});
it('should reset search term after typeaheadDelay', async () => {
await setupListbox({options: getOptions(), focusMode, typeaheadDelay: 100});
await type('A');
expect(isFocused(1)).toBe(true);
await new Promise(resolve => setTimeout(resolve, 100));
await type('A');
expect(isFocused(0)).toBe(true);
});
it('should skip disabled options with typeahead (softDisabled=false)', async () => {
await setupListbox({
options: getOptions(),
focusMode,
disabledOptions: [2],
softDisabled: false,
});
await type('B');
expect(isFocused(3)).toBe(true);
});
it('should focus disabled options with typeahead if softDisabled=true', async () => {
await setupListbox({
options: getOptions(),
focusMode,
disabledOptions: [2],
softDisabled: true,
});
await type('B');
expect(isFocused(2)).toBe(true);
});
});
}
runNavigationTests('roving', i => {
return optionElements[i] && optionElements[i].getAttribute('tabindex') === '0';
});
runNavigationTests('activedescendant', i => {
return (
listboxElement &&
optionElements[i] &&
listboxElement.getAttribute('aria-activedescendant') === optionElements[i].id
);
});
describe('failure cases', () => {
it('should handle an empty set of options gracefully', async () => {
await setupListbox({options: []});
expect(optionElements.length).toBe(0);
await down();
await space();
expect(listboxInstance.value()).toEqual([]);
});
});
describe('item mutations and focus stability', () => {
it('should recover focus by shifting to the default state if the active option is removed', async () => {
await setupListbox({focusMode: 'activedescendant'});
await click(2);
expect(listboxElement.getAttribute('aria-activedescendant')).toBe(optionElements[2].id);
const testComponent = fixture.componentInstance as ListboxExample;
const updatedOptions = testComponent.options().filter(o => o.value !== 2);
testComponent.options.set(updatedOptions);
await fixture.whenStable();
await waitForMicrotasks();
defineTestVariables(fixture);
expect(listboxElement.getAttribute('aria-activedescendant')).toBe(optionElements[0].id);
});
});
});
interface TestOption {
value: number;
label: string;
disabled: boolean;
}
@Component({
template: `
<ul
ngListbox
aria-label="Test Listbox"
[(value)]="value"
[disabled]="disabled"
[readonly]="readonly"
[focusMode]="focusMode"
[orientation]="orientation"
[softDisabled]="softDisabled"
[multi]="multi"
[wrap]="wrap"
[selectionMode]="selectionMode"
[typeaheadDelay]="typeaheadDelay"
[tabIndex]="tabIndex">
@for (option of options(); track option.value) {
<li ngOption [value]="option.value" [disabled]="option.disabled" [label]="option.label">{{ option.label }}</li>
}
</ul>
`,
imports: [Listbox, Option],
changeDetection: ChangeDetectionStrategy.Eager,
})
class ListboxExample {
options = signal<TestOption[]>([
{value: 0, label: 'Option 0', disabled: false},
{value: 1, label: 'Option 1', disabled: false},
{value: 2, label: 'Option 2', disabled: false},
{value: 3, label: 'Option 3', disabled: false},
{value: 4, label: 'Option 4', disabled: false},
]);
value: number[] = [];
disabled = false;
readonly = false;
softDisabled = true;
focusMode: 'roving' | 'activedescendant' = 'roving';
orientation: 'vertical' | 'horizontal' = 'vertical';
multi = false;
wrap = true;
selectionMode: 'follow' | 'explicit' = 'explicit';
typeaheadDelay = 500;
tabIndex: number | undefined = undefined;
}
@Component({
template: `
<ul aria-label="Test Listbox" ngListbox>
<li ngOption [value]="0">0</li>
<li ngOption [value]="1">1</li>
<li ngOption [value]="2">2</li>
</ul>
`,
imports: [Listbox, Option],
changeDetection: ChangeDetectionStrategy.Eager,
})
class DefaultListboxExample {}
@Component({
template: `
<ul ngListbox>
<li ngOption value="item0">Item 0</li>
<li ngOption value="item0">Item 0 Copy</li>
</ul>
`,
imports: [Listbox, Option],
changeDetection: ChangeDetectionStrategy.Eager,
})
class ListboxWithDuplicateValues {}
@Component({
template: `
<ul ngListbox>
<li ngOption value="item0" id="option0">Item 0</li>
<li ngOption value="item1" id="option0">Item 1</li>
</ul>
`,
imports: [Listbox, Option],
changeDetection: ChangeDetectionStrategy.Eager,
})
class ListboxWithDuplicateIds {}
@Component({
template: `
<ul ngListbox [multi]="false" [value]="['item0', 'item1']">
<li ngOption value="item0">Item 0</li>
<li ngOption value="item1">Item 1</li>
</ul>