/*** IMPORT ------------------------------------------- ***/ import { expect, test } from "vitest"; /*** UTILITY ------------------------------------------ ***/ import { matchShortcut } from "../source/library/state/keyboard.ts"; type EventInit = { altKey?: boolean; ctrlKey?: boolean; key: string; metaKey?: boolean; shiftKey?: boolean; }; function makeEvent(init: EventInit): KeyboardEvent { return { altKey: init.altKey ?? false, ctrlKey: init.ctrlKey ?? false, key: init.key, metaKey: init.metaKey ?? false, shiftKey: init.shiftKey ?? false } as KeyboardEvent; } /*** TESTS -------------------------------------------- ***/ test("matchShortcut returns null for plain Enter", () => { expect(matchShortcut(makeEvent({ key: "Enter" }))).toEqual(null); }); test("matchShortcut returns null when no modifier is pressed", () => { expect(matchShortcut(makeEvent({ key: "f", shiftKey: true }))).toEqual(null); }); test("matchShortcut maps Cmd+Enter to run", () => { expect( matchShortcut(makeEvent({ key: "Enter", metaKey: true })) ).toEqual({ type: "run" }); }); test("matchShortcut maps Ctrl+Enter to run", () => { expect( matchShortcut(makeEvent({ ctrlKey: true, key: "Enter" })) ).toEqual({ type: "run" }); }); test("matchShortcut maps Cmd+Shift+Enter to newTab", () => { expect( matchShortcut(makeEvent({ key: "Enter", metaKey: true, shiftKey: true })) ).toEqual({ type: "newTab" }); }); test("matchShortcut maps Ctrl+Shift+Enter to newTab", () => { expect( matchShortcut(makeEvent({ ctrlKey: true, key: "Enter", shiftKey: true })) ).toEqual({ type: "newTab" }); }); test("matchShortcut maps Cmd+Shift+W to closeTab", () => { expect( matchShortcut(makeEvent({ key: "w", metaKey: true, shiftKey: true })) ).toEqual({ type: "closeTab" }); }); test("matchShortcut maps Ctrl+Shift+W to closeTab", () => { expect( matchShortcut(makeEvent({ ctrlKey: true, key: "W", shiftKey: true })) ).toEqual({ type: "closeTab" }); }); test("matchShortcut maps Cmd+Shift+F to format on mac", () => { expect( matchShortcut(makeEvent({ key: "f", metaKey: true, shiftKey: true })) ).toEqual({ type: "format" }); }); test("matchShortcut maps Ctrl+Shift+F to format on non-mac", () => { expect( matchShortcut(makeEvent({ ctrlKey: true, key: "F", shiftKey: true })) ).toEqual({ type: "format" }); }); test("matchShortcut maps Cmd+Alt+ArrowRight to nextTab", () => { expect( matchShortcut(makeEvent({ altKey: true, key: "ArrowRight", metaKey: true })) ).toEqual({ type: "nextTab" }); }); test("matchShortcut maps Ctrl+Alt+ArrowLeft to prevTab", () => { expect( matchShortcut(makeEvent({ altKey: true, ctrlKey: true, key: "ArrowLeft" })) ).toEqual({ type: "prevTab" }); }); test("matchShortcut ignores unrelated Cmd+key combos", () => { expect( matchShortcut(makeEvent({ key: "s", metaKey: true })) ).toEqual(null); }); test("matchShortcut ignores Cmd+Alt+Enter", () => { expect( matchShortcut(makeEvent({ altKey: true, key: "Enter", metaKey: true })) ).toEqual(null); });