Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

keybindingMatching.test.ts

112 lines3.9 KB

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("matches Shift+i on US and Turkish Q without losing the dotted key or apostrophe", () => {
92 const us = event("I", { code: "KeyI", shiftKey: true });
93 const turkish = event("İ", { code: "Quote", shiftKey: true });
94 const matches = (input: ShortcutEventLike, key: string) =>
95 matchesKeybindingShortcut(input, { ...shortcut(key), shiftKey: true }, "Linux x86_64");
96​
97 expect(matches(us, "i")).toBe(true);
98 expect(matches(turkish, "i\u0307")).toBe(true);
99 expect(matches(turkish, "'")).toBe(true);
100 expect(matches(turkish, "i")).toBe(true);
101 });
102​
103 it("treats AltGraph text entry as typing, not a Ctrl+Alt chord, off macOS", () => {
104 const ctrlAltEuro = { ...shortcut("€"), ctrlKey: true, altKey: true };
105 const typed = event("€", { ctrlKey: true, altKey: true });
106 const altGraph = { ...typed, getModifierState: (k: "AltGraph") => k === "AltGraph" };
107​
108 expect(matchesKeybindingShortcut(typed, ctrlAltEuro, "Win32")).toBe(true);
109 expect(matchesKeybindingShortcut(altGraph, ctrlAltEuro, "Win32")).toBe(false);
110 expect(matchesKeybindingShortcut(altGraph, ctrlAltEuro, "MacIntel")).toBe(true);
111 });
112});