-
Notifications
You must be signed in to change notification settings - Fork 5
/
lrud.test.js
810 lines (637 loc) · 33.1 KB
/
lrud.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
const puppeteer = require('puppeteer');
const server = require('./server');
const testPath = `${server.address}/test/layouts`;
describe('LRUD spatial', () => {
let browser;
let page;
let context;
const getParentContainerDataFocus = (id) => page.evaluate((id) => document.getElementById(id).parentElement.getAttribute('data-focus'), id);
beforeAll(async () => {
try {
await server.listen();
} catch (ex) {
if (ex.code !== 'EADDRINUSE') {
throw ex;
}
}
browser = await puppeteer.launch({
defaultViewport: {width: 1280, height: 800}
});
context = await browser.createIncognitoBrowserContext();
});
beforeEach(async () => {
page = await context.newPage();
});
afterEach(async () => {
await page?.close();
});
afterAll(async () => {
await context?.close();
await browser?.close();
try {
await server.close();
} catch { /* empty */ }
});
describe('Initialising', () => {
it('should focus on the first candidate by default', async () => {
await page.goto(`${testPath}/1c-4f.html`);
await page.waitForFunction('document.activeElement');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-1');
});
});
describe('Page with one container and four candidates', () => {
it('should focus on the second item on a right arrow press', async () => {
await page.goto(`${testPath}/1c-4f.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-2');
});
it('should focus on the third item on a down arrow press', async () => {
await page.goto(`${testPath}/1c-4f.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-3');
});
it('should focus on the fourth item on a right, down arrow press', async () => {
await page.goto(`${testPath}/1c-4f.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-4');
});
it('should focus on the first item on a right, down, left, up arrow press', async () => {
await page.goto(`${testPath}/1c-4f.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowLeft');
await page.keyboard.press('ArrowUp');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-1');
});
it('should remain focused on the first item on a left arrow press', async () => {
await page.goto(`${testPath}/1c-4f.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowLeft');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-1');
});
it('should remain focused on the second item on a right, right arrow press', async () => {
await page.goto(`${testPath}/1c-4f.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowRight');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-2');
});
});
describe('Page with two vertical containers and four candidates', () => {
it('should focus on the third item on a down arrow press', async () => {
await page.goto(`${testPath}/2c-v-4f.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-3');
});
it('should focus on the first item on a down, up arrow press', async () => {
await page.goto(`${testPath}/2c-v-4f.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowUp');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-1');
});
it('should return to the last active child on a down, right, up arrow press', async () => {
await page.goto(`${testPath}/2c-v-4f.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowUp');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-1');
});
it('should return to the last active child on a right, down, left, up arrow press', async () => {
await page.goto(`${testPath}/2c-v-4f.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowLeft');
await page.keyboard.press('ArrowUp');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-2');
});
it('should remain focused on the first item on a left arrow press', async () => {
await page.goto(`${testPath}/2c-v-4f.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowLeft');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-1');
});
it('should remain focused on the second item on a right, right arrow press', async () => {
await page.goto(`${testPath}/2c-v-4f.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowRight');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-2');
});
});
describe('Page with two vertical containers and four candidates but no ids', () => {
it('should focus on the first item on a down, up arrow press', async () => {
await page.goto(`${testPath}/2c-v-4f-no-ids.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowUp');
const result = await page.evaluate(() => document.activeElement.classList);
expect(result).toEqual({'0': 'first-item', '1': 'item'});
});
it('should focus on the first item on a down, right, up arrow press', async () => {
await page.goto(`${testPath}/2c-v-4f-no-ids.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowUp');
const result = await page.evaluate(() => document.activeElement.classList);
expect(result).toEqual({'0': 'first-item', '1': 'item'});
});
it('should focus on the first item on a right, down, left, up arrow press', async () => {
await page.goto(`${testPath}/2c-v-4f-no-ids.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowLeft');
await page.keyboard.press('ArrowUp');
const result = await page.evaluate(() => document.activeElement.classList);
expect(result).toEqual({'0': 'first-item', '1': 'item'});
});
});
describe('Page with two horizontal containers and four candidates', () => {
it('should remain focused on the first item on a down arrow press', async () => {
await page.goto(`${testPath}/2c-h-4f.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-1');
});
it('should focus on the third item on a right, right arrow press', async () => {
await page.goto(`${testPath}/2c-h-4f.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowRight');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-3');
});
it('should focus on the second item on a right, right, left arrow press', async () => {
await page.goto(`${testPath}/2c-h-4f.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowLeft');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-2');
});
});
describe('Invisible elements', () => {
/*
* Elements that are `display: none` or `visibility: hidden` have their
* width, height, and coordinates set to 0. These should not be considered focusable candidates.
*
* Elements with the `lrud-ignore` class, or inside a parent with the `lrud-ignore` class, should
* not be considered focusable candidates
*
*/
it('should ignore hidden items as possible candidates and move past them', async () => {
await page.goto(`${testPath}/hidden.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowRight');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-6');
});
it('should ignore visible items inside ignored containers', async () => {
await page.goto(`${testPath}/hidden.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-9');
});
});
describe('Unreachable foucsable elements', () => {
/*
* Elements with a tabindex of -1 should not be considered focusable candidates.
* Elements inside a parent with tabindex -1 should be considered focusable candidates.
*
*/
it('should ignore tabindex -1 items as possible candidates and move past them', async () => {
await page.goto(`${testPath}/unreachable.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowRight');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-4');
});
it('should not ignore visible items inside tabindex -1 containers', async () => {
await page.goto(`${testPath}/unreachable.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-6');
await page.keyboard.press('ArrowDown');
const nextResult = await page.evaluate(() => document.activeElement.id);
expect(nextResult).toEqual('item-7');
});
});
describe('Page with 11 candidates, with varying sizes', () => {
it('should focus on candidate 6 when right, down is pressed', async () => {
await page.goto(`${testPath}/0c-v-11f-size.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-6');
});
it('should focus on candidate 6 when right, right, down is pressed', async () => {
await page.goto(`${testPath}/0c-v-11f-size.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-6');
});
it('should focus on candidate 11 when right, right, down, down is pressed', async () => {
await page.goto(`${testPath}/0c-v-11f-size.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-11');
});
it('should focus on candidate 5 when right, down, down, left, left up is pressed', async () => {
await page.goto(`${testPath}/0c-v-11f-size.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowLeft');
await page.keyboard.press('ArrowLeft');
await page.keyboard.press('ArrowUp');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-5');
});
it('should focus on candidate 14 when right, right, right down, down is pressed', async () => {
await page.goto(`${testPath}/0c-v-11f-size.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-14');
});
it('should focus on candidate 7 when right, right, right down, down, up is pressed', async () => {
await page.goto(`${testPath}/0c-v-11f-size.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowUp');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-7');
});
});
describe('Page with 3 candidates with equal distances to matching corners', () => {
it('should focus on candidate 1 when down, right, up is pressed', async () => {
await page.goto(`${testPath}/0c-3f-distance.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowUp');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-1');
});
});
describe('Page with two containers with blocked exit directions', () => {
it('should prevent focus moving to candidate 3 when down is pressed', async () => {
await page.goto(`${testPath}/3c-h-6f-blocked-exit.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-2');
});
it('should prevent focus moving to candidate 2 when up is pressed', async () => {
await page.goto(`${testPath}/3c-h-6f-blocked-exit.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowUp');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-3');
});
it('should allow container exit for non blocked directions', async () => {
await page.goto(`${testPath}/3c-h-6f-blocked-exit.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-5');
});
it('should move to the second best candidate if first was blocked', async () => {
await page.goto(`${testPath}/3c-h-6f-blocked-exit.html`);
await page.evaluate(() => document.getElementById('item-6').focus());
await page.keyboard.press('ArrowUp');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-1');
});
});
describe('Page with nested containers', () => {
it('should allow movement into a nested container', async () => {
await page.goto(`${testPath}/4c-v-5f-nested.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-2');
});
it('should allow movement out of nested container', async () => {
await page.goto(`${testPath}/4c-v-5f-nested.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-3');
});
it('should apply data-block-exit rules', async () => {
await page.goto(`${testPath}/4c-v-5f-nested.html`);
await page.waitForFunction('document.activeElement');
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-3');
});
it('should apply data-block-exit rules in nested container', async () => {
await page.goto(`${testPath}/4c-v-5f-nested.html`);
await page.waitForFunction('document.activeElement');
await page.evaluate(() => document.getElementById('item-6').focus());
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-6');
});
it('should not apply data-block-exit rules to candidates without containers', async () => {
await page.goto(`${testPath}/4c-v-5f-nested.html`);
await page.waitForFunction('document.activeElement');
await page.evaluate(() => document.getElementById('item-8').focus());
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-9');
});
});
describe('Page with small or close items', () => {
beforeEach(async () => {
await page.goto(`${testPath}/tiled-items.html`);
await page.waitForFunction('document.activeElement');
});
it('should not skip items with far away corners', async () => {
await page.evaluate(() => document.getElementById('item-48').focus());
await page.keyboard.press('ArrowUp');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-41');
await page.keyboard.press('ArrowDown');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-48');
});
it('should go to the most central item when there are multiple below the exit edge', async () => {
await page.keyboard.press('ArrowRight');
await page.keyboard.press('ArrowRight');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-3');
await page.keyboard.press('ArrowDown');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-11');
});
});
describe('Candidate overlap', () => {
beforeEach(async () => {
await page.goto(`${testPath}/tiled-items.html`);
await page.waitForFunction('document.activeElement');
});
it('should consider items in the direction you want to move if they are fully in that direction with no overlap', async () => {
await page.evaluate(() => document.getElementById('item-36').focus());
await page.keyboard.press('ArrowRight');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-44');
});
it('should consider items in the direction you want to move if they overlap by up to 30%', async () => {
await page.evaluate(() => document.getElementById('item-22').focus());
await page.keyboard.press('ArrowRight');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-15');
});
it('should not consider items in the direction you want to move if they overlap by more than 30%', async () => {
await page.evaluate(() => document.getElementById('item-29').focus());
await page.keyboard.press('ArrowRight');
expect(await page.evaluate(() => document.activeElement.id)).not.toEqual('item-15');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-8');
});
it('should consider items with a customised overlap value', async () => {
await page.evaluate(() => document.getElementById('item-44').setAttribute('data-lrud-overlap-threshold', 0.6));
await page.evaluate(() => document.getElementById('item-29').focus());
await page.keyboard.press('ArrowRight');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-44');
await page.evaluate(() => document.getElementById('item-15').setAttribute('data-lrud-overlap-threshold', 0));
await page.evaluate(() => document.getElementById('item-22').focus());
await page.keyboard.press('ArrowRight');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-8');
});
});
describe('Scope', () => {
beforeEach(async () => {
await page.goto(`${testPath}/2c-v-4f.html`);
await page.waitForFunction('document.activeElement');
});
it('should not focus on elements outside of the provided scope', async () => {
await page.evaluate(() => window.setScope(document.querySelector('section')));
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-1');
await page.keyboard.press('ArrowDown');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-1');
await page.keyboard.press('ArrowRight');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-2');
await page.keyboard.press('ArrowDown');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-2');
});
it('moves inside the scoped area if current focus is outside', async () => {
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-1');
await page.keyboard.press('ArrowDown');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-3');
await page.evaluate(() => window.setScope(document.querySelector('section')));
await page.keyboard.press('ArrowRight');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-1');
});
it('should ignore an invalid scope', async () => {
await page.evaluate(() => window.setScope(document.getElementById('doesnt-exist')));
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-1');
await page.keyboard.press('ArrowDown');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-3');
});
});
describe('Prioritised Containers', () => {
describe('Prioritised', () => {
it('should select a candidate within the same container over a closer external candidate', async () => {
await page.goto(`${testPath}/container-with-empty-space.html`);
await page.evaluate(() => document.getElementById('item-4').focus());
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-5');
});
it('moves inside the scoped area if current focus is outside, and ignore prioritised siblings', async () => {
await page.goto(`${testPath}/container-with-empty-space.html`);
await page.evaluate(() => document.getElementById('item-1').focus());
await page.evaluate(() => window.setScope(document.getElementById('section-3')));
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-7');
});
});
describe('Non Prioritised', () => {
beforeEach(async () => {
await page.goto(`${testPath}/container-with-empty-space.html`);
await page.waitForFunction('document.activeElement');
await page.evaluate(() => document.getElementById('section-1').setAttribute('data-lrud-prioritise-children', false));
await page.evaluate(() => document.getElementById('section-3').setAttribute('data-lrud-prioritise-children', false));
});
it('should select the closest candidate regardless of positions of sibling candidates', async () => {
await page.evaluate(() => document.getElementById('item-4').focus());
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-7');
});
it('moves inside the scoped area if current focus is outside', async () => {
await page.evaluate(() => document.getElementById('item-1').focus());
await page.evaluate(() => window.setScope(document.getElementById('section-3')));
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-7');
});
});
});
describe('Focusable containers', () => {
it('should move into a focusable container that is nearer than any other focusable candidates', async () => {
await page.goto(`${testPath}/focusable-container-with-empty-space.html`);
await page.waitForFunction('document.activeElement');
await page.evaluate(() => document.getElementById('item-4').focus());
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-6');
});
it('should not move into a focusable container that has no focusable children', async () => {
await page.goto(`${testPath}/focusable-container-with-empty-space.html`);
await page.evaluate(() => document.getElementById('item-6').remove());
await page.evaluate(() => document.getElementById('item-4').focus());
await page.keyboard.press('ArrowDown');
const result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-7');
});
it('should not set data-focus attributes to a container ID', async () => {
await page.goto(`${testPath}/focusable-container-with-empty-space.html`);
// makes section-2 the best candidate, although it will later be ignored
await page.evaluate(() => document.getElementById('section-2').setAttribute('data-lrud-overlap-threshold', 1));
await page.evaluate(() => document.getElementById('item-6').focus());
await page.keyboard.press('ArrowLeft');
const result = await page.evaluate(() => document.activeElement.id);
expect(await page.evaluate(() => document.getElementById('section-2').getAttribute('data-focus'))).toBe('item-6');
expect(result).toEqual('item-6');
});
it('should remember the last active child of a focusable container', async () => {
await page.goto(`${testPath}/nested-focusable-containers.html`);
await page.waitForFunction('document.activeElement');
await page.evaluate(() => document.getElementById('item-3').focus());
await page.keyboard.press('ArrowRight');
let result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-4');
await page.evaluate(() => document.getElementById('item-9').focus());
await page.keyboard.press('ArrowUp');
result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-4');
await page.evaluate(() => document.getElementById('item-5').focus());
await page.keyboard.press('ArrowUp');
result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-1');
await page.keyboard.press('ArrowDown');
result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-5');
});
it('should prioritise focusable containers that are the same distance away as a regular focusable', async () => {
await page.goto(`${testPath}/nested-focusable-containers.html`);
await page.waitForFunction('document.activeElement');
await page.evaluate(() => document.getElementById('item-11').focus());
await page.keyboard.press('ArrowDown');
let result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-12');
await page.evaluate(() => document.getElementById('item-9').focus());
await page.keyboard.press('ArrowDown');
result = await page.evaluate(() => document.activeElement.id);
expect(result).toEqual('item-12');
});
});
describe('Last active child', () => {
beforeEach(async () => {
await page.goto(`${testPath}/2c-v-11f-size.html`);
await page.waitForFunction('document.activeElement');
});
it('should return to the last active child when re-entering a container', async () => {
await page.evaluate(() => document.getElementById('item-7').focus());
await page.keyboard.press('ArrowDown');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-8');
await page.keyboard.press('ArrowUp');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-7');
});
it('should focus the containers first child if the last active child no longer exists', async () => {
await page.evaluate(() => document.getElementById('item-7').focus());
await page.keyboard.press('ArrowDown');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-8');
await page.evaluate(() => document.getElementById('item-7').remove());
await page.keyboard.press('ArrowUp');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-1');
});
it('should still remember last active child if focus is manually moved from a container', async () => {
await page.evaluate(() => document.getElementById('item-1').focus());
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowRight');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-6');
await page.evaluate(() => document.getElementById('item-8').focus());
await page.keyboard.press('ArrowUp');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-6');
});
it('should only store the last active child ID if the child is not inside another container', async () => {
await page.goto(`${testPath}/4c-v-5f-nested.html`);
await page.evaluate(() => document.getElementById('item-1').focus());
await page.keyboard.press('ArrowDown');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-2');
expect(await getParentContainerDataFocus('item-1')).toEqual('item-1');
expect(await getParentContainerDataFocus('item-2')).toEqual('item-2');
await page.keyboard.press('ArrowDown');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-3');
expect(await getParentContainerDataFocus('item-1')).toEqual('item-3');
expect(await getParentContainerDataFocus('item-2')).toEqual('item-2');
await page.keyboard.press('ArrowUp');
expect(await getParentContainerDataFocus('item-1')).toEqual('item-3');
expect(await getParentContainerDataFocus('item-2')).toEqual('item-2');
await page.keyboard.press('ArrowUp');
expect(await getParentContainerDataFocus('item-1')).toEqual('item-1');
expect(await getParentContainerDataFocus('item-2')).toEqual('item-2');
});
it('does not update the new active child ID if the exit was blocked', async () => {
await page.goto(`${testPath}/3c-h-6f-blocked-exit.html`);
await page.evaluate(() => document.getElementById('item-2').focus());
await page.keyboard.press('ArrowDown');
expect(await getParentContainerDataFocus('item-2')).toEqual('item-2');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-2');
expect(await getParentContainerDataFocus('item-3')).toEqual(null);
});
});
});