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

PreviewKeyboard.test.ts

97 lines2.8 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { makePreviewAutomationKeySequence } from "./PreviewKeyboard.ts";
4​
5describe("preview keyboard packets", () => {
6 it("includes the Chromium virtual key code and Enter text", () => {
7 expect(makePreviewAutomationKeySequence({ key: "Enter" })).toEqual({
8 keyDown: {
9 type: "keyDown",
10 key: "Enter",
11 code: "Enter",
12 modifiers: 0,
13 windowsVirtualKeyCode: 13,
14 location: 0,
15 isKeypad: false,
16 text: "\r",
17 unmodifiedText: "\r",
18 },
19 keyUp: {
20 type: "keyUp",
21 key: "Enter",
22 code: "Enter",
23 modifiers: 0,
24 windowsVirtualKeyCode: 13,
25 location: 0,
26 isKeypad: false,
27 },
28 signal: { kind: "key", key: "Enter", code: "Enter" },
29 });
30 });
31​
32 it("dispatches printable keys as text key-down events", () => {
33 const sequence = makePreviewAutomationKeySequence({ key: "z" });
34 expect(sequence.keyDown).toMatchObject({
35 type: "keyDown",
36 key: "z",
37 code: "KeyZ",
38 windowsVirtualKeyCode: 90,
39 text: "z",
40 });
41 expect(sequence.keyUp).not.toHaveProperty("text");
42 });
43​
44 it("suppresses text and uses raw key-down for shortcuts", () => {
45 expect(
46 makePreviewAutomationKeySequence({ key: "a", modifiers: ["Meta"] }, { isMac: true }).keyDown,
47 ).toEqual({
48 type: "rawKeyDown",
49 key: "a",
50 code: "KeyA",
51 modifiers: 4,
52 windowsVirtualKeyCode: 65,
53 location: 0,
54 isKeypad: false,
55 commands: ["selectAll"],
56 });
57 });
58​
59 it("maps common macOS editing shortcuts without changing other platforms", () => {
60 expect(
61 makePreviewAutomationKeySequence({ key: "z", modifiers: ["Shift", "Meta"] }, { isMac: true })
62 .keyDown.commands,
63 ).toEqual(["redo"]);
64 expect(
65 makePreviewAutomationKeySequence({ key: "a", modifiers: ["Meta"] }).keyDown,
66 ).not.toHaveProperty("commands");
67 });
68​
69 it("resolves shifted printable keys to their browser values", () => {
70 const sequence = makePreviewAutomationKeySequence({ key: "1", modifiers: ["Shift"] });
71 expect(sequence.keyDown).toMatchObject({
72 key: "!",
73 code: "Digit1",
74 modifiers: 8,
75 windowsVirtualKeyCode: 49,
76 text: "!",
77 });
78 expect(sequence.signal).toEqual({ kind: "key", key: "!", code: "Digit1" });
79 });
80​
81 it("keeps shifted key values while suppressing text for modified chords", () => {
82 const sequence = makePreviewAutomationKeySequence({
83 key: "1",
84 modifiers: ["Control", "Shift"],
85 });
86 expect(sequence.keyDown).toEqual({
87 type: "rawKeyDown",
88 key: "!",
89 code: "Digit1",
90 modifiers: 10,
91 windowsVirtualKeyCode: 49,
92 location: 0,
93 isKeypad: false,
94 });
95 expect(sequence.signal).toEqual({ kind: "key", key: "!", code: "Digit1" });
96 });
97});