Keybinding code unification #6 (#8042)

This commit is contained in:
Šimon Brandner 2022-03-14 14:25:51 +01:00 committed by GitHub
parent e725f14651
commit 8d13e238b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 462 additions and 351 deletions

View file

@ -21,6 +21,7 @@ import { mount, ReactWrapper } from "enzyme";
import { Key } from "../../../../../../src/Keyboard";
const PATH_TO_KEYBOARD_SHORTCUTS = "../../../../../../src/accessibility/KeyboardShortcuts";
const PATH_TO_KEYBOARD_SHORTCUT_UTILS = "../../../../../../src/accessibility/KeyboardShortcutUtils";
const PATH_TO_COMPONENT = "../../../../../../src/components/views/settings/tabs/user/KeyboardUserSettingsTab";
const mockKeyboardShortcuts = (override) => {
@ -33,9 +34,19 @@ const mockKeyboardShortcuts = (override) => {
});
};
const renderKeyboardUserSettingsTab = async (component, props?): Promise<ReactWrapper> => {
const mockKeyboardShortcutUtils = (override) => {
jest.doMock(PATH_TO_KEYBOARD_SHORTCUT_UTILS, () => {
const original = jest.requireActual(PATH_TO_KEYBOARD_SHORTCUT_UTILS);
return {
...original,
...override,
};
});
};
const renderKeyboardUserSettingsTab = async (component): Promise<ReactWrapper> => {
const Component = (await import(PATH_TO_COMPONENT))[component];
return mount(<Component {...props} />);
return mount(<Component />);
};
describe("KeyboardUserSettingsTab", () => {
@ -43,79 +54,8 @@ describe("KeyboardUserSettingsTab", () => {
jest.resetModules();
});
it("renders key icon", async () => {
const body = await renderKeyboardUserSettingsTab("KeyboardKey", { name: Key.ARROW_DOWN });
expect(body).toMatchSnapshot();
});
it("renders alternative key name", async () => {
const body = await renderKeyboardUserSettingsTab("KeyboardKey", { name: Key.PAGE_DOWN });
expect(body).toMatchSnapshot();
});
it("doesn't render + if last", async () => {
const body = await renderKeyboardUserSettingsTab("KeyboardKey", { name: Key.A, last: true });
expect(body).toMatchSnapshot();
});
it("doesn't render same modifier twice", async () => {
mockKeyboardShortcuts({
"getKeyboardShortcutsForUI": () => ({
"keybind1": {
default: {
key: Key.A,
ctrlOrCmdKey: true,
metaKey: true,
},
displayName: "Cancel replying to a message",
},
}),
});
const body1 = await renderKeyboardUserSettingsTab("KeyboardShortcut", { name: "keybind1" });
expect(body1).toMatchSnapshot();
jest.resetModules();
mockKeyboardShortcuts({
"getKeyboardShortcutsForUI": () => ({
"keybind1": {
default: {
key: Key.A,
ctrlOrCmdKey: true,
ctrlKey: true,
},
displayName: "Cancel replying to a message",
},
}),
});
const body2 = await renderKeyboardUserSettingsTab("KeyboardShortcut", { name: "keybind1" });
expect(body2).toMatchSnapshot();
jest.resetModules();
});
it("renders list of keyboard shortcuts", async () => {
mockKeyboardShortcuts({
"getKeyboardShortcutsForUI": () => ({
"keybind1": {
default: {
key: Key.A,
ctrlKey: true,
},
displayName: "Cancel replying to a message",
},
"keybind2": {
default: {
key: Key.B,
ctrlKey: true,
},
displayName: "Toggle Bold",
},
"keybind3": {
default: {
key: Key.ENTER,
},
displayName: "Select room from the room list",
},
}),
"CATEGORIES": {
"Composer": {
settingNames: ["keybind1", "keybind2"],
@ -127,6 +67,38 @@ describe("KeyboardUserSettingsTab", () => {
},
},
});
mockKeyboardShortcutUtils({
"getKeyboardShortcutValue": (name) => {
switch (name) {
case "keybind1":
return {
key: Key.A,
ctrlKey: true,
};
case "keybind2": {
return {
key: Key.B,
ctrlKey: true };
}
case "keybind3": {
return {
key: Key.ENTER,
};
}
}
},
"getKeyboardShortcutDisplayName": (name) => {
switch (name) {
case "keybind1":
return "Cancel replying to a message";
case "keybind2":
return "Toggle Bold";
case "keybind3":
return "Select room from the room list";
}
},
});
const body = await renderKeyboardUserSettingsTab("default");
expect(body).toMatchSnapshot();