Open History to see the last commit
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;
24}
25
26function setup() {
27 useCommandPaletteStore.setState(useCommandPaletteStore.getInitialState(), true);
28 useThemeEditorStore.setState(useThemeEditorStore.getInitialState(), true);
29}
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("ignore composing keydowns, keeping a child search open", async () => {
74 setup();
75 using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
76 await using screen = await render(
77 <>
78 <MountedPalette />
79 <input aria-label="Search files" />
80 </>,
81 { router: "/" },
82 );
83 using commands = new DisposableStack();
84 commands.defer(
85 installCommandPaletteCommands(screen.router!, { keybindings: () => keybindings }),
86 );
87 dispatchCommandPalette({ _tag: "ToggleMode", mode: "files" });
88 const input = page.getByRole("textbox", { name: "Search files" });
89 await input.fill("にほん");
90
91 for (const composition of [
92 { key: "Escape", isComposing: true, keyCode: 27 },
93 { key: "Escape", isComposing: false, keyCode: 229 },
94 { key: "k", ctrlKey: true, isComposing: true, keyCode: 75 },
95 { key: "k", ctrlKey: true, isComposing: false, keyCode: 229 },
96 ]) {
97 const event = new KeyboardEvent("keydown", {
98 ...composition,
99 bubbles: true,
100 cancelable: true,
101 });
102 input.element().dispatchEvent(event);
103 expect(event.defaultPrevented, JSON.stringify(composition)).toBe(false);
104 expect(useCommandPaletteStore.getState()).toMatchObject({ open: true, mode: "files" });
105 await expect.element(input).toHaveValue("にほん");
106 await expect.element(input).toHaveFocus();
107 }
108
109 const escape = new KeyboardEvent("keydown", {
110 key: "Escape",
111 keyCode: 27,
112 bubbles: true,
113 cancelable: true,
114 });
115 input.element().dispatchEvent(escape);
116 expect(escape.defaultPrevented).toBe(true);
117 });
118
119 it("keeps a child search open on a repeated Escape", async () => {
120 setup();
121 await using screen = await render(
122 <>
123 <MountedPalette />
124 <input aria-label="Search files" />
125 </>,
126 { router: "/" },
127 );
128 using commands = new DisposableStack();
129 commands.defer(
130 installCommandPaletteCommands(screen.router!, { keybindings: () => keybindings }),
131 );
132 dispatchCommandPalette({ _tag: "ToggleMode", mode: "files" });
133 const input = page.getByRole("textbox", { name: "Search files" });
134 await input.click();
135
136 const repeatEscape = new KeyboardEvent("keydown", {
137 key: "Escape",
138 keyCode: 27,
139 repeat: true,
140 bubbles: true,
141 cancelable: true,
142 });
143 input.element().dispatchEvent(repeatEscape);
144 expect(repeatEscape.defaultPrevented).toBe(true);
145 expect(useCommandPaletteStore.getState()).toMatchObject({ open: true, mode: "files" });
146
147 const freshEscape = new KeyboardEvent("keydown", {
148 key: "Escape",
149 keyCode: 27,
150 repeat: false,
151 bubbles: true,
152 cancelable: true,
153 });
154 input.element().dispatchEvent(freshEscape);
155 expect(freshEscape.defaultPrevented).toBe(true);
156 expect(useCommandPaletteStore.getState()).toMatchObject({ open: true, mode: "command" });
157 });
158
159 it("ignore held theme editor and Usage shortcuts", async () => {
160 setup();
161 using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
162 await using screen = await render(<MountedPalette />, { router: "/" });
163 using commands = new DisposableStack();
164 commands.defer(
165 installCommandPaletteCommands(screen.router!, { keybindings: () => keybindings }),
166 );
167 using navigate = vi.spyOn(screen.router!, "navigate").mockResolvedValue(undefined);
168
169 for (const keys of [
170 { key: "T", ctrlKey: true, altKey: true, shiftKey: true },
171 { key: "u", ctrlKey: true },
172 ]) {
173 const event = new KeyboardEvent("keydown", {
174 ...keys,
175 repeat: true,
176 bubbles: true,
177 cancelable: true,
178 });
179 document.body.dispatchEvent(event);
180 expect(event.defaultPrevented, keys.key).toBe(true);
181 }
182 expect(useThemeEditorStore.getState().session).toBeNull();
183 expect(navigate).not.toHaveBeenCalled();
184 });
185});
186
187describe("openCommandPalette", () => {
188 it("drops a request made while no palette is mounted", async () => {
189 setup();
190 await using screen = await render(null, { router: "/pair" });
191
192 openCommandPalette({ open: "new-thread-in" });
193 await screen.rerender(<MountedPalette />);
194 expect(useCommandPaletteStore.getState()).toMatchObject({ open: false, openIntent: null });
195
196 openCommandPalette({ open: "new-thread-in" });
197 expect(useCommandPaletteStore.getState()).toMatchObject({
198 open: true,
199 openIntent: { kind: "new-thread-in" },
200 });
201 });
202});