From aa74c5eaa03f60ee2b9cebd9db8cc79cc04fa74b Mon Sep 17 00:00:00 2001 From: Marcos Castilhos <130991577+Marcosatc147@users.noreply.github.com> Date: Sun, 28 Jul 2024 16:02:03 -0300 Subject: [PATCH] test: updateAltGrState (@marcosatc147) (#5656) --- .../__tests__/test/layout-emulator.spec.ts | 89 +++++++++++++++++++ frontend/src/ts/test/layout-emulator.ts | 6 +- 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 frontend/__tests__/test/layout-emulator.spec.ts diff --git a/frontend/__tests__/test/layout-emulator.spec.ts b/frontend/__tests__/test/layout-emulator.spec.ts new file mode 100644 index 000000000..4ea324cee --- /dev/null +++ b/frontend/__tests__/test/layout-emulator.spec.ts @@ -0,0 +1,89 @@ +import { describe, it, expect, afterEach } from "vitest"; +import { + updateAltGrState, + getIsAltGrPressed, +} from "../../src/ts/test/layout-emulator"; + +describe("LayoutEmulator", () => { + describe("updateAltGrState", () => { + afterEach(() => { + // Reset isAltGrPressed state after each test + // Simulate keyup event to reset state + const event = createEvent("AltRight", "keyup"); + updateAltGrState(event); + }); + + const createEvent = ( + code: string, + type: string + ): JQuery.KeyboardEventBase => + ({ + code, + type, + } as JQuery.KeyboardEventBase); + + it("should set isAltGrPressed to true on AltRight keydown", () => { + const event = createEvent("AltRight", "keydown"); + updateAltGrState(event); + expect(getIsAltGrPressed()).toBe(true); + }); + + it("should set isAltGrPressed to false on AltRight keyup", () => { + const event = createEvent("AltRight", "keyup"); + updateAltGrState(event); + expect(getIsAltGrPressed()).toBe(false); + }); + + it("should set isAltGrPressed to true on AltLeft keydown on Mac", () => { + Object.defineProperty(window.navigator, "userAgent", { + value: "Mac", + configurable: true, + }); + const event = createEvent("AltLeft", "keydown"); + updateAltGrState(event); + expect(getIsAltGrPressed()).toBe(true); + }); + + it("should set isAltGrPressed to false on AltLeft keyup on Mac", () => { + Object.defineProperty(window.navigator, "userAgent", { + value: "Mac", + configurable: true, + }); + const event = createEvent("AltLeft", "keyup"); + updateAltGrState(event); + expect(getIsAltGrPressed()).toBe(false); + }); + + it("should not change isAltGrPressed on AltLeft keydown on non-Mac", () => { + Object.defineProperty(window.navigator, "userAgent", { + value: "Windows", + configurable: true, + }); + const event = createEvent("AltLeft", "keydown"); + updateAltGrState(event); + expect(getIsAltGrPressed()).toBe(false); + }); + + it("should not change isAltGrPressed on AltLeft keyup on non-Mac", () => { + Object.defineProperty(window.navigator, "userAgent", { + value: "Windows", + configurable: true, + }); + const event = createEvent("AltLeft", "keyup"); + updateAltGrState(event); + expect(getIsAltGrPressed()).toBe(false); + }); + + it("should not change isAltGrPressed on keydown of other keys", () => { + const event = createEvent("KeyA", "keydown"); + updateAltGrState(event); + expect(getIsAltGrPressed()).toBe(false); + }); + + it("should not change isAltGrPressed on keyup of other keys", () => { + const event = createEvent("KeyA", "keyup"); + updateAltGrState(event); + expect(getIsAltGrPressed()).toBe(false); + }); + }); +}); diff --git a/frontend/src/ts/test/layout-emulator.ts b/frontend/src/ts/test/layout-emulator.ts index 37210d6f5..caa500486 100644 --- a/frontend/src/ts/test/layout-emulator.ts +++ b/frontend/src/ts/test/layout-emulator.ts @@ -230,7 +230,7 @@ export async function getCharFromEvent( } } -function updateAltGrState(event: JQuery.KeyboardEventBase): void { +export function updateAltGrState(event: JQuery.KeyboardEventBase): void { const shouldHandleLeftAlt = event.code === "AltLeft" && navigator.userAgent.includes("Mac"); if (event.code !== "AltRight" && !shouldHandleLeftAlt) return; @@ -238,5 +238,9 @@ function updateAltGrState(event: JQuery.KeyboardEventBase): void { if (event.type === "keyup") isAltGrPressed = false; } +export function getIsAltGrPressed(): boolean { + return isAltGrPressed; +} + $(document).on("keydown", updateAltGrState); $(document).on("keyup", updateAltGrState);