-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
list-key-manager.spec.ts
565 lines (428 loc) · 21.6 KB
/
list-key-manager.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
import {QueryList} from '@angular/core';
import {fakeAsync, tick} from '@angular/core/testing';
import {FocusKeyManager} from './focus-key-manager';
import {DOWN_ARROW, UP_ARROW, TAB} from '../keycodes/keycodes';
import {ListKeyManager} from './list-key-manager';
import {ActiveDescendantKeyManager} from './activedescendant-key-manager';
import {createKeyboardEvent} from '../testing/event-objects';
import {first} from '../rxjs/index';
class FakeFocusable {
constructor(private _label = '') { }
disabled = false;
focus() {}
getLabel() { return this._label; }
}
class FakeHighlightable {
disabled = false;
setActiveStyles() {}
setInactiveStyles() {}
}
class FakeQueryList<T> extends QueryList<T> {
items: T[];
get length() { return this.items.length; }
get first() { return this.items[0]; }
toArray() { return this.items; }
some() { return this.items.some.apply(this.items, arguments); }
}
describe('Key managers', () => {
let itemList: FakeQueryList<any>;
let fakeKeyEvents: {
downArrow: KeyboardEvent,
upArrow: KeyboardEvent,
tab: KeyboardEvent,
unsupported: KeyboardEvent
};
beforeEach(() => {
itemList = new FakeQueryList<any>();
fakeKeyEvents = {
downArrow: createKeyboardEvent('keydown', DOWN_ARROW),
upArrow: createKeyboardEvent('keydown', UP_ARROW),
tab: createKeyboardEvent('keydown', TAB),
unsupported: createKeyboardEvent('keydown', 192) // corresponds to the tilde character (~)
};
});
describe('ListKeyManager', () => {
let keyManager: ListKeyManager<FakeFocusable>;
beforeEach(() => {
itemList.items = [
new FakeFocusable('one'),
new FakeFocusable('two'),
new FakeFocusable('three')
];
keyManager = new ListKeyManager<FakeFocusable>(itemList);
// first item is already focused
keyManager.setFirstItemActive();
spyOn(keyManager, 'setActiveItem').and.callThrough();
});
describe('Key events', () => {
it('should set subsequent items as active when down arrow is pressed', () => {
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex)
.toBe(1, 'Expected active item to be 1 after 1 down arrow event.');
expect(keyManager.setActiveItem).not.toHaveBeenCalledWith(0);
expect(keyManager.setActiveItem).toHaveBeenCalledWith(1);
expect(keyManager.setActiveItem).not.toHaveBeenCalledWith(2);
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex)
.toBe(2, 'Expected active item to be 2 after 2 down arrow events.');
expect(keyManager.setActiveItem).toHaveBeenCalledWith(2);
expect(keyManager.setActiveItem).not.toHaveBeenCalledWith(0);
});
it('should set first item active when down arrow pressed if no active item', () => {
keyManager.setActiveItem(-1);
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex)
.toBe(0, 'Expected active item to be 0 after down key if active item was null.');
expect(keyManager.setActiveItem).toHaveBeenCalledWith(0);
expect(keyManager.setActiveItem).not.toHaveBeenCalledWith(1);
expect(keyManager.setActiveItem).not.toHaveBeenCalledWith(2);
});
it('should set previous items as active when up arrow is pressed', () => {
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex)
.toBe(1, 'Expected active item to be 1 after 1 down arrow event.');
expect(keyManager.setActiveItem).not.toHaveBeenCalledWith(0);
expect(keyManager.setActiveItem).toHaveBeenCalledWith(1);
keyManager.onKeydown(fakeKeyEvents.upArrow);
expect(keyManager.activeItemIndex)
.toBe(0, 'Expected active item to be 0 after 1 down and 1 up arrow event.');
expect(keyManager.setActiveItem).toHaveBeenCalledWith(0);
});
it('should do nothing when up arrow is pressed if no active item and not wrap', () => {
keyManager.setActiveItem(-1);
keyManager.onKeydown(fakeKeyEvents.upArrow);
expect(keyManager.activeItemIndex)
.toBe(-1, 'Expected nothing to happen if up arrow occurs and no active item.');
expect(keyManager.setActiveItem).not.toHaveBeenCalledWith(0);
expect(keyManager.setActiveItem).not.toHaveBeenCalledWith(1);
expect(keyManager.setActiveItem).not.toHaveBeenCalledWith(2);
});
it('should skip disabled items using arrow keys', () => {
itemList.items[1].disabled = true;
// down arrow should skip past disabled item from 0 to 2
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex)
.toBe(2, 'Expected active item to skip past disabled item on down arrow.');
expect(keyManager.setActiveItem).not.toHaveBeenCalledWith(0);
expect(keyManager.setActiveItem).not.toHaveBeenCalledWith(1);
expect(keyManager.setActiveItem).toHaveBeenCalledWith(2);
// up arrow should skip past disabled item from 2 to 0
keyManager.onKeydown(fakeKeyEvents.upArrow);
expect(keyManager.activeItemIndex)
.toBe(0, 'Expected active item to skip past disabled item on up arrow.');
expect(keyManager.setActiveItem).toHaveBeenCalledWith(0);
expect(keyManager.setActiveItem).not.toHaveBeenCalledWith(1);
});
it('should work normally when disabled property does not exist', () => {
itemList.items[0].disabled = undefined;
itemList.items[1].disabled = undefined;
itemList.items[2].disabled = undefined;
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex)
.toBe(1, 'Expected active item to be 1 after 1 down arrow when disabled not set.');
expect(keyManager.setActiveItem).not.toHaveBeenCalledWith(0);
expect(keyManager.setActiveItem).toHaveBeenCalledWith(1);
expect(keyManager.setActiveItem).not.toHaveBeenCalledWith(2);
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex)
.toBe(2, 'Expected active item to be 2 after 2 down arrows when disabled not set.');
expect(keyManager.setActiveItem).not.toHaveBeenCalledWith(0);
expect(keyManager.setActiveItem).toHaveBeenCalledWith(2);
});
it('should not move active item past either end of the list', () => {
keyManager.onKeydown(fakeKeyEvents.downArrow);
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex)
.toBe(2, `Expected last item of the list to be active.`);
// this down arrow would move active item past the end of the list
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex)
.toBe(2, `Expected active item to remain at the end of the list.`);
keyManager.onKeydown(fakeKeyEvents.upArrow);
keyManager.onKeydown(fakeKeyEvents.upArrow);
expect(keyManager.activeItemIndex)
.toBe(0, `Expected first item of the list to be active.`);
// this up arrow would move active item past the beginning of the list
keyManager.onKeydown(fakeKeyEvents.upArrow);
expect(keyManager.activeItemIndex)
.toBe(0, `Expected active item to remain at the beginning of the list.`);
});
it('should not move active item to end when the last item is disabled', () => {
itemList.items[2].disabled = true;
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex)
.toBe(1, `Expected second item of the list to be active.`);
// this down arrow would set active item to the last item, which is disabled
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex)
.toBe(1, `Expected the second item to remain active.`);
expect(keyManager.setActiveItem).not.toHaveBeenCalledWith(2);
});
it('should emit tabOut when the tab key is pressed', () => {
let spy = jasmine.createSpy('tabOut spy');
first.call(keyManager.tabOut).subscribe(spy);
keyManager.onKeydown(fakeKeyEvents.tab);
expect(spy).toHaveBeenCalled();
});
it('should prevent the default keyboard action when pressing the arrow keys', () => {
expect(fakeKeyEvents.downArrow.defaultPrevented).toBe(false);
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(fakeKeyEvents.downArrow.defaultPrevented).toBe(true);
expect(fakeKeyEvents.upArrow.defaultPrevented).toBe(false);
keyManager.onKeydown(fakeKeyEvents.upArrow);
expect(fakeKeyEvents.upArrow.defaultPrevented).toBe(true);
});
it('should not prevent the default keyboard action when pressing tab', () => {
expect(fakeKeyEvents.tab.defaultPrevented).toBe(false);
keyManager.onKeydown(fakeKeyEvents.tab);
expect(fakeKeyEvents.tab.defaultPrevented).toBe(false);
});
it('should not do anything for unsupported key presses', () => {
keyManager.setActiveItem(1);
expect(keyManager.activeItemIndex).toBe(1);
expect(fakeKeyEvents.unsupported.defaultPrevented).toBe(false);
keyManager.onKeydown(fakeKeyEvents.unsupported);
expect(keyManager.activeItemIndex).toBe(1);
expect(fakeKeyEvents.unsupported.defaultPrevented).toBe(false);
});
it('should activate the first item when pressing down on a clean key manager', () => {
keyManager = new ListKeyManager<FakeFocusable>(itemList);
expect(keyManager.activeItemIndex).toBe(-1, 'Expected active index to default to -1.');
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex).toBe(0, 'Expected first item to become active.');
});
});
describe('programmatic focus', () => {
it('should setActiveItem()', () => {
expect(keyManager.activeItemIndex)
.toBe(0, `Expected first item of the list to be active.`);
keyManager.setActiveItem(1);
expect(keyManager.activeItemIndex)
.toBe(1, `Expected activeItemIndex to be updated when setActiveItem() was called.`);
});
it('should expose the active item correctly', () => {
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex).toBe(1, 'Expected active item to be the second option.');
expect(keyManager.activeItem)
.toBe(itemList.items[1], 'Expected the active item to match the second option.');
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex).toBe(2, 'Expected active item to be the third option.');
expect(keyManager.activeItem)
.toBe(itemList.items[2], 'Expected the active item ID to match the third option.');
});
it('should setFirstItemActive()', () => {
keyManager.onKeydown(fakeKeyEvents.downArrow);
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex)
.toBe(2, `Expected last item of the list to be active.`);
keyManager.setFirstItemActive();
expect(keyManager.activeItemIndex)
.toBe(0, `Expected setFirstItemActive() to set the active item to the first item.`);
});
it('should set the active item to the second item if the first one is disabled', () => {
itemList.items[0].disabled = true;
keyManager.setFirstItemActive();
expect(keyManager.activeItemIndex)
.toBe(1, `Expected the second item to be active if the first was disabled.`);
});
it('should setLastItemActive()', () => {
expect(keyManager.activeItemIndex)
.toBe(0, `Expected first item of the list to be active.`);
keyManager.setLastItemActive();
expect(keyManager.activeItemIndex)
.toBe(2, `Expected setLastItemActive() to set the active item to the last item.`);
});
it('should set the active item to the second to last item if the last is disabled', () => {
itemList.items[2].disabled = true;
keyManager.setLastItemActive();
expect(keyManager.activeItemIndex)
.toBe(1, `Expected the second to last item to be active if the last was disabled.`);
});
it('should setNextItemActive()', () => {
expect(keyManager.activeItemIndex)
.toBe(0, `Expected first item of the list to be active.`);
keyManager.setNextItemActive();
expect(keyManager.activeItemIndex)
.toBe(1, `Expected setNextItemActive() to set the active item to the next item.`);
});
it('should set the active item to the next enabled item if next is disabled', () => {
itemList.items[1].disabled = true;
expect(keyManager.activeItemIndex)
.toBe(0, `Expected first item of the list to be active.`);
keyManager.setNextItemActive();
expect(keyManager.activeItemIndex)
.toBe(2, `Expected setNextItemActive() to only set enabled items as active.`);
});
it('should setPreviousItemActive()', () => {
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex)
.toBe(1, `Expected second item of the list to be active.`);
keyManager.setPreviousItemActive();
expect(keyManager.activeItemIndex)
.toBe(0, `Expected setPreviousItemActive() to set the active item to the previous.`);
});
it('should skip disabled items when setPreviousItemActive() is called', () => {
itemList.items[1].disabled = true;
keyManager.onKeydown(fakeKeyEvents.downArrow);
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex)
.toBe(2, `Expected third item of the list to be active.`);
keyManager.setPreviousItemActive();
expect(keyManager.activeItemIndex)
.toBe(0, `Expected setPreviousItemActive() to skip the disabled item.`);
});
});
describe('wrap mode', () => {
it('should return itself to allow chaining', () => {
expect(keyManager.withWrap())
.toEqual(keyManager, `Expected withWrap() to return an instance of ListKeyManager.`);
});
it('should wrap focus when arrow keying past items while in wrap mode', () => {
keyManager.withWrap();
keyManager.onKeydown(fakeKeyEvents.downArrow);
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex).toBe(2, 'Expected last item to be active.');
// this down arrow moves down past the end of the list
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex).toBe(0, 'Expected active item to wrap to beginning.');
// this up arrow moves up past the beginning of the list
keyManager.onKeydown(fakeKeyEvents.upArrow);
expect(keyManager.activeItemIndex).toBe(2, 'Expected active item to wrap to end.');
});
it('should set last item active when up arrow is pressed if no active item', () => {
keyManager.withWrap();
keyManager.setActiveItem(-1);
keyManager.onKeydown(fakeKeyEvents.upArrow);
expect(keyManager.activeItemIndex)
.toBe(2, 'Expected last item to be active on up arrow if no active item.');
expect(keyManager.setActiveItem).not.toHaveBeenCalledWith(0);
expect(keyManager.setActiveItem).toHaveBeenCalledWith(2);
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(keyManager.activeItemIndex)
.toBe(0, 'Expected active item to be 0 after wrapping back to beginning.');
expect(keyManager.setActiveItem).toHaveBeenCalledWith(0);
});
});
describe('typeahead mode', () => {
const debounceInterval = 300;
beforeEach(() => {
keyManager.withTypeAhead(debounceInterval);
keyManager.setActiveItem(-1);
});
it('should throw if the items do not implement the getLabel method', () => {
const invalidQueryList = new FakeQueryList();
invalidQueryList.items = [{ disabled: false }];
const invalidManager = new ListKeyManager(invalidQueryList);
expect(() => invalidManager.withTypeAhead()).toThrowError(/must implement/);
});
it('should debounce the input key presses', fakeAsync(() => {
keyManager.onKeydown(createKeyboardEvent('keydown', 79)); // types "o"
keyManager.onKeydown(createKeyboardEvent('keydown', 78)); // types "n"
keyManager.onKeydown(createKeyboardEvent('keydown', 69)); // types "e"
expect(keyManager.activeItem).not.toBe(itemList.items[0]);
tick(debounceInterval);
expect(keyManager.activeItem).toBe(itemList.items[0]);
}));
it('should focus the first item that starts with a letter', fakeAsync(() => {
keyManager.onKeydown(createKeyboardEvent('keydown', 84)); // types "t"
tick(debounceInterval);
expect(keyManager.activeItem).toBe(itemList.items[1]);
}));
it('should focus the first item that starts with sequence of letters', fakeAsync(() => {
keyManager.onKeydown(createKeyboardEvent('keydown', 84)); // types "t"
keyManager.onKeydown(createKeyboardEvent('keydown', 72)); // types "h"
tick(debounceInterval);
expect(keyManager.activeItem).toBe(itemList.items[2]);
}));
it('should cancel any pending timers if a navigation key is pressed', fakeAsync(() => {
keyManager.onKeydown(createKeyboardEvent('keydown', 84)); // types "t"
keyManager.onKeydown(createKeyboardEvent('keydown', 72)); // types "h"
keyManager.onKeydown(fakeKeyEvents.downArrow);
tick(debounceInterval);
expect(keyManager.activeItem).toBe(itemList.items[0]);
}));
});
});
describe('FocusKeyManager', () => {
let keyManager: FocusKeyManager<FakeFocusable>;
beforeEach(() => {
itemList.items = [new FakeFocusable(), new FakeFocusable(), new FakeFocusable()];
keyManager = new FocusKeyManager<FakeFocusable>(itemList);
// first item is already focused
keyManager.setFirstItemActive();
spyOn(itemList.items[0], 'focus');
spyOn(itemList.items[1], 'focus');
spyOn(itemList.items[2], 'focus');
});
it('should focus subsequent items when down arrow is pressed', () => {
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(itemList.items[0].focus).not.toHaveBeenCalled();
expect(itemList.items[1].focus).toHaveBeenCalledTimes(1);
expect(itemList.items[2].focus).not.toHaveBeenCalled();
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(itemList.items[0].focus).not.toHaveBeenCalled();
expect(itemList.items[1].focus).toHaveBeenCalledTimes(1);
expect(itemList.items[2].focus).toHaveBeenCalledTimes(1);
});
it('should focus previous items when up arrow is pressed', () => {
keyManager.onKeydown(fakeKeyEvents.downArrow);
expect(itemList.items[0].focus).not.toHaveBeenCalled();
expect(itemList.items[1].focus).toHaveBeenCalledTimes(1);
keyManager.onKeydown(fakeKeyEvents.upArrow);
expect(itemList.items[0].focus).toHaveBeenCalledTimes(1);
expect(itemList.items[1].focus).toHaveBeenCalledTimes(1);
});
it('should allow setting the focused item without calling focus', () => {
expect(keyManager.activeItemIndex)
.toBe(0, `Expected first item of the list to be active.`);
keyManager.updateActiveItemIndex(1);
expect(keyManager.activeItemIndex)
.toBe(1, `Expected activeItemIndex to update after calling updateActiveItemIndex().`);
expect(itemList.items[1].focus).not.toHaveBeenCalledTimes(1);
});
});
describe('ActiveDescendantKeyManager', () => {
let keyManager: ActiveDescendantKeyManager<FakeHighlightable>;
beforeEach(fakeAsync(() => {
itemList.items = [new FakeHighlightable(), new FakeHighlightable(), new FakeHighlightable()];
keyManager = new ActiveDescendantKeyManager<FakeHighlightable>(itemList);
// first item is already focused
keyManager.setFirstItemActive();
tick();
spyOn(itemList.items[0], 'setActiveStyles');
spyOn(itemList.items[1], 'setActiveStyles');
spyOn(itemList.items[2], 'setActiveStyles');
spyOn(itemList.items[0], 'setInactiveStyles');
spyOn(itemList.items[1], 'setInactiveStyles');
spyOn(itemList.items[2], 'setInactiveStyles');
}));
it('should set subsequent items as active with the DOWN arrow', fakeAsync(() => {
keyManager.onKeydown(fakeKeyEvents.downArrow);
tick();
expect(itemList.items[1].setActiveStyles).toHaveBeenCalled();
expect(itemList.items[2].setActiveStyles).not.toHaveBeenCalled();
keyManager.onKeydown(fakeKeyEvents.downArrow);
tick();
expect(itemList.items[2].setActiveStyles).toHaveBeenCalled();
}));
it('should set previous items as active with the UP arrow', fakeAsync(() => {
keyManager.setLastItemActive();
tick();
keyManager.onKeydown(fakeKeyEvents.upArrow);
tick();
expect(itemList.items[1].setActiveStyles).toHaveBeenCalled();
expect(itemList.items[0].setActiveStyles).not.toHaveBeenCalled();
keyManager.onKeydown(fakeKeyEvents.upArrow);
tick();
expect(itemList.items[0].setActiveStyles).toHaveBeenCalled();
}));
it('should set inactive styles on previously active items', fakeAsync(() => {
keyManager.onKeydown(fakeKeyEvents.downArrow);
tick();
expect(itemList.items[0].setInactiveStyles).toHaveBeenCalled();
keyManager.onKeydown(fakeKeyEvents.upArrow);
tick();
expect(itemList.items[1].setInactiveStyles).toHaveBeenCalled();
}));
});
});