From 7ee6b2487201f32d93a6b8f4e9f1857da2cf2fa6 Mon Sep 17 00:00:00 2001 From: Matias Chomicki Date: Mon, 9 Jun 2025 19:08:52 +0200 Subject: [PATCH] KeybindingSet: fix missing item type (#106428) * KeybindingSet: fix missing item type * KeybindingSet: pass default value to bind call --- .../app/core/services/KeybindingSet.test.ts | 44 +++++++++++++++++++ public/app/core/services/KeybindingSet.ts | 7 ++- 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 public/app/core/services/KeybindingSet.test.ts 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() {