mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-22 05:26:14 +08:00
test: updateAltGrState (@marcosatc147) (#5656)
This commit is contained in:
parent
e8ddd7785e
commit
aa74c5eaa0
2 changed files with 94 additions and 1 deletions
89
frontend/__tests__/test/layout-emulator.spec.ts
Normal file
89
frontend/__tests__/test/layout-emulator.spec.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue