Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 isMacPlatform,
5 matchesKeybindingShortcut,
6 matchesKeybindingShortcutModifiers,
7 resolveEventKeys,
8 shortcutKeyFromEvent,
9 type ShortcutEventLike,
10} from "./keybindingMatching.ts";
11
12const event = (key: string, overrides: Partial<ShortcutEventLike> = {}): ShortcutEventLike => ({
13 key,
14 metaKey: false,
15 ctrlKey: false,
16 shiftKey: false,
17 altKey: false,
18 ...overrides,
19});
20
21const shortcut = (key: string) => ({
22 key,
23 metaKey: false,
24 ctrlKey: false,
25 shiftKey: false,
26 altKey: false,
27 modKey: false,
28});
29
30describe("isMacPlatform", () => {
31 it("matches navigator.platform values for Apple devices only", () => {
32 expect(isMacPlatform("MacIntel")).toBe(true);
33 expect(isMacPlatform("iPad")).toBe(true);
34 expect(isMacPlatform("darwin")).toBe(false);
35 expect(isMacPlatform("Linux x86_64")).toBe(false);
36 });
37});
38
39describe("shortcutKeyFromEvent", () => {
40 it("falls back to the physical code when the key is unidentified", () => {
41 expect(shortcutKeyFromEvent(event("Unidentified", { code: "Comma" }))).toBe(",");
42 });
43
44 it("keeps an unshifted + as a zoom key unless it came from a digit row", () => {
45 expect(shortcutKeyFromEvent(event("+", { code: "Equal" }))).toBe("+");
46 expect(shortcutKeyFromEvent(event("+", { code: "Digit1" }))).toBe("1");
47 expect(shortcutKeyFromEvent(event("+", { code: "Equal", shiftKey: true }))).toBe("=");
48 });
49});
50
51describe("resolveEventKeys", () => {
52 it("adds the physical letter when the layout types punctuation there", () => {
53 const keys = resolveEventKeys(event(",", { code: "KeyW" }));
54 expect(keys.has(",")).toBe(true);
55 expect(keys.has("w")).toBe(true);
56 });
57});
58
59describe("matchesKeybindingShortcutModifiers", () => {
60 const modifiers = (m: Partial<ShortcutEventLike>) => ({ ...event(""), ...m });
61 const modS = { ...shortcut("s"), modKey: true };
62
63 it("maps modKey to meta on mac and ctrl elsewhere", () => {
64 expect(matchesKeybindingShortcutModifiers(modifiers({ metaKey: true }), modS, "MacIntel")).toBe(
65 true,
66 );
67 expect(matchesKeybindingShortcutModifiers(modifiers({ ctrlKey: true }), modS, "MacIntel")).toBe(
68 false,
69 );
70 expect(matchesKeybindingShortcutModifiers(modifiers({ ctrlKey: true }), modS, "Win32")).toBe(
71 true,
72 );
73 expect(matchesKeybindingShortcutModifiers(modifiers({ metaKey: true }), modS, "Win32")).toBe(
74 false,
75 );
76 });
77
78 it("rejects an extra modifier", () => {
79 const ctrlShiftS = { ...shortcut("s"), ctrlKey: true, shiftKey: true };
80 expect(
81 matchesKeybindingShortcutModifiers(
82 modifiers({ ctrlKey: true, shiftKey: true, altKey: true }),
83 ctrlShiftS,
84 "Win32",
85 ),
86 ).toBe(false);
87 });
88});
89
90describe("matchesKeybindingShortcut", () => {
91 it("treats AltGraph text entry as typing, not a Ctrl+Alt chord, off macOS", () => {
92 const ctrlAltEuro = { ...shortcut("€"), ctrlKey: true, altKey: true };
93 const typed = event("€", { ctrlKey: true, altKey: true });
94 const altGraph = { ...typed, getModifierState: (k: "AltGraph") => k === "AltGraph" };
95
96 expect(matchesKeybindingShortcut(typed, ctrlAltEuro, "Win32")).toBe(true);
97 expect(matchesKeybindingShortcut(altGraph, ctrlAltEuro, "Win32")).toBe(false);
98 expect(matchesKeybindingShortcut(altGraph, ctrlAltEuro, "MacIntel")).toBe(true);
99 });
100});