1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
/*** 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);
});
|