-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathkeyboardShortcut.test.ts
More file actions
80 lines (72 loc) · 2 KB
/
keyboardShortcut.test.ts
File metadata and controls
80 lines (72 loc) · 2 KB
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
// @vitest-environment happy-dom
import {
formatAcceleratorForDisplay,
keyboardEventToAccelerator,
} from './keyboardShortcut';
function makeKeyDown(
init: Partial<KeyboardEventInit> & { code: string },
): KeyboardEvent {
return new KeyboardEvent('keydown', {
bubbles: true,
cancelable: true,
...init,
});
}
describe('keyboardEventToAccelerator', () => {
it('returns CommandOrControl+Shift+G for meta+shift+KeyG on macOS-style event', () => {
const ev = makeKeyDown({
code: 'KeyG',
key: 'G',
metaKey: true,
shiftKey: true,
});
expect(keyboardEventToAccelerator(ev)).toBe('CommandOrControl+Shift+G');
});
it('returns CommandOrControl+Shift+Alt+G when Option/Alt is held', () => {
const ev = makeKeyDown({
code: 'KeyG',
key: 'G',
metaKey: true,
shiftKey: true,
altKey: true,
});
expect(keyboardEventToAccelerator(ev)).toBe('CommandOrControl+Shift+Alt+G');
});
it('returns null without meta or ctrl', () => {
const ev = makeKeyDown({
code: 'KeyG',
key: 'g',
shiftKey: true,
});
expect(keyboardEventToAccelerator(ev)).toBeNull();
});
it('returns null for modifier-only keydown', () => {
const ev = makeKeyDown({
code: 'ShiftLeft',
key: 'Shift',
shiftKey: true,
metaKey: true,
});
expect(keyboardEventToAccelerator(ev)).toBeNull();
});
it('maps ctrl+KeyG on Windows-style event', () => {
const ev = makeKeyDown({
code: 'KeyG',
key: 'g',
ctrlKey: true,
});
expect(keyboardEventToAccelerator(ev)).toBe('CommandOrControl+G');
});
});
describe('formatAcceleratorForDisplay', () => {
it('uses Option on mac for Alt segment', () => {
expect(
formatAcceleratorForDisplay('CommandOrControl+Shift+Alt+G', true),
).toBe('⌘·⇧·Option (⌥)·G');
});
it('uses Alt on non-mac', () => {
expect(
formatAcceleratorForDisplay('CommandOrControl+Shift+Alt+G', false),
).toBe('Ctrl+⇧+Alt+G');
});
});