|
| 1 | +import {QueryList} from '@angular/core'; |
| 2 | +import {ListKeyManager, MdFocusable} from './list-key-manager'; |
| 3 | +import {DOWN_ARROW, UP_ARROW, TAB} from '../keyboard/keycodes'; |
| 4 | + |
| 5 | +class FakeFocusable { |
| 6 | + disabled = false; |
| 7 | + focus() {} |
| 8 | +} |
| 9 | + |
| 10 | +const DOWN_ARROW_EVENT = { keyCode: DOWN_ARROW } as KeyboardEvent; |
| 11 | +const UP_ARROW_EVENT = { keyCode: UP_ARROW } as KeyboardEvent; |
| 12 | +const TAB_EVENT = { keyCode: TAB } as KeyboardEvent; |
| 13 | + |
| 14 | +describe('ListKeyManager', () => { |
| 15 | + let keyManager: ListKeyManager; |
| 16 | + let itemList: QueryList<MdFocusable>; |
| 17 | + let items: MdFocusable[]; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + itemList = new QueryList<MdFocusable>(); |
| 21 | + items = [ |
| 22 | + new FakeFocusable(), |
| 23 | + new FakeFocusable(), |
| 24 | + new FakeFocusable() |
| 25 | + ]; |
| 26 | + |
| 27 | + itemList.toArray = () => items; |
| 28 | + |
| 29 | + keyManager = new ListKeyManager(itemList); |
| 30 | + |
| 31 | + // first item is already focused |
| 32 | + keyManager.focusedItemIndex = 0; |
| 33 | + |
| 34 | + spyOn(items[0], 'focus'); |
| 35 | + spyOn(items[1], 'focus'); |
| 36 | + spyOn(items[2], 'focus'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should focus subsequent items when down arrow is pressed', () => { |
| 40 | + keyManager.onKeydown(DOWN_ARROW_EVENT); |
| 41 | + |
| 42 | + expect(items[0].focus).not.toHaveBeenCalled(); |
| 43 | + expect(items[1].focus).toHaveBeenCalledTimes(1); |
| 44 | + expect(items[2].focus).not.toHaveBeenCalled(); |
| 45 | + |
| 46 | + keyManager.onKeydown(DOWN_ARROW_EVENT); |
| 47 | + expect(items[0].focus).not.toHaveBeenCalled(); |
| 48 | + expect(items[1].focus).toHaveBeenCalledTimes(1); |
| 49 | + expect(items[2].focus).toHaveBeenCalledTimes(1); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should focus previous items when up arrow is pressed', () => { |
| 53 | + keyManager.onKeydown(DOWN_ARROW_EVENT); |
| 54 | + |
| 55 | + expect(items[0].focus).not.toHaveBeenCalled(); |
| 56 | + expect(items[1].focus).toHaveBeenCalledTimes(1); |
| 57 | + |
| 58 | + keyManager.onKeydown(UP_ARROW_EVENT); |
| 59 | + |
| 60 | + expect(items[0].focus).toHaveBeenCalledTimes(1); |
| 61 | + expect(items[1].focus).toHaveBeenCalledTimes(1); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should skip disabled items using arrow keys', () => { |
| 65 | + items[1].disabled = true; |
| 66 | + |
| 67 | + // down arrow should skip past disabled item from 0 to 2 |
| 68 | + keyManager.onKeydown(DOWN_ARROW_EVENT); |
| 69 | + expect(items[0].focus).not.toHaveBeenCalled(); |
| 70 | + expect(items[1].focus).not.toHaveBeenCalled(); |
| 71 | + expect(items[2].focus).toHaveBeenCalledTimes(1); |
| 72 | + |
| 73 | + // up arrow should skip past disabled item from 2 to 0 |
| 74 | + keyManager.onKeydown(UP_ARROW_EVENT); |
| 75 | + expect(items[0].focus).toHaveBeenCalledTimes(1); |
| 76 | + expect(items[1].focus).not.toHaveBeenCalled(); |
| 77 | + expect(items[2].focus).toHaveBeenCalledTimes(1); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should wrap back to menu when arrow keying past items', () => { |
| 81 | + keyManager.onKeydown(DOWN_ARROW_EVENT); |
| 82 | + keyManager.onKeydown(DOWN_ARROW_EVENT); |
| 83 | + |
| 84 | + expect(items[0].focus).not.toHaveBeenCalled(); |
| 85 | + expect(items[1].focus).toHaveBeenCalledTimes(1); |
| 86 | + expect(items[2].focus).toHaveBeenCalledTimes(1); |
| 87 | + |
| 88 | + // this down arrow moves down past the end of the list |
| 89 | + keyManager.onKeydown(DOWN_ARROW_EVENT); |
| 90 | + expect(items[0].focus).toHaveBeenCalledTimes(1); |
| 91 | + expect(items[1].focus).toHaveBeenCalledTimes(1); |
| 92 | + expect(items[2].focus).toHaveBeenCalledTimes(1); |
| 93 | + |
| 94 | + // this up arrow moves up past the beginning of the list |
| 95 | + keyManager.onKeydown(UP_ARROW_EVENT); |
| 96 | + expect(items[0].focus).toHaveBeenCalledTimes(1); |
| 97 | + expect(items[1].focus).toHaveBeenCalledTimes(1); |
| 98 | + expect(items[2].focus).toHaveBeenCalledTimes(2); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should emit tabOut when the tab key is pressed', () => { |
| 102 | + let tabOutEmitted = false; |
| 103 | + keyManager.tabOut.first().subscribe(() => tabOutEmitted = true); |
| 104 | + keyManager.onKeydown(TAB_EVENT); |
| 105 | + |
| 106 | + expect(tabOutEmitted).toBe(true); |
| 107 | + }); |
| 108 | + |
| 109 | +}); |
0 commit comments