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

commandPaletteCommands.browser.test.tsx

199 lines6.8 KB

1import { compileResolvedKeybindingsConfig } from "@t3tools/shared/keybindings";
2import { describe, expect, it, vi } from "vite-plus/test";
3import { page, userEvent } from "vite-plus/test/browser";
4​
5import { render } from "../../test/render";
6import { openCommandPalette } from "../commandPaletteBus";
7import {
8 dispatchCommandPalette,
9 useCommandPaletteMount,
10 useCommandPaletteStore,
11} from "../commandPaletteStore";
12import { useThemeEditorStore } from "../components/settings/themeEditorStore";
13import { installCommandPaletteCommands } from "./commandPaletteCommands";
14​
15const keybindings = compileResolvedKeybindingsConfig([
16 { key: "mod+k", command: "commandPalette.toggle" },
17 { key: "mod+alt+shift+t", command: "themeEditor.toggle" },
18 { key: "mod+u", command: "usage.open" },
19]);
20​
21function MountedPalette() {
22 useCommandPaletteMount();
23 return null;
25​
26function setup() {
27 useCommandPaletteStore.setState(useCommandPaletteStore.getInitialState(), true);
28 useThemeEditorStore.setState(useThemeEditorStore.getInitialState(), true);
30​
31describe("command palette shortcuts", () => {
32 it("stay inert on routes without the palette", async () => {
33 setup();
34 using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
35 await using screen = await render(null, { router: "/pair" });
36 using commands = new DisposableStack();
37 commands.defer(
38 installCommandPaletteCommands(screen.router!, { keybindings: () => keybindings }),
39 );
40​
41 await userEvent.keyboard("{Control>}k{/Control}");
42​
43 expect(useCommandPaletteStore.getState().open).toBe(false);
44 });
45​
46 it("open the palette while it is mounted, keep it open on repeat, and it closes when it unmounts", async () => {
47 setup();
48 using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
49 await using screen = await render(<MountedPalette />, { router: "/" });
50 using commands = new DisposableStack();
51 commands.defer(
52 installCommandPaletteCommands(screen.router!, { keybindings: () => keybindings }),
53 );
54​
55 await userEvent.keyboard("{Control>}k{/Control}");
56 expect(useCommandPaletteStore.getState().open).toBe(true);
57​
58 const repeatEvent = new KeyboardEvent("keydown", {
59 key: "k",
60 ctrlKey: true,
61 repeat: true,
62 bubbles: true,
63 cancelable: true,
64 });
65 document.body.dispatchEvent(repeatEvent);
66 expect(repeatEvent.defaultPrevented).toBe(true);
67 expect(useCommandPaletteStore.getState().open).toBe(true);
68​
69 await screen.rerender(<div />);
70 expect(useCommandPaletteStore.getState().open).toBe(false);
71 });
72​
73 it("keep a child search open on a composing Escape", async () => {
74 setup();
75 await using screen = await render(
76 <>
77 <MountedPalette />
78 <input aria-label="Search files" />
79 </>,
80 { router: "/" },
81 );
82 using commands = new DisposableStack();
83 commands.defer(
84 installCommandPaletteCommands(screen.router!, { keybindings: () => keybindings }),
85 );
86 dispatchCommandPalette({ _tag: "ToggleMode", mode: "files" });
87 const input = page.getByRole("textbox", { name: "Search files" });
88 await input.fill("にほん");
89​
90 for (const composition of [
91 { key: "Escape", isComposing: true, keyCode: 27 },
92 { key: "Escape", isComposing: false, keyCode: 229 },
93 ]) {
94 const event = new KeyboardEvent("keydown", {
95 ...composition,
96 bubbles: true,
97 cancelable: true,
98 });
99 input.element().dispatchEvent(event);
100 expect(event.defaultPrevented, JSON.stringify(composition)).toBe(false);
101 expect(useCommandPaletteStore.getState()).toMatchObject({ open: true, mode: "files" });
102 await expect.element(input).toHaveValue("にほん");
103 await expect.element(input).toHaveFocus();
104 }
105​
106 const escape = new KeyboardEvent("keydown", {
107 key: "Escape",
108 keyCode: 27,
109 bubbles: true,
110 cancelable: true,
111 });
112 input.element().dispatchEvent(escape);
113 expect(escape.defaultPrevented).toBe(true);
114 });
115​
116 it("keeps a child search open on a repeated Escape", async () => {
117 setup();
118 await using screen = await render(
119 <>
120 <MountedPalette />
121 <input aria-label="Search files" />
122 </>,
123 { router: "/" },
124 );
125 using commands = new DisposableStack();
126 commands.defer(
127 installCommandPaletteCommands(screen.router!, { keybindings: () => keybindings }),
128 );
129 dispatchCommandPalette({ _tag: "ToggleMode", mode: "files" });
130 const input = page.getByRole("textbox", { name: "Search files" });
131 await input.click();
132​
133 const repeatEscape = new KeyboardEvent("keydown", {
134 key: "Escape",
135 keyCode: 27,
136 repeat: true,
137 bubbles: true,
138 cancelable: true,
139 });
140 input.element().dispatchEvent(repeatEscape);
141 expect(repeatEscape.defaultPrevented).toBe(true);
142 expect(useCommandPaletteStore.getState()).toMatchObject({ open: true, mode: "files" });
143​
144 const freshEscape = new KeyboardEvent("keydown", {
145 key: "Escape",
146 keyCode: 27,
147 repeat: false,
148 bubbles: true,
149 cancelable: true,
150 });
151 input.element().dispatchEvent(freshEscape);
152 expect(freshEscape.defaultPrevented).toBe(true);
153 expect(useCommandPaletteStore.getState()).toMatchObject({ open: true, mode: "command" });
154 });
155​
156 it("ignore held theme editor and Usage shortcuts", async () => {
157 setup();
158 using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
159 await using screen = await render(<MountedPalette />, { router: "/" });
160 using commands = new DisposableStack();
161 commands.defer(
162 installCommandPaletteCommands(screen.router!, { keybindings: () => keybindings }),
163 );
164 using navigate = vi.spyOn(screen.router!, "navigate").mockResolvedValue(undefined);
165​
166 for (const keys of [
167 { key: "T", ctrlKey: true, altKey: true, shiftKey: true },
168 { key: "u", ctrlKey: true },
169 ]) {
170 const event = new KeyboardEvent("keydown", {
171 ...keys,
172 repeat: true,
173 bubbles: true,
174 cancelable: true,
175 });
176 document.body.dispatchEvent(event);
177 expect(event.defaultPrevented, keys.key).toBe(true);
178 }
179 expect(useThemeEditorStore.getState().session).toBeNull();
180 expect(navigate).not.toHaveBeenCalled();
181 });
182});
183​
184describe("openCommandPalette", () => {
185 it("drops a request made while no palette is mounted", async () => {
186 setup();
187 await using screen = await render(null, { router: "/pair" });
188​
189 openCommandPalette({ open: "new-thread-in" });
190 await screen.rerender(<MountedPalette />);
191 expect(useCommandPaletteStore.getState()).toMatchObject({ open: false, openIntent: null });
192​
193 openCommandPalette({ open: "new-thread-in" });
194 expect(useCommandPaletteStore.getState()).toMatchObject({
195 open: true,
196 openIntent: { kind: "new-thread-in" },
197 });
198 });
199});