Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Remove mock from KeyboardShortcuts.ts (#9034)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBrandner authored Jul 11, 2022
1 parent 19e514d commit 8c67984
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 47 deletions.
11 changes: 1 addition & 10 deletions src/accessibility/KeyboardShortcuts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2020 The Matrix.org Foundation C.I.C.
Copyright 2022 The Matrix.org Foundation C.I.C.
Copyright 2021 - 2022 Šimon Brandner <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -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));
}
91 changes: 54 additions & 37 deletions test/accessibility/KeyboardShortcutUtils-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022 Šimon Brandner <[email protected]>
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -14,60 +15,76 @@ See the License for the specific language governing permissions and
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";

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", () => {
afterEach(() => {
beforeEach(() => {
unmockPlatformPeg();
jest.resetModules();
});

it("doesn't change KEYBOARD_SHORTCUTS when getting shortcuts", async () => {
mock({
keyboardShortcuts: {
mockKeyboardShortcuts({
KEYBOARD_SHORTCUTS: {
"Keybind1": {},
"Keybind2": {},
},
macOnlyShortcuts: ["Keybind1"],
desktopShortcuts: ["Keybind2"],
MAC_ONLY_SHORTCUTS: ["Keybind1"],
DESKTOP_SHORTCUTS: ["Keybind2"],
});
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();
expect(KEYBOARD_SHORTCUTS).toEqual(copyKeyboardShortcuts);
getKeyboardShortcutsForUI();
expect(KEYBOARD_SHORTCUTS).toEqual(copyKeyboardShortcuts);
utils.getKeyboardShortcuts();
expect(file.KEYBOARD_SHORTCUTS).toEqual(copyKeyboardShortcuts);
utils.getKeyboardShortcutsForUI();
expect(file.KEYBOARD_SHORTCUTS).toEqual(copyKeyboardShortcuts);
});

it("correctly filters shortcuts", async () => {
mock({
keyboardShortcuts: {
"Keybind1": {},
"Keybind2": {},
"Keybind3": { "controller": { settingDisabled: true } },
"Keybind4": {},
},
macOnlyShortcuts: ["Keybind1"],
desktopShortcuts: ["Keybind2"],

describe("correctly filters shortcuts", () => {
it("when on web and not on macOS ", async () => {
mockKeyboardShortcuts({
KEYBOARD_SHORTCUTS: {
"Keybind1": {},
"Keybind2": {},
"Keybind3": { "controller": { settingDisabled: true } },
"Keybind4": {},
},
MAC_ONLY_SHORTCUTS: ["Keybind1"],
DESKTOP_SHORTCUTS: ["Keybind2"],
});
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) });
expect((await getUtils()).getKeyboardShortcuts()).toEqual({ "Keybind4": {} });
});
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) });
expect(getKeyboardShortcuts()).toEqual({ "Keybind4": {} });

mock({
keyboardShortcuts: {
"Keybind1": {},
"Keybind2": {},
},
macOnlyShortcuts: undefined,
desktopShortcuts: ["Keybind2"],
it("when on desktop", async () => {
mockKeyboardShortcuts({
KEYBOARD_SHORTCUTS: {
"Keybind1": {},
"Keybind2": {},
},
MAC_ONLY_SHORTCUTS: [],
DESKTOP_SHORTCUTS: ["Keybind2"],
});
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(true) });
expect((await getUtils()).getKeyboardShortcuts()).toEqual({ "Keybind1": {}, "Keybind2": {} });
});
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(true) });
expect(getKeyboardShortcuts()).toEqual({ "Keybind1": {}, "Keybind2": {} });
});
});

0 comments on commit 8c67984

Please sign in to comment.