Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | import CommandsManager from './CommandsManager'; import HotkeysManager from './HotkeysManager'; import hotkeys from './../utils/hotkeys'; import log from './../log'; import objectHash from 'object-hash'; jest.mock('./CommandsManager'); jest.mock('./../utils/hotkeys'); jest.mock('./../log'); describe('HotkeysManager', () => { let hotkeysManager, commandsManager; beforeEach(() => { commandsManager = new CommandsManager(); hotkeysManager = new HotkeysManager(commandsManager); CommandsManager.mockClear(); hotkeys.mockClear(); log.warn.mockClear(); jest.clearAllMocks(); }); it('has expected properties', () => { const allProperties = Object.keys(hotkeysManager); const expectedProperties = ['hotkeyDefinitions', 'hotkeyDefaults', 'isEnabled']; const containsAllExpectedProperties = expectedProperties.every(expected => allProperties.includes(expected) ); expect(containsAllExpectedProperties).toBe(true); }); describe('disable()', () => { beforeEach(() => hotkeys.pause.mockClear()); it('sets isEnabled property to false', () => { hotkeysManager.disable(); expect(hotkeysManager.isEnabled).toBe(false); }); it('calls hotkeys.pause()', () => { hotkeysManager.disable(); expect(hotkeys.pause.mock.calls.length).toBe(1); }); }); describe('enable()', () => { beforeEach(() => { hotkeys.unpause = jest.fn(); hotkeys.unpause.mockClear(); }); it('sets isEnabled property to true', () => { hotkeysManager.disable(); hotkeysManager.enable(); expect(hotkeysManager.isEnabled).toBe(true); }); it('calls hotkeys.unpause()', () => { hotkeysManager.enable(); expect(hotkeys.unpause.mock.calls.length).toBe(1); }); }); describe('setHotkeys()', () => { it('calls registerHotkeys for each hotkeyDefinition', () => { const hotkeyDefinitions = [ { commandName: 'dance', label: 'dance dance', keys: '+' }, { commandName: 'celebrate', label: 'celebrate everything', keys: 'q' }, ]; hotkeysManager.registerHotkeys = jest.fn(); hotkeysManager.setHotkeys(hotkeyDefinitions); const numberOfCalls = hotkeysManager.registerHotkeys.mock.calls.length; const firstCallArgs = hotkeysManager.registerHotkeys.mock.calls[0][0]; const secondCallArgs = hotkeysManager.registerHotkeys.mock.calls[1][0]; expect(numberOfCalls).toBe(2); expect(firstCallArgs).toEqual(hotkeyDefinitions[0]); expect(secondCallArgs).toEqual(hotkeyDefinitions[1]); }); it('does not set this.hotkeyDefaults when calling setHotKeys', () => { const hotkeyDefinitions = [{ commandName: 'dance', keys: '+' }]; hotkeysManager.setHotkeys(hotkeyDefinitions); expect(hotkeysManager.hotkeyDefaults).toEqual([]); }); }); describe('setDefaultHotKeys()', () => { it('it sets default hotkeys', () => { const hotkeyDefinitions = [{ commandName: 'dance', keys: '+' }]; hotkeysManager.setDefaultHotKeys(hotkeyDefinitions); expect(hotkeysManager.hotkeyDefaults).toEqual(hotkeyDefinitions); }); }); describe('registerHotkeys()', () => { it('throws an Error if a commandName is not provided', () => { const definition = { commandName: undefined, keys: '+' }; expect(() => { hotkeysManager.registerHotkeys(definition); }).toThrow(); }); it('updates hotkeyDefinitions property with registered keys', () => { const definition = { commandName: 'dance', commandOptions: {}, label: 'hello', keys: '+', }; hotkeysManager.registerHotkeys(definition); const numOfHotkeyDefinitions = Object.keys(hotkeysManager.hotkeyDefinitions).length; const commandHash = objectHash({ commandName: definition.commandName, commandOptions: definition.commandOptions, }); const hotkeyDefinitionForRegisteredCommand = hotkeysManager.hotkeyDefinitions[commandHash]; expect(numOfHotkeyDefinitions).toBe(1); expect(Object.keys(hotkeysManager.hotkeyDefinitions)[0]).toEqual(commandHash); expect(hotkeyDefinitionForRegisteredCommand).toEqual(definition); }); it('calls hotkeys.bind for the group of keys', () => { const definition = { commandName: 'dance', keys: ['shift', 'e'] }; hotkeysManager.registerHotkeys(definition); expect(hotkeys.bind.mock.calls.length).toBe(1); expect(hotkeys.bind.mock.calls[0][0]).toBe('shift+e'); }); it('calls hotkeys.unbind if commandName was previously registered, for each previously registered set of keys', () => { const firstDefinition = { commandName: 'dance', keys: ['alt', 'e'], }; const secondDefinition = { commandName: 'dance', keys: 'a' }; // First call hotkeysManager.registerHotkeys(firstDefinition); // Second call hotkeysManager.registerHotkeys(secondDefinition); expect(hotkeys.unbind.mock.calls.length).toBe(1); expect(hotkeys.unbind.mock.calls[0][0]).toBe('alt+e'); }); }); describe('restoreDefaults()', () => { it('calls setHotkeys with hotkey defaults', () => { hotkeysManager.setHotkeys = jest.fn(); hotkeysManager.restoreDefaultBindings(); expect(hotkeysManager.setHotkeys.mock.calls[0][0]).toEqual(hotkeysManager.hotkeyDefaults); }); }); describe('destroy()', () => { it('clears default and definition properties', () => { hotkeysManager.hotkeyDefaults = ['hotdog', 'jeremy', 'qasar']; hotkeysManager.hotkeyDefinitions = { hello: 'world', }; hotkeysManager.destroy(); expect(hotkeysManager.hotkeyDefaults).toEqual([]); expect(hotkeysManager.hotkeyDefinitions).toEqual({}); }); it('resets all hotkey bindings', () => { hotkeysManager.destroy(); expect(hotkeys.reset.mock.calls.length).toEqual(1); }); }); }); |