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

Commit 8c67984

Browse files
Remove mock from KeyboardShortcuts.ts (#9034)
1 parent 19e514d commit 8c67984

File tree

2 files changed

+55
-47
lines changed

2 files changed

+55
-47
lines changed

src/accessibility/KeyboardShortcuts.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright 2020 The Matrix.org Foundation C.I.C.
3+
Copyright 2022 The Matrix.org Foundation C.I.C.
34
Copyright 2021 - 2022 Šimon Brandner <[email protected]>
45
56
Licensed under the Apache License, Version 2.0 (the "License");
@@ -712,13 +713,3 @@ export const KEYBOARD_SHORTCUTS: IKeyboardShortcuts = {
712713
},
713714
},
714715
};
715-
716-
// For tests
717-
export function mock({ keyboardShortcuts, macOnlyShortcuts, desktopShortcuts }): void {
718-
Object.keys(KEYBOARD_SHORTCUTS).forEach((k) => delete KEYBOARD_SHORTCUTS[k]);
719-
if (keyboardShortcuts) Object.assign(KEYBOARD_SHORTCUTS, keyboardShortcuts);
720-
MAC_ONLY_SHORTCUTS.splice(0, MAC_ONLY_SHORTCUTS.length);
721-
if (macOnlyShortcuts) macOnlyShortcuts.forEach((e) => MAC_ONLY_SHORTCUTS.push(e));
722-
DESKTOP_SHORTCUTS.splice(0, DESKTOP_SHORTCUTS.length);
723-
if (desktopShortcuts) desktopShortcuts.forEach((e) => DESKTOP_SHORTCUTS.push(e));
724-
}
Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright 2022 Šimon Brandner <[email protected]>
3+
Copyright 2022 The Matrix.org Foundation C.I.C.
34
45
Licensed under the Apache License, Version 2.0 (the "License");
56
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
1415
limitations under the License.
1516
*/
1617

17-
import {
18-
KEYBOARD_SHORTCUTS,
19-
mock,
20-
} from "../../src/accessibility/KeyboardShortcuts";
21-
import { getKeyboardShortcuts, getKeyboardShortcutsForUI } from "../../src/accessibility/KeyboardShortcutUtils";
2218
import { mockPlatformPeg, unmockPlatformPeg } from "../test-utils";
2319

20+
const PATH_TO_KEYBOARD_SHORTCUTS = "../../src/accessibility/KeyboardShortcuts";
21+
const PATH_TO_KEYBOARD_SHORTCUT_UTILS = "../../src/accessibility/KeyboardShortcutUtils";
22+
23+
const mockKeyboardShortcuts = (override) => {
24+
jest.doMock(PATH_TO_KEYBOARD_SHORTCUTS, () => {
25+
const original = jest.requireActual(PATH_TO_KEYBOARD_SHORTCUTS);
26+
return {
27+
...original,
28+
...override,
29+
};
30+
});
31+
};
32+
const getFile = async () => await import(PATH_TO_KEYBOARD_SHORTCUTS);
33+
const getUtils = async () => await import(PATH_TO_KEYBOARD_SHORTCUT_UTILS);
34+
2435
describe("KeyboardShortcutUtils", () => {
25-
afterEach(() => {
36+
beforeEach(() => {
2637
unmockPlatformPeg();
38+
jest.resetModules();
2739
});
2840

2941
it("doesn't change KEYBOARD_SHORTCUTS when getting shortcuts", async () => {
30-
mock({
31-
keyboardShortcuts: {
42+
mockKeyboardShortcuts({
43+
KEYBOARD_SHORTCUTS: {
3244
"Keybind1": {},
3345
"Keybind2": {},
3446
},
35-
macOnlyShortcuts: ["Keybind1"],
36-
desktopShortcuts: ["Keybind2"],
47+
MAC_ONLY_SHORTCUTS: ["Keybind1"],
48+
DESKTOP_SHORTCUTS: ["Keybind2"],
3749
});
3850
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) });
39-
const copyKeyboardShortcuts = Object.assign({}, KEYBOARD_SHORTCUTS);
51+
const utils = await getUtils();
52+
const file = await getFile();
53+
const copyKeyboardShortcuts = Object.assign({}, file.KEYBOARD_SHORTCUTS);
4054

41-
getKeyboardShortcuts();
42-
expect(KEYBOARD_SHORTCUTS).toEqual(copyKeyboardShortcuts);
43-
getKeyboardShortcutsForUI();
44-
expect(KEYBOARD_SHORTCUTS).toEqual(copyKeyboardShortcuts);
55+
utils.getKeyboardShortcuts();
56+
expect(file.KEYBOARD_SHORTCUTS).toEqual(copyKeyboardShortcuts);
57+
utils.getKeyboardShortcutsForUI();
58+
expect(file.KEYBOARD_SHORTCUTS).toEqual(copyKeyboardShortcuts);
4559
});
4660

47-
it("correctly filters shortcuts", async () => {
48-
mock({
49-
keyboardShortcuts: {
50-
"Keybind1": {},
51-
"Keybind2": {},
52-
"Keybind3": { "controller": { settingDisabled: true } },
53-
"Keybind4": {},
54-
},
55-
macOnlyShortcuts: ["Keybind1"],
56-
desktopShortcuts: ["Keybind2"],
57-
61+
describe("correctly filters shortcuts", () => {
62+
it("when on web and not on macOS ", async () => {
63+
mockKeyboardShortcuts({
64+
KEYBOARD_SHORTCUTS: {
65+
"Keybind1": {},
66+
"Keybind2": {},
67+
"Keybind3": { "controller": { settingDisabled: true } },
68+
"Keybind4": {},
69+
},
70+
MAC_ONLY_SHORTCUTS: ["Keybind1"],
71+
DESKTOP_SHORTCUTS: ["Keybind2"],
72+
});
73+
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) });
74+
expect((await getUtils()).getKeyboardShortcuts()).toEqual({ "Keybind4": {} });
5875
});
59-
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) });
60-
expect(getKeyboardShortcuts()).toEqual({ "Keybind4": {} });
6176

62-
mock({
63-
keyboardShortcuts: {
64-
"Keybind1": {},
65-
"Keybind2": {},
66-
},
67-
macOnlyShortcuts: undefined,
68-
desktopShortcuts: ["Keybind2"],
77+
it("when on desktop", async () => {
78+
mockKeyboardShortcuts({
79+
KEYBOARD_SHORTCUTS: {
80+
"Keybind1": {},
81+
"Keybind2": {},
82+
},
83+
MAC_ONLY_SHORTCUTS: [],
84+
DESKTOP_SHORTCUTS: ["Keybind2"],
85+
});
86+
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(true) });
87+
expect((await getUtils()).getKeyboardShortcuts()).toEqual({ "Keybind1": {}, "Keybind2": {} });
6988
});
70-
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(true) });
71-
expect(getKeyboardShortcuts()).toEqual({ "Keybind1": {}, "Keybind2": {} });
7289
});
7390
});

0 commit comments

Comments
 (0)