test: updateAltGrState (@marcosatc147) (#5656)

This commit is contained in:
Marcos Castilhos 2024-07-28 16:02:03 -03:00 committed by GitHub
parent e8ddd7785e
commit aa74c5eaa0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 94 additions and 1 deletions

View file

@ -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);
});
});
});

View file

@ -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);