diff --git a/public/app/core/services/KeybindingSet.test.ts b/public/app/core/services/KeybindingSet.test.ts new file mode 100644 index 00000000000..f6d565b7916 --- /dev/null +++ b/public/app/core/services/KeybindingSet.test.ts @@ -0,0 +1,44 @@ +import { KeybindingSet } from './KeybindingSet'; +import { mousetrap } from './mousetrap'; + +jest.mock('./mousetrap'); + +afterAll(() => { + jest.unmock('./mousetrap'); +}); + +describe('KeybindingSet', () => { + let keyBindingSet: KeybindingSet; + beforeEach(() => { + keyBindingSet = new KeybindingSet(); + }); + + test('Binds and unbinds keys', () => { + keyBindingSet.addBinding({ + key: 'a b', + onTrigger: () => {}, + }); + + expect(mousetrap.bind).toHaveBeenCalledTimes(1); + expect(mousetrap.bind).toHaveBeenCalledWith('a b', expect.any(Function), 'keydown'); + + keyBindingSet.removeAll(); + + expect(mousetrap.unbind).toHaveBeenCalledTimes(1); + expect(mousetrap.unbind).toHaveBeenCalledWith('a b', 'keydown'); + }); + + test('Binds and unbinds keys of a certain type', () => { + keyBindingSet.addBinding({ + key: 'a b', + onTrigger: () => {}, + type: 'keypress', + }); + + expect(mousetrap.bind).toHaveBeenCalledWith('a b', expect.any(Function), 'keypress'); + + keyBindingSet.removeAll(); + + expect(mousetrap.unbind).toHaveBeenCalledWith('a b', 'keypress'); + }); +}); diff --git a/public/app/core/services/KeybindingSet.ts b/public/app/core/services/KeybindingSet.ts index ed9a0892a0f..50962912554 100644 --- a/public/app/core/services/KeybindingSet.ts +++ b/public/app/core/services/KeybindingSet.ts @@ -24,9 +24,12 @@ export class KeybindingSet { evt.returnValue = false; item.onTrigger(); }, - 'keydown' + item.type ?? 'keydown' ); - this._binds.push(item); + this._binds.push({ + ...item, + type: item.type ?? 'keydown', + }); } removeAll() {