Remove `mock` from `KeyboardShortcuts.ts` (#9034)

pull/28788/head^2
Šimon Brandner 2022-07-11 09:56:20 +02:00 committed by GitHub
parent 19e514d83c
commit 8c67984f50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 47 deletions

View File

@ -1,5 +1,6 @@
/* /*
Copyright 2020 The Matrix.org Foundation C.I.C. Copyright 2020 The Matrix.org Foundation C.I.C.
Copyright 2022 The Matrix.org Foundation C.I.C.
Copyright 2021 - 2022 Šimon Brandner <simon.bra.ag@gmail.com> Copyright 2021 - 2022 Šimon Brandner <simon.bra.ag@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
@ -712,13 +713,3 @@ export const KEYBOARD_SHORTCUTS: IKeyboardShortcuts = {
}, },
}, },
}; };
// For tests
export function mock({ keyboardShortcuts, macOnlyShortcuts, desktopShortcuts }): void {
Object.keys(KEYBOARD_SHORTCUTS).forEach((k) => delete KEYBOARD_SHORTCUTS[k]);
if (keyboardShortcuts) Object.assign(KEYBOARD_SHORTCUTS, keyboardShortcuts);
MAC_ONLY_SHORTCUTS.splice(0, MAC_ONLY_SHORTCUTS.length);
if (macOnlyShortcuts) macOnlyShortcuts.forEach((e) => MAC_ONLY_SHORTCUTS.push(e));
DESKTOP_SHORTCUTS.splice(0, DESKTOP_SHORTCUTS.length);
if (desktopShortcuts) desktopShortcuts.forEach((e) => DESKTOP_SHORTCUTS.push(e));
}

View File

@ -1,5 +1,6 @@
/* /*
Copyright 2022 Šimon Brandner <simon.bra.ag@gmail.com> Copyright 2022 Šimon Brandner <simon.bra.ag@gmail.com>
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -14,60 +15,76 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {
KEYBOARD_SHORTCUTS,
mock,
} from "../../src/accessibility/KeyboardShortcuts";
import { getKeyboardShortcuts, getKeyboardShortcutsForUI } from "../../src/accessibility/KeyboardShortcutUtils";
import { mockPlatformPeg, unmockPlatformPeg } from "../test-utils"; import { mockPlatformPeg, unmockPlatformPeg } from "../test-utils";
const PATH_TO_KEYBOARD_SHORTCUTS = "../../src/accessibility/KeyboardShortcuts";
const PATH_TO_KEYBOARD_SHORTCUT_UTILS = "../../src/accessibility/KeyboardShortcutUtils";
const mockKeyboardShortcuts = (override) => {
jest.doMock(PATH_TO_KEYBOARD_SHORTCUTS, () => {
const original = jest.requireActual(PATH_TO_KEYBOARD_SHORTCUTS);
return {
...original,
...override,
};
});
};
const getFile = async () => await import(PATH_TO_KEYBOARD_SHORTCUTS);
const getUtils = async () => await import(PATH_TO_KEYBOARD_SHORTCUT_UTILS);
describe("KeyboardShortcutUtils", () => { describe("KeyboardShortcutUtils", () => {
afterEach(() => { beforeEach(() => {
unmockPlatformPeg(); unmockPlatformPeg();
jest.resetModules();
}); });
it("doesn't change KEYBOARD_SHORTCUTS when getting shortcuts", async () => { it("doesn't change KEYBOARD_SHORTCUTS when getting shortcuts", async () => {
mock({ mockKeyboardShortcuts({
keyboardShortcuts: { KEYBOARD_SHORTCUTS: {
"Keybind1": {}, "Keybind1": {},
"Keybind2": {}, "Keybind2": {},
}, },
macOnlyShortcuts: ["Keybind1"], MAC_ONLY_SHORTCUTS: ["Keybind1"],
desktopShortcuts: ["Keybind2"], DESKTOP_SHORTCUTS: ["Keybind2"],
}); });
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) }); mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) });
const copyKeyboardShortcuts = Object.assign({}, KEYBOARD_SHORTCUTS); const utils = await getUtils();
const file = await getFile();
const copyKeyboardShortcuts = Object.assign({}, file.KEYBOARD_SHORTCUTS);
getKeyboardShortcuts(); utils.getKeyboardShortcuts();
expect(KEYBOARD_SHORTCUTS).toEqual(copyKeyboardShortcuts); expect(file.KEYBOARD_SHORTCUTS).toEqual(copyKeyboardShortcuts);
getKeyboardShortcutsForUI(); utils.getKeyboardShortcutsForUI();
expect(KEYBOARD_SHORTCUTS).toEqual(copyKeyboardShortcuts); expect(file.KEYBOARD_SHORTCUTS).toEqual(copyKeyboardShortcuts);
}); });
it("correctly filters shortcuts", async () => { describe("correctly filters shortcuts", () => {
mock({ it("when on web and not on macOS ", async () => {
keyboardShortcuts: { mockKeyboardShortcuts({
KEYBOARD_SHORTCUTS: {
"Keybind1": {}, "Keybind1": {},
"Keybind2": {}, "Keybind2": {},
"Keybind3": { "controller": { settingDisabled: true } }, "Keybind3": { "controller": { settingDisabled: true } },
"Keybind4": {}, "Keybind4": {},
}, },
macOnlyShortcuts: ["Keybind1"], MAC_ONLY_SHORTCUTS: ["Keybind1"],
desktopShortcuts: ["Keybind2"], DESKTOP_SHORTCUTS: ["Keybind2"],
}); });
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) }); mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) });
expect(getKeyboardShortcuts()).toEqual({ "Keybind4": {} }); expect((await getUtils()).getKeyboardShortcuts()).toEqual({ "Keybind4": {} });
});
mock({ it("when on desktop", async () => {
keyboardShortcuts: { mockKeyboardShortcuts({
KEYBOARD_SHORTCUTS: {
"Keybind1": {}, "Keybind1": {},
"Keybind2": {}, "Keybind2": {},
}, },
macOnlyShortcuts: undefined, MAC_ONLY_SHORTCUTS: [],
desktopShortcuts: ["Keybind2"], DESKTOP_SHORTCUTS: ["Keybind2"],
}); });
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(true) }); mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(true) });
expect(getKeyboardShortcuts()).toEqual({ "Keybind1": {}, "Keybind2": {} }); expect((await getUtils()).getKeyboardShortcuts()).toEqual({ "Keybind1": {}, "Keybind2": {} });
});
}); });
}); });