From 81f52283cff6c542e54964d98a93e9d50604961b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Wed, 16 Feb 2022 16:24:00 +0100 Subject: [PATCH] Don't change `KEYBOARD_SHORTCUTS` and do some refactoring (#7818) --- src/accessibility/KeyboardShortcuts.ts | 14 +++++++------- test/accessibility/KeyboardShortcuts-test.ts | 11 +++++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/accessibility/KeyboardShortcuts.ts b/src/accessibility/KeyboardShortcuts.ts index 0dfd441ae0..85a9afbc46 100644 --- a/src/accessibility/KeyboardShortcuts.ts +++ b/src/accessibility/KeyboardShortcuts.ts @@ -250,7 +250,8 @@ export const CATEGORIES: Record = { // This is very intentionally modelled after SETTINGS as it will make it easier // to implement customizable keyboard shortcuts // TODO: TravisR will fix this nightmare when the new version of the SettingsStore becomes a thing -const KEYBOARD_SHORTCUTS: IKeyboardShortcuts = { +// XXX: Exported for tests +export const KEYBOARD_SHORTCUTS: IKeyboardShortcuts = { [KeyBindingAction.FormatBold]: { default: { ctrlOrCmdKey: true, @@ -582,7 +583,7 @@ const getNonCustomizableShortcuts = (): IKeyboardShortcuts => { }; export const getCustomizableShortcuts = (): IKeyboardShortcuts => { - const keyboardShortcuts = KEYBOARD_SHORTCUTS; + const keyboardShortcuts = Object.assign({}, KEYBOARD_SHORTCUTS); keyboardShortcuts[KeyBindingAction.EditRedo] = { default: { @@ -607,11 +608,10 @@ export const getKeyboardShortcuts = (): IKeyboardShortcuts => { ...Object.entries(getCustomizableShortcuts()), ]; - const keyboardShortcuts: IKeyboardShortcuts = {}; - for (const [key, value] of entries) { - keyboardShortcuts[key] = value; - } - return keyboardShortcuts; + return entries.reduce((acc, [key, value]) => { + acc[key] = value; + return acc; + }, {}); }; export const registerShortcut = (shortcutName: string, categoryName: CategoryName, shortcut: ISetting): void => { diff --git a/test/accessibility/KeyboardShortcuts-test.ts b/test/accessibility/KeyboardShortcuts-test.ts index cac87367a4..88be362af9 100644 --- a/test/accessibility/KeyboardShortcuts-test.ts +++ b/test/accessibility/KeyboardShortcuts-test.ts @@ -17,13 +17,24 @@ limitations under the License. import { CATEGORIES, CategoryName, + getCustomizableShortcuts, getKeyboardShortcuts, + KEYBOARD_SHORTCUTS, registerShortcut, } from "../../src/accessibility/KeyboardShortcuts"; import { Key } from "../../src/Keyboard"; import { ISetting } from "../../src/settings/Settings"; describe("KeyboardShortcuts", () => { + it("doesn't change KEYBOARD_SHORTCUTS when getting shortcuts", () => { + const copyKeyboardShortcuts = Object.assign({}, KEYBOARD_SHORTCUTS); + + getCustomizableShortcuts(); + expect(KEYBOARD_SHORTCUTS).toEqual(copyKeyboardShortcuts); + getKeyboardShortcuts(); + expect(KEYBOARD_SHORTCUTS).toEqual(copyKeyboardShortcuts); + }); + describe("registerShortcut()", () => { it("correctly registers shortcut", () => { const shortcutName = "Keybinding.definitelyARealShortcut";