2023-02-07 20:31:55 +01:00
|
|
|
import CommandsManager from './CommandsManager';
|
|
|
|
|
import HotkeysManager from './HotkeysManager';
|
2020-02-12 21:35:04 +01:00
|
|
|
import hotkeys from './../utils/hotkeys';
|
2023-02-07 20:31:55 +01:00
|
|
|
import log from './../log';
|
2021-07-21 13:38:00 +02:00
|
|
|
import objectHash from 'object-hash';
|
2019-08-13 21:36:46 +02:00
|
|
|
|
2023-02-07 20:31:55 +01:00
|
|
|
jest.mock('./CommandsManager');
|
2020-02-12 21:35:04 +01:00
|
|
|
jest.mock('./../utils/hotkeys');
|
2023-02-07 20:31:55 +01:00
|
|
|
jest.mock('./../log');
|
2019-08-13 21:36:46 +02:00
|
|
|
|
|
|
|
|
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);
|
2023-09-01 22:19:39 +02:00
|
|
|
const expectedProperties = ['hotkeyDefinitions', 'hotkeyDefaults', 'isEnabled'];
|
2019-08-13 21:36:46 +02:00
|
|
|
|
2022-07-27 18:39:04 +02:00
|
|
|
const containsAllExpectedProperties = expectedProperties.every(expected =>
|
2019-08-13 21:36:46 +02:00
|
|
|
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()', () => {
|
2020-02-12 21:35:04 +01:00
|
|
|
beforeEach(() => {
|
|
|
|
|
hotkeys.unpause = jest.fn();
|
|
|
|
|
hotkeys.unpause.mockClear();
|
|
|
|
|
});
|
2019-08-13 21:36:46 +02:00
|
|
|
|
|
|
|
|
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]);
|
|
|
|
|
});
|
2021-03-01 16:52:54 +01:00
|
|
|
|
2019-11-28 06:48:27 +01:00
|
|
|
it('does not set this.hotkeyDefaults when calling setHotKeys', () => {
|
2019-08-13 21:36:46 +02:00
|
|
|
const hotkeyDefinitions = [{ commandName: 'dance', keys: '+' }];
|
|
|
|
|
|
|
|
|
|
hotkeysManager.setHotkeys(hotkeyDefinitions);
|
|
|
|
|
|
|
|
|
|
expect(hotkeysManager.hotkeyDefaults).toEqual([]);
|
|
|
|
|
});
|
2019-11-28 06:48:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('setDefaultHotKeys()', () => {
|
|
|
|
|
it('it sets default hotkeys', () => {
|
2019-08-13 21:36:46 +02:00
|
|
|
const hotkeyDefinitions = [{ commandName: 'dance', keys: '+' }];
|
|
|
|
|
|
2019-11-28 06:48:27 +01:00
|
|
|
hotkeysManager.setDefaultHotKeys(hotkeyDefinitions);
|
2019-08-13 21:36:46 +02:00
|
|
|
|
|
|
|
|
expect(hotkeysManager.hotkeyDefaults).toEqual(hotkeyDefinitions);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('registerHotkeys()', () => {
|
2021-03-01 16:52:54 +01:00
|
|
|
it('throws an Error if a commandName is not provided', () => {
|
2019-08-13 21:36:46 +02:00
|
|
|
const definition = { commandName: undefined, keys: '+' };
|
|
|
|
|
|
2021-03-01 16:52:54 +01:00
|
|
|
expect(() => {
|
2021-07-21 13:38:00 +02:00
|
|
|
hotkeysManager.registerHotkeys(definition);
|
2021-03-01 16:52:54 +01:00
|
|
|
}).toThrow();
|
2019-08-13 21:36:46 +02:00
|
|
|
});
|
|
|
|
|
it('updates hotkeyDefinitions property with registered keys', () => {
|
2021-07-21 13:38:00 +02:00
|
|
|
const definition = {
|
|
|
|
|
commandName: 'dance',
|
|
|
|
|
commandOptions: {},
|
|
|
|
|
label: 'hello',
|
|
|
|
|
keys: '+',
|
|
|
|
|
};
|
2019-08-13 21:36:46 +02:00
|
|
|
|
|
|
|
|
hotkeysManager.registerHotkeys(definition);
|
|
|
|
|
|
2023-09-01 22:19:39 +02:00
|
|
|
const numOfHotkeyDefinitions = Object.keys(hotkeysManager.hotkeyDefinitions).length;
|
2021-03-01 16:52:54 +01:00
|
|
|
|
|
|
|
|
const commandHash = objectHash({
|
|
|
|
|
commandName: definition.commandName,
|
2021-07-21 13:38:00 +02:00
|
|
|
commandOptions: definition.commandOptions,
|
2021-03-01 16:52:54 +01:00
|
|
|
});
|
2023-09-01 22:19:39 +02:00
|
|
|
const hotkeyDefinitionForRegisteredCommand = hotkeysManager.hotkeyDefinitions[commandHash];
|
2019-08-13 21:36:46 +02:00
|
|
|
|
|
|
|
|
expect(numOfHotkeyDefinitions).toBe(1);
|
2023-09-01 22:19:39 +02:00
|
|
|
expect(Object.keys(hotkeysManager.hotkeyDefinitions)[0]).toEqual(commandHash);
|
2021-03-01 16:52:54 +01:00
|
|
|
expect(hotkeyDefinitionForRegisteredCommand).toEqual(definition);
|
2019-08-13 21:36:46 +02:00
|
|
|
});
|
2020-02-12 21:35:04 +01:00
|
|
|
it('calls hotkeys.bind for the group of keys', () => {
|
|
|
|
|
const definition = { commandName: 'dance', keys: ['shift', 'e'] };
|
2019-08-13 21:36:46 +02:00
|
|
|
|
|
|
|
|
hotkeysManager.registerHotkeys(definition);
|
|
|
|
|
|
2020-02-12 21:35:04 +01:00
|
|
|
expect(hotkeys.bind.mock.calls.length).toBe(1);
|
|
|
|
|
expect(hotkeys.bind.mock.calls[0][0]).toBe('shift+e');
|
2019-08-13 21:36:46 +02:00
|
|
|
});
|
|
|
|
|
it('calls hotkeys.unbind if commandName was previously registered, for each previously registered set of keys', () => {
|
|
|
|
|
const firstDefinition = {
|
|
|
|
|
commandName: 'dance',
|
2020-02-12 21:35:04 +01:00
|
|
|
keys: ['alt', 'e'],
|
2019-08-13 21:36:46 +02:00
|
|
|
};
|
|
|
|
|
const secondDefinition = { commandName: 'dance', keys: 'a' };
|
|
|
|
|
|
|
|
|
|
// First call
|
|
|
|
|
hotkeysManager.registerHotkeys(firstDefinition);
|
|
|
|
|
// Second call
|
|
|
|
|
hotkeysManager.registerHotkeys(secondDefinition);
|
|
|
|
|
|
2020-02-12 21:35:04 +01:00
|
|
|
expect(hotkeys.unbind.mock.calls.length).toBe(1);
|
|
|
|
|
expect(hotkeys.unbind.mock.calls[0][0]).toBe('alt+e');
|
2019-08-13 21:36:46 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('restoreDefaults()', () => {
|
|
|
|
|
it('calls setHotkeys with hotkey defaults', () => {
|
|
|
|
|
hotkeysManager.setHotkeys = jest.fn();
|
|
|
|
|
|
|
|
|
|
hotkeysManager.restoreDefaultBindings();
|
|
|
|
|
|
2023-09-01 22:19:39 +02:00
|
|
|
expect(hotkeysManager.setHotkeys.mock.calls[0][0]).toEqual(hotkeysManager.hotkeyDefaults);
|
2019-08-13 21:36:46 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|