-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathListView.test.js
More file actions
1735 lines (1509 loc) · 69.5 KB
/
ListView.test.js
File metadata and controls
1735 lines (1509 loc) · 69.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
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
/*
* Copyright 2021 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
jest.mock('@react-aria/live-announcer');
jest.mock('@react-aria/utils/src/scrollIntoView');
import {act, fireEvent, installPointerEvent, mockClickDefault, pointerMap, render as renderComponent, triggerTouch, within} from '@react-spectrum/test-utils-internal';
import {ActionButton} from '@react-spectrum/button';
import {announce} from '@react-aria/live-announcer';
import {FocusExample} from '../stories/ListViewActions.stories';
import {Item, ListView} from '../src';
import {Provider} from '@react-spectrum/provider';
import React from 'react';
import {renderEmptyState} from '../stories/ListView.stories';
import {scrollIntoView} from '@react-aria/utils';
import {Text} from '@react-spectrum/text';
import {theme} from '@react-spectrum/theme-default';
import {User} from '@react-aria/test-utils';
import userEvent from '@testing-library/user-event';
function pointerEvent(type, opts) {
let evt = new Event(type, {bubbles: true, cancelable: true});
Object.assign(evt, {
ctrlKey: false,
metaKey: false,
shiftKey: false,
altKey: false,
button: opts.button || 0,
width: 1,
height: 1
}, opts);
return evt;
}
describe('ListView', function () {
let offsetWidth, offsetHeight, scrollHeight;
let onSelectionChange = jest.fn();
let onAction = jest.fn();
let user;
let testUtilUser = new User();
let checkSelection = (onSelectionChange, selectedKeys) => {
expect(onSelectionChange).toHaveBeenCalledTimes(1);
expect(new Set(onSelectionChange.mock.calls[0][0])).toEqual(new Set(selectedKeys));
};
let items = [
{key: 'foo', label: 'Foo'},
{key: 'bar', label: 'Bar'},
{key: 'baz', label: 'Baz'}
];
let manyItems = [];
for (let i = 1; i <= 100; i++) {
manyItems.push({id: i, label: 'Foo ' + i});
}
let innerHeight, innerWidth;
beforeAll(function () {
user = userEvent.setup({delay: null, pointerMap});
innerHeight = window.innerHeight;
innerWidth = window.innerWidth;
Object.defineProperty(window, 'innerHeight', {value: 1000, configurable: true, writable: true});
Object.defineProperty(window, 'innerWidth', {value: 1000, configurable: true, writable: true});
offsetWidth = jest.spyOn(window.HTMLElement.prototype, 'clientWidth', 'get').mockImplementation(() => 1000);
offsetHeight = jest.spyOn(window.HTMLElement.prototype, 'clientHeight', 'get').mockImplementation(() => 1000);
scrollHeight = jest.spyOn(window.HTMLElement.prototype, 'scrollHeight', 'get').mockImplementation(() => 40);
jest.useFakeTimers();
});
afterEach(function () {
fireEvent.keyDown(document.activeElement, {key: 'Escape'});
fireEvent.keyUp(document.activeElement, {key: 'Escape'});
act(() => {jest.runAllTimers();});
jest.clearAllMocks();
});
afterAll(function () {
window.innerHeight = innerHeight;
window.innerWidth = innerWidth;
offsetWidth.mockReset();
offsetHeight.mockReset();
scrollHeight.mockReset();
});
let render = (children, locale = 'en-US', scale = 'medium') => {
let tree = renderComponent(
<Provider theme={theme} scale={scale} locale={locale}>
{children}
</Provider>
);
// Allow for Virtualizer layout to update
act(() => {jest.runAllTimers();});
return tree;
};
let renderList = (props = {}) => {
let {
locale,
scale,
...otherProps
} = props;
return render(
<ListView items={items} aria-label="List" {...otherProps}>
{item => (
<Item textValue={item.label}>
{item.label}
</Item>
)}
</ListView>,
locale,
scale
);
};
let renderListWithFocusables = (props = {}) => {
let {
locale,
scale,
...otherProps
} = props;
return render(
<ListView items={items} aria-label="List" {...otherProps}>
{item => (
<Item textValue={item.label}>
{item.label}
<ActionButton>button1 {item.label}</ActionButton>
<ActionButton>button2 {item.label}</ActionButton>
</Item>
)}
</ListView>,
locale,
scale
);
};
let getRow = (tree, text) => {
// Find by text, then go up to the element with the row role.
let el = tree.getByText(text);
while (el && !/row/.test(el.getAttribute('role'))) {
el = el.parentElement;
}
return el;
};
let moveFocus = (key, opts = {}) => {
fireEvent.keyDown(document.activeElement, {key, ...opts});
fireEvent.keyUp(document.activeElement, {key, ...opts});
};
let focusRow = (tree, text) => act(() => getRow(tree, text).focus());
it('renders a static listview', function () {
let {getByRole} = render(
<ListView aria-label="List" data-testid="test">
<Item>Foo</Item>
<Item>Bar</Item>
<Item>Baz</Item>
</ListView>
);
let grid = getByRole('grid');
let gridListTester = testUtilUser.createTester('GridList', {root: grid});
expect(grid).toBeVisible();
expect(grid).toHaveAttribute('aria-label', 'List');
expect(grid).toHaveAttribute('data-testid', 'test');
expect(grid).toHaveAttribute('aria-rowcount', '3');
expect(grid).toHaveAttribute('aria-colcount', '1');
let rows = gridListTester.rows;
expect(rows).toHaveLength(3);
expect(rows[0]).toHaveAttribute('aria-rowindex', '1');
expect(rows[1]).toHaveAttribute('aria-rowindex', '2');
expect(rows[2]).toHaveAttribute('aria-rowindex', '3');
let gridCells = gridListTester.cells({element: rows[0]});
expect(gridCells).toHaveLength(1);
expect(gridCells[0]).toHaveTextContent('Foo');
expect(gridCells[0]).toHaveAttribute('aria-colindex', '1');
});
it('renders a dynamic listview', function () {
let items = [
{key: 'foo', label: 'Foo'},
{key: 'bar', label: 'Bar'},
{key: 'baz', label: 'Baz'}
];
let {getByRole, getAllByRole} = render(
<ListView items={items} aria-label="List">
{item =>
<Item textValue={item.key}>{item.label}</Item>
}
</ListView>
);
let grid = getByRole('grid');
expect(grid).toBeVisible();
expect(grid).toHaveAttribute('aria-label', 'List');
expect(grid).toHaveAttribute('aria-rowcount', '3');
expect(grid).toHaveAttribute('aria-colcount', '1');
let rows = getAllByRole('row');
expect(rows).toHaveLength(3);
expect(rows[0]).toHaveAttribute('aria-rowindex', '1');
expect(rows[1]).toHaveAttribute('aria-rowindex', '2');
expect(rows[2]).toHaveAttribute('aria-rowindex', '3');
let gridCells = within(rows[0]).getAllByRole('gridcell');
expect(gridCells).toHaveLength(1);
expect(gridCells[0]).toHaveTextContent('Foo');
expect(gridCells[0]).toHaveAttribute('aria-colindex', '1');
});
it('renders a falsy ids', function () {
let items = [
{id: 0, label: 'Foo'},
{id: 1, label: 'Bar'}
];
let {getByRole, getAllByRole} = render(
<ListView items={items} aria-label="List">
{item =>
<Item textValue={item.label}>{item.label}</Item>
}
</ListView>
);
let grid = getByRole('grid');
expect(grid).toBeVisible();
let rows = getAllByRole('row');
expect(rows).toHaveLength(2);
let gridCells = within(rows[0]).getAllByRole('gridcell');
expect(gridCells).toHaveLength(1);
expect(gridCells[0]).toHaveTextContent('Foo');
});
it('should retain focus on the pressed child', async function () {
let tree = renderListWithFocusables();
let button = within(getRow(tree, 'Foo')).getAllByRole('button')[1];
await user.click(button);
expect(document.activeElement).toBe(button);
});
it('should focus the row if the cell is pressed', async function () {
let tree = renderList({selectionMode: 'single'});
let cell = within(getRow(tree, 'Bar')).getByRole('gridcell');
await user.click(cell);
act(() => {
jest.runAllTimers();
});
expect(document.activeElement).toBe(getRow(tree, 'Bar'));
});
it('should have an aria-label on the row for the row text content', function () {
let tree = renderList();
expect(getRow(tree, 'Foo')).toHaveAttribute('aria-label', 'Foo');
expect(getRow(tree, 'Bar')).toHaveAttribute('aria-label', 'Bar');
expect(getRow(tree, 'Baz')).toHaveAttribute('aria-label', 'Baz');
});
it('should label the checkboxes with the row label', function () {
let tree = renderList({selectionMode: 'single'});
let rows = tree.getAllByRole('row');
for (let row of rows) {
let checkbox = within(row).getByRole('checkbox');
expect(checkbox).toHaveAttribute('aria-labelledby', `${checkbox.id} ${row.id}`);
}
});
it('should disable nested elements when row is disabled', function () {
let tree = renderListWithFocusables({disabledKeys: ['foo'], selectionMode: 'multiple'});
let row = getRow(tree, 'Foo');
expect(row).toHaveAttribute('aria-disabled', 'true');
expect(row).not.toHaveAttribute('aria-selected');
expect(within(row).getByRole('checkbox')).toHaveAttribute('disabled');
let buttons = within(row).getAllByRole('button');
expect(buttons[0]).toHaveAttribute('disabled');
expect(buttons[1]).toHaveAttribute('disabled');
row = getRow(tree, 'Bar');
expect(row).not.toHaveAttribute('aria-disabled', 'true');
expect(row).toHaveAttribute('aria-selected', 'false');
expect(within(row).getByRole('checkbox')).not.toHaveAttribute('disabled');
buttons = within(row).getAllByRole('button');
expect(buttons[0]).not.toHaveAttribute('disabled');
expect(buttons[1]).not.toHaveAttribute('disabled');
});
it('should disable nested elements with disabledBehavior="selection"', function () {
let tree = renderListWithFocusables({disabledKeys: ['foo'], disabledBehavior: 'selection', selectionMode: 'multiple'});
let row = getRow(tree, 'Foo');
expect(row).not.toHaveAttribute('aria-disabled');
expect(row).not.toHaveAttribute('aria-selected');
expect(within(row).getByRole('checkbox')).toHaveAttribute('disabled');
let buttons = within(row).getAllByRole('button');
expect(buttons[0]).not.toHaveAttribute('disabled');
expect(buttons[1]).not.toHaveAttribute('disabled');
});
describe('keyboard focus', function () {
describe('Type to select', function () {
it('focuses the correct cell when typing', async function () {
let tree = renderList();
let target = getRow(tree, 'Baz');
let grid = tree.getByRole('grid');
await user.tab();
fireEvent.keyDown(grid, {key: 'B'});
fireEvent.keyUp(grid, {key: 'Enter'});
fireEvent.keyDown(grid, {key: 'A'});
fireEvent.keyUp(grid, {key: 'A'});
fireEvent.keyDown(grid, {key: 'Z'});
fireEvent.keyUp(grid, {key: 'Z'});
expect(document.activeElement).toBe(target);
});
});
describe('ArrowRight', function () {
it('should not move focus if no focusables present', async function () {
let tree = renderList();
let start = getRow(tree, 'Foo');
await user.tab();
moveFocus('ArrowRight');
expect(document.activeElement).toBe(start);
});
describe('with cell focusables', function () {
it('should move focus to next cell and back to row', async function () {
let tree = renderListWithFocusables();
let start = getRow(tree, 'Foo');
let focusables = within(start).getAllByRole('button');
await user.tab();
moveFocus('ArrowRight');
expect(document.activeElement).toBe(focusables[0]);
moveFocus('ArrowRight');
expect(document.activeElement).toBe(focusables[1]);
moveFocus('ArrowRight');
expect(document.activeElement).toBe(start);
});
it('should move focus to previous cell in RTL', async function () {
let tree = renderListWithFocusables({locale: 'ar-AE'});
// Should move from button two to button one
let start = within(getRow(tree, 'Foo')).getAllByRole('button')[1];
let end = within(getRow(tree, 'Foo')).getAllByRole('button')[0];
// Need to press to set a modality, otherwise useSelectableCollection will think this is a tab operation
await user.click(start);
expect(document.activeElement).toHaveTextContent('button2 Foo');
expect(document.activeElement).toBe(start);
moveFocus('ArrowRight');
expect(document.activeElement).toBe(end);
expect(document.activeElement).toHaveTextContent('button1 Foo');
});
});
});
describe('ArrowLeft', function () {
it('should not move focus if no focusables present', async function () {
let tree = renderList();
let start = getRow(tree, 'Foo');
await user.tab();
moveFocus('ArrowLeft');
expect(document.activeElement).toBe(start);
});
describe('with cell focusables', function () {
it('should move focus to previous cell and back to row', async function () {
let tree = renderListWithFocusables();
let focusables = within(getRow(tree, 'Foo')).getAllByRole('button');
let start = getRow(tree, 'Foo');
await user.tab();
moveFocus('ArrowLeft');
expect(document.activeElement).toBe(focusables[1]);
moveFocus('ArrowLeft');
expect(document.activeElement).toBe(focusables[0]);
moveFocus('ArrowLeft');
expect(document.activeElement).toBe(start);
});
it('should move focus to next cell in RTL', async function () {
let tree = renderListWithFocusables({locale: 'ar-AE'});
// Should move from button one to button two
let start = within(getRow(tree, 'Foo')).getAllByRole('button')[0];
let end = within(getRow(tree, 'Foo')).getAllByRole('button')[1];
// Need to press to set a modality, otherwise useSelectableCollection will think this is a tab operation
await user.click(start);
expect(document.activeElement).toHaveTextContent('button1 Foo');
expect(document.activeElement).toBe(start);
moveFocus('ArrowLeft');
expect(document.activeElement).toBe(end);
expect(document.activeElement).toHaveTextContent('button2 Foo');
});
});
});
describe('ArrowUp', function () {
it('should not wrap focus', async function () {
let tree = renderListWithFocusables();
let start = getRow(tree, 'Foo');
await user.tab();
moveFocus('ArrowUp');
expect(document.activeElement).toBe(start);
});
it('should move focus to above row', async function () {
let tree = renderListWithFocusables({selectionMode: 'single'});
let start = getRow(tree, 'Bar');
let end = getRow(tree, 'Foo');
await user.click(start);
moveFocus('ArrowUp');
expect(document.activeElement).toBe(end);
});
it('should skip disabled rows', async function () {
let tree = renderListWithFocusables({disabledKeys: ['bar'], selectionMode: 'single'});
let start = getRow(tree, 'Baz');
let end = getRow(tree, 'Foo');
await user.click(start);
moveFocus('ArrowUp');
expect(document.activeElement).toBe(end);
});
it('should allow focus on disabled rows with disabledBehavior="selection"', async function () {
let tree = renderListWithFocusables({disabledKeys: ['foo'], disabledBehavior: 'selection', selectionMode: 'single'});
let start = getRow(tree, 'Bar');
let end = getRow(tree, 'Foo');
await user.click(start);
moveFocus('ArrowUp');
expect(document.activeElement).toBe(end);
});
});
describe('ArrowDown', function () {
it('should not wrap focus', async function () {
let tree = renderListWithFocusables({selectionMode: 'single'});
let start = getRow(tree, 'Baz');
await user.click(start);
moveFocus('ArrowDown');
expect(document.activeElement).toBe(start);
});
it('should move focus to below row', async function () {
let tree = renderListWithFocusables({selectionMode: 'single'});
let start = getRow(tree, 'Foo');
let end = getRow(tree, 'Bar');
await user.click(start);
moveFocus('ArrowDown');
expect(document.activeElement).toBe(end);
});
it('should skip disabled rows', async function () {
let tree = renderListWithFocusables({disabledKeys: ['bar'], selectionMode: 'single'});
let start = getRow(tree, 'Foo');
let end = getRow(tree, 'Baz');
await user.click(start);
moveFocus('ArrowDown');
expect(document.activeElement).toBe(end);
});
it('should allow focus on disabled rows with disabledBehavior="selection"', async function () {
let tree = renderListWithFocusables({disabledKeys: ['bar'], disabledBehavior: 'selection', selectionMode: 'single'});
let start = getRow(tree, 'Foo');
let end = getRow(tree, 'Bar');
await user.click(start);
moveFocus('ArrowDown');
expect(document.activeElement).toBe(end);
});
});
describe('PageUp', function () {
it('should move focus to a row a page above when focus starts on a row', async function () {
let tree = renderListWithFocusables({items: manyItems, selectionMode: 'single'});
let start = getRow(tree, 'Foo 25');
await user.click(start);
moveFocus('PageUp');
expect(document.activeElement).toBe(getRow(tree, 'Foo 1'));
});
it('should move focus to a row a page above when focus starts in the row cell', async function () {
let tree = renderListWithFocusables({items: manyItems});
let focusables = within(getRow(tree, 'Foo 25')).getAllByRole('button');
let start = focusables[0];
await user.click(start);
expect(document.activeElement).toBe(start);
moveFocus('PageUp');
expect(document.activeElement).toBe(getRow(tree, 'Foo 1'));
});
});
describe('PageDown', function () {
it('should move focus to a row a page below when focus starts on a row', async function () {
let tree = renderListWithFocusables({items: manyItems, selectionMode: 'single'});
tree.getByRole('grid').style.overflow = 'auto'; // make ListKeyboardDelegate know we are scrollable
await user.tab();
moveFocus('PageDown');
expect(document.activeElement).toBe(getRow(tree, 'Foo 25'));
moveFocus('PageDown');
expect(document.activeElement).toBe(getRow(tree, 'Foo 49'));
});
it('should move focus to a row a page below when focus starts in the row cell', async function () {
let tree = renderListWithFocusables({items: manyItems});
tree.getByRole('grid').style.overflow = 'auto'; // make ListKeyboardDelegate know we are scrollable
let focusables = within(getRow(tree, 'Foo 1')).getAllByRole('button');
let start = focusables[0];
await user.click(start);
expect(document.activeElement).toBe(start);
moveFocus('PageDown');
expect(document.activeElement).toBe(getRow(tree, 'Foo 25'));
moveFocus('PageDown');
expect(document.activeElement).toBe(getRow(tree, 'Foo 49'));
});
});
describe('Home', function () {
it('should move focus to the first row when focus starts on a row', async function () {
let tree = renderListWithFocusables({items: manyItems, selectionMode: 'single'});
let start = getRow(tree, 'Foo 15');
await user.click(start);
moveFocus('Home');
expect(document.activeElement).toBe(getRow(tree, 'Foo 1'));
});
it('should move focus to the first row when focus starts in the row cell', async function () {
let tree = renderListWithFocusables({items: manyItems});
let focusables = within(getRow(tree, 'Foo 15')).getAllByRole('button');
let start = focusables[0];
await user.click(start);
expect(document.activeElement).toBe(start);
moveFocus('Home');
expect(document.activeElement).toBe(getRow(tree, 'Foo 1'));
});
});
describe('End', function () {
it('should move focus to the last row when focus starts on a row', async function () {
let tree = renderListWithFocusables({items: manyItems});
await user.tab();
moveFocus('End');
expect(document.activeElement).toBe(getRow(tree, 'Foo 100'));
});
it('should move focus to the last row when focus starts in the row cell', async function () {
let tree = renderListWithFocusables({items: manyItems});
let focusables = within(getRow(tree, 'Foo 1')).getAllByRole('button');
let start = focusables[0];
await user.click(start);
expect(document.activeElement).toBe(start);
moveFocus('End');
expect(document.activeElement).toBe(getRow(tree, 'Foo 100'));
});
});
it('should move focus to the next item that is not disabled when the focused item is removed', async () => {
let tree = render(<FocusExample />);
let rows = tree.getAllByRole('row');
act(() => rows[3].focus());
expect(document.activeElement).toBe(rows[3]);
moveFocus('ArrowRight');
expect(document.activeElement).toBe(within(rows[3]).getByRole('button'));
expect(rows[4]).toHaveAttribute('aria-disabled', 'true');
await user.click(document.activeElement);
act(() => {
jest.runAllTimers();
});
rows = tree.getAllByRole('row');
expect(document.activeElement).toBe(rows[4]);
act(() => rows[rows.length - 1].focus());
expect(document.activeElement).toBe(rows[rows.length - 1]);
moveFocus('ArrowRight');
expect(document.activeElement).toBe(within(rows[rows.length - 1]).getByRole('button'));
await user.click(document.activeElement);
act(() => {
jest.runAllTimers();
});
rows = tree.getAllByRole('row');
expect(document.activeElement).toBe(rows[rows.length - 1]);
moveFocus('ArrowRight');
expect(document.activeElement).toBe(within(rows[rows.length - 1]).getByRole('button'));
await user.click(document.activeElement);
act(() => {
jest.runAllTimers();
});
rows = tree.getAllByRole('row');
expect(document.activeElement).toBe(rows[rows.length - 1]);
moveFocus('ArrowRight');
expect(document.activeElement).toBe(within(rows[rows.length - 1]).getByRole('button'));
await user.click(document.activeElement);
act(() => {
jest.runAllTimers();
});
rows = tree.getAllByRole('row');
expect(document.activeElement).toBe(rows[rows.length - 1]);
moveFocus('ArrowRight');
expect(document.activeElement).toBe(within(rows[rows.length - 1]).getByRole('button'));
await user.click(document.activeElement);
act(() => {
jest.runAllTimers();
});
rows = tree.getAllByRole('row');
expect(document.activeElement).toBe(rows[rows.length - 1]);
moveFocus('ArrowRight');
expect(document.activeElement).toBe(within(rows[rows.length - 1]).getByRole('button'));
await user.click(document.activeElement);
act(() => {
jest.runAllTimers();
});
rows = tree.getAllByRole('row');
expect(document.activeElement).toBe(rows[rows.length - 2]);
expect(rows[rows.length - 1]).toHaveAttribute('aria-disabled', 'true');
});
});
it('should display loading affordance with proper height (isLoading)', function () {
let {getAllByRole} = render(<ListView aria-label="List" loadingState="loading">{[]}</ListView>);
let row = getAllByRole('row')[0];
expect(row.parentNode.style.height).toBe('1000px');
let progressbar = within(row).getByRole('progressbar');
expect(progressbar).toBeTruthy();
});
it('should allow you to tab to ListView body if loading (no tabbable children)', async function () {
let {getByRole} = render(<ListView aria-label="List" loadingState="loading">{[]}</ListView>);
let grid = getByRole('grid');
await user.tab();
expect(document.activeElement).toBe(grid);
});
it('should display loading affordance with proper height (isLoadingMore)', function () {
let items = [
{key: 'foo', label: 'Foo'},
{key: 'bar', label: 'Bar'},
{key: 'baz', label: 'Baz'}
];
let {getByRole} = render(
<ListView items={items} aria-label="List" loadingState="loadingMore">
{item =>
<Item textValue={item.key}>{item.label}</Item>
}
</ListView>
);
let progressbar = getByRole('progressbar');
expect(progressbar).toBeTruthy();
expect(progressbar.parentNode.parentNode.parentNode.style.height).toBe('40px');
});
it('should render empty state', async function () {
let {getByText} = render(<ListView aria-label="List" renderEmptyState={renderEmptyState} />);
await act(() => Promise.resolve()); // wait for MutationObserver in useHasTabbableChild or we get act warnings
expect(getByText('No results')).toBeTruthy();
});
it('should allow you to tab into ListView body if empty with link', async function () {
let {getByRole} = render(
<>
<ActionButton>Toggle</ActionButton>
<ListView aria-label="List" renderEmptyState={renderEmptyState}>{[]}</ListView>
</>
);
await act(() => Promise.resolve());
let toggleButton = getByRole('button');
let link = getByRole('link');
await user.tab();
expect(document.activeElement).toBe(toggleButton);
await user.tab();
expect(document.activeElement).toBe(link);
});
it('supports custom data attributes', () => {
let {getByRole} = render(
<ListView aria-label="List" data-testid="test">
<Item>Foo</Item>
<Item>Bar</Item>
</ListView>
);
let grid = getByRole('grid');
expect(grid).toHaveAttribute('data-testid', 'test');
});
it('should use item description text as aria-describedby', function () {
let {getAllByRole} = render(
<ListView aria-label="List">
<Item textValue="Label">
<Text>Label</Text>
<Text slot="description">Description</Text>
</Item>
</ListView>
);
let rows = getAllByRole('row');
let description = within(rows[0]).getByText('Description');
expect(rows[0]).toHaveAttribute('aria-labelledby', `${rows[0].id} ${description.id}`);
});
describe('selection', function () {
let items = [
{key: 'foo', label: 'Foo'},
{key: 'bar', label: 'Bar'},
{key: 'baz', label: 'Baz'}
];
let renderSelectionList = (props) => render(
<ListView items={items} aria-label="List" {...props}>
{item => (
<Item key={item.key} textValue={item.label}>
{item.label}
</Item>
)}
</ListView>
);
it('should announce the selected or deselected row', async function () {
let onSelectionChange = jest.fn();
let tree = renderSelectionList({onSelectionChange, selectionMode: 'single'});
let row = tree.getAllByRole('row')[1];
await user.click(row);
expect(announce).toHaveBeenLastCalledWith('Bar selected.');
expect(announce).toHaveBeenCalledTimes(1);
await user.click(row);
expect(announce).toHaveBeenLastCalledWith('Bar not selected.');
expect(announce).toHaveBeenCalledTimes(2);
});
it('should select an item from checkbox', async function () {
let tree = renderSelectionList({onSelectionChange, selectionMode: 'multiple'});
let row = tree.getAllByRole('row')[1];
expect(row).toHaveAttribute('aria-selected', 'false');
await user.click(within(row).getByRole('checkbox'));
checkSelection(onSelectionChange, ['bar']);
expect(row).toHaveAttribute('aria-selected', 'true');
expect(announce).toHaveBeenLastCalledWith('Bar selected.');
expect(announce).toHaveBeenCalledTimes(1);
});
it('should select a row by pressing the Space key on a row', async function () {
let tree = renderSelectionList({onSelectionChange, selectionMode: 'multiple'});
let row = tree.getAllByRole('row')[0];
await user.tab();
expect(row).toHaveAttribute('aria-selected', 'false');
await user.keyboard(' ');
checkSelection(onSelectionChange, ['foo']);
expect(row).toHaveAttribute('aria-selected', 'true');
expect(announce).toHaveBeenLastCalledWith('Foo selected.');
expect(announce).toHaveBeenCalledTimes(1);
});
it('should select a row by pressing the Enter key on a row', async function () {
let tree = renderSelectionList({onSelectionChange, selectionMode: 'multiple'});
let row = tree.getAllByRole('row')[0];
await user.tab();
expect(row).toHaveAttribute('aria-selected', 'false');
await user.keyboard('{Enter}');
checkSelection(onSelectionChange, ['foo']);
expect(row).toHaveAttribute('aria-selected', 'true');
expect(announce).toHaveBeenLastCalledWith('Foo selected.');
expect(announce).toHaveBeenCalledTimes(1);
});
it('should only allow one item to be selected in single selection', async function () {
let tree = renderSelectionList({onSelectionChange, selectionMode: 'single'});
let rows = tree.getAllByRole('row');
expect(rows[1]).toHaveAttribute('aria-selected', 'false');
await user.click(within(rows[1]).getByRole('checkbox'));
checkSelection(onSelectionChange, ['bar']);
expect(rows[1]).toHaveAttribute('aria-selected', 'true');
expect(announce).toHaveBeenLastCalledWith('Bar selected.');
expect(announce).toHaveBeenCalledTimes(1);
onSelectionChange.mockClear();
await user.click(within(rows[2]).getByRole('checkbox'));
checkSelection(onSelectionChange, ['baz']);
expect(rows[1]).toHaveAttribute('aria-selected', 'false');
expect(rows[2]).toHaveAttribute('aria-selected', 'true');
});
it('should allow multiple items to be selected in multiple selection', async function () {
let tree = renderSelectionList({onSelectionChange, selectionMode: 'multiple'});
let rows = tree.getAllByRole('row');
expect(rows[1]).toHaveAttribute('aria-selected', 'false');
await user.click(within(rows[1]).getByRole('checkbox'));
checkSelection(onSelectionChange, ['bar']);
expect(rows[1]).toHaveAttribute('aria-selected', 'true');
expect(announce).toHaveBeenLastCalledWith('Bar selected.');
expect(announce).toHaveBeenCalledTimes(1);
onSelectionChange.mockClear();
await user.click(within(rows[2]).getByRole('checkbox'));
checkSelection(onSelectionChange, ['bar', 'baz']);
expect(rows[1]).toHaveAttribute('aria-selected', 'true');
expect(rows[2]).toHaveAttribute('aria-selected', 'true');
expect(announce).toHaveBeenLastCalledWith('Baz selected. 2 items selected.');
expect(announce).toHaveBeenCalledTimes(2);
await user.click(within(rows[2]).getByRole('checkbox'));
expect(announce).toHaveBeenLastCalledWith('Baz not selected. 1 item selected.');
expect(announce).toHaveBeenCalledTimes(3);
});
it('should prevent Esc from clearing selection if escapeKeyBehavior is "none"', async function () {
let tree = renderSelectionList({onSelectionChange, selectionMode: 'multiple', escapeKeyBehavior: 'none'});
let rows = tree.getAllByRole('row');
await user.click(within(rows[1]).getByRole('checkbox'));
checkSelection(onSelectionChange, ['bar']);
onSelectionChange.mockClear();
await user.click(within(rows[2]).getByRole('checkbox'));
checkSelection(onSelectionChange, ['bar', 'baz']);
onSelectionChange.mockClear();
await user.keyboard('{Escape}');
expect(onSelectionChange).not.toHaveBeenCalled();
expect(rows[1]).toHaveAttribute('aria-selected', 'true');
expect(rows[2]).toHaveAttribute('aria-selected', 'true');
});
it('should support range selection', async function () {
let tree = renderSelectionList({onSelectionChange, selectionMode: 'multiple'});
let rows = tree.getAllByRole('row');
await user.click(rows[0]);
checkSelection(onSelectionChange, ['foo']);
onSelectionChange.mockClear();
await user.keyboard('{Shift>}');
await user.click(rows[2]);
await user.keyboard('{/Shift}');
checkSelection(onSelectionChange, ['foo', 'bar', 'baz']);
onSelectionChange.mockClear();
expect(announce).toHaveBeenLastCalledWith('3 items selected.');
expect(announce).toHaveBeenCalledTimes(2);
await user.keyboard('{Shift>}');
await user.click(rows[0], {shiftKey: true});
await user.keyboard('{/Shift}');
checkSelection(onSelectionChange, ['foo']);
expect(announce).toHaveBeenLastCalledWith('1 item selected.');
expect(announce).toHaveBeenCalledTimes(3);
});
it('should support select all and clear all via keyboard', async function () {
let tree = renderSelectionList({onSelectionChange, selectionMode: 'multiple'});
let grid = tree.getByRole('grid');
let gridListTester = testUtilUser.createTester('GridList', {root: grid});
let rows = gridListTester.rows;
await gridListTester.toggleRowSelection({row: 0});
checkSelection(onSelectionChange, ['foo']);
onSelectionChange.mockClear();
expect(announce).toHaveBeenLastCalledWith('Foo selected.');
expect(announce).toHaveBeenCalledTimes(1);
expect(gridListTester.selectedRows).toHaveLength(1);
await user.keyboard('{Control>}a{/Control}');
act(() => jest.runAllTimers());
checkSelection(onSelectionChange, 'all');
onSelectionChange.mockClear();
expect(announce).toHaveBeenLastCalledWith('All items selected.');
expect(announce).toHaveBeenCalledTimes(2);
expect(gridListTester.selectedRows).toHaveLength(3);
fireEvent.keyDown(rows[0], {key: 'Escape'});
fireEvent.keyUp(rows[0], {key: 'Escape'});
checkSelection(onSelectionChange, []);
onSelectionChange.mockClear();
expect(announce).toHaveBeenLastCalledWith('No items selected.');
expect(announce).toHaveBeenCalledTimes(3);
expect(gridListTester.selectedRows).toHaveLength(0);
});
describe('onAction', function () {
it('should trigger onAction when clicking items with the mouse', async function () {
let onSelectionChange = jest.fn();
let onAction = jest.fn();
let tree = renderSelectionList({onSelectionChange, selectionMode: 'multiple', onAction});
let gridListTester = testUtilUser.createTester('GridList', {root: tree.getByRole('grid')});
await gridListTester.triggerRowAction({row: 1});
expect(onSelectionChange).not.toHaveBeenCalled();
expect(onAction).toHaveBeenCalledTimes(1);
expect(onAction).toHaveBeenLastCalledWith('bar');
await gridListTester.toggleRowSelection({row: 1});
expect(onSelectionChange).toHaveBeenCalledTimes(1);
checkSelection(onSelectionChange, ['bar']);
onSelectionChange.mockReset();
await gridListTester.toggleRowSelection({row: 2});
expect(onSelectionChange).toHaveBeenCalledTimes(1);
checkSelection(onSelectionChange, ['bar', 'baz']);
});
it('should trigger onAction when clicking items with touch', async function () {
let onSelectionChange = jest.fn();
let onAction = jest.fn();
let tree = renderSelectionList({onSelectionChange, selectionMode: 'multiple', onAction});
let gridListTester = testUtilUser.createTester('GridList', {root: tree.getByRole('grid')});
await gridListTester.triggerRowAction({row: 1});
expect(onSelectionChange).not.toHaveBeenCalled();
expect(onAction).toHaveBeenCalledTimes(1);
expect(onAction).toHaveBeenLastCalledWith('bar');
await gridListTester.toggleRowSelection({row: 1});
expect(onSelectionChange).toHaveBeenCalledTimes(1);
checkSelection(onSelectionChange, ['bar']);
onSelectionChange.mockReset();
gridListTester.setInteractionType('touch');
await gridListTester.toggleRowSelection({row: 2});
expect(onSelectionChange).toHaveBeenCalledTimes(1);
checkSelection(onSelectionChange, ['bar', 'baz']);
});
describe('still needs pointer events install', function () {
installPointerEvent();
it('should support long press to enter selection mode on touch', async function () {
let onSelectionChange = jest.fn();
let onAction = jest.fn();
let tree = renderSelectionList({onSelectionChange, selectionMode: 'multiple', onAction});
await user.click(document.body);
let rows = tree.getAllByRole('row');
fireEvent.pointerDown(rows[1], {pointerType: 'touch'});
expect(onSelectionChange).not.toHaveBeenCalled();
expect(onAction).not.toHaveBeenCalled();
act(() => jest.advanceTimersByTime(800));
checkSelection(onSelectionChange, ['bar']);
expect(onAction).not.toHaveBeenCalled();
fireEvent.pointerUp(rows[1], {pointerType: 'touch'});
onSelectionChange.mockReset();
triggerTouch(rows[2]);
checkSelection(onSelectionChange, ['bar', 'baz']);
// Deselect all to exit selection mode
triggerTouch(rows[2]);
onSelectionChange.mockReset();
triggerTouch(rows[1]);
act(() => jest.runAllTimers());
checkSelection(onSelectionChange, []);
expect(onAction).not.toHaveBeenCalled();
});
});
it('should trigger onAction when pressing Enter', async function () {
let onSelectionChange = jest.fn();
let onAction = jest.fn();
let tree = renderSelectionList({onSelectionChange, selectionMode: 'multiple', onAction});
let gridListTester = testUtilUser.createTester('GridList', {root: tree.getByRole('grid')});
gridListTester.setInteractionType('keyboard');
await gridListTester.triggerRowAction({row: 1});
expect(onSelectionChange).not.toHaveBeenCalled();
expect(onAction).toHaveBeenCalledTimes(1);
expect(onAction).toHaveBeenLastCalledWith('bar');
onAction.mockReset();
await gridListTester.toggleRowSelection({row: 2});
expect(onSelectionChange).toHaveBeenCalledTimes(1);
expect(onAction).not.toHaveBeenCalled();
checkSelection(onSelectionChange, ['baz']);
});
it('should not trigger action when deselecting with mouse', async function () {
let onSelectionChange = jest.fn();
let onAction = jest.fn();
let tree = renderSelectionList({onSelectionChange, selectionMode: 'multiple', onAction, defaultSelectedKeys: ['foo']});
let rows = tree.getAllByRole('row');
await user.click(rows[0]);
expect(onSelectionChange).toHaveBeenCalledTimes(1);
expect(onAction).not.toHaveBeenCalled();