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

CommandPaletteContent.browser.test.tsx

267 lines9.1 KB

1import "~/index.css";
2​
3import { useState } from "react";
4import { describe, expect, it } from "vite-plus/test";
5import { page, userEvent } from "vite-plus/test/browser";
6​
7import { render } from "../../test/render";
8import { CommandPaletteContent } from "./CommandPaletteContent";
9import {
10 Combobox,
11 ComboboxItem,
12 ComboboxList,
13 ComboboxPopup,
14 ComboboxSearchInput,
15 ComboboxTrigger,
16} from "./ui/combobox";
17import { CommandDialog, CommandDialogPopup, CommandItem, CommandList } from "./ui/command";
18import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "./ui/select";
19​
20function LongPalette() {
21 const items = Array.from({ length: 40 }, (_, index) => `Project ${index}`);
22 return (
23 <CommandDialog open>
24 <CommandDialogPopup aria-label="Command palette">
25 <CommandPaletteContent items={items} inputProps={{ placeholder: "Search projects" }}>
26 <CommandList>
27 {(item: string) => <CommandItem value={item}>{item}</CommandItem>}
28 </CommandList>
29 </CommandPaletteContent>
30 </CommandDialogPopup>
31 </CommandDialog>
32 );
34​
35function SubmenuPalette() {
36 const [open, setOpen] = useState(false);
37 return (
38 <>
39 <button type="button" onClick={() => setOpen(true)}>
40 Open
41 </button>
42 <CommandDialog open={open} onOpenChange={setOpen}>
43 <CommandDialogPopup aria-label="Command palette">
44 <CommandPaletteContent
45 items={["Project A"]}
46 inputProps={{
47 placeholder: "Search projects",
48 startAddon: (
49 <button type="button" aria-label="Back">
50 Back
51 </button>
52 ),
53 }}
54 >
55 <CommandList>
56 {(item: string) => <CommandItem value={item}>{item}</CommandItem>}
57 </CommandList>
58 </CommandPaletteContent>
59 </CommandDialogPopup>
60 </CommandDialog>
61 </>
62 );
64​
65function DisabledInputPalette() {
66 const [open, setOpen] = useState(false);
67 return (
68 <>
69 <button type="button" onClick={() => setOpen(true)}>
70 Open
71 </button>
72 <CommandDialog open={open} onOpenChange={setOpen}>
73 <CommandDialogPopup aria-label="File picker">
74 <CommandPaletteContent
75 inputProps={{ disabled: true, placeholder: "Search files…" }}
76 mode="none"
77 value=""
78 >
79 <div>Open a project to search its files.</div>
80 </CommandPaletteContent>
81 </CommandDialogPopup>
82 </CommandDialog>
83 </>
84 );
86​
87function SwapToDisabledInputPalette() {
88 const [mode, setMode] = useState<"command" | "files">("command");
89 return (
90 // Base UI's focusout restore only fires on DOM removal in Chromium, not Firefox/WebKit.
91 <CommandDialog open disablePointerDismissal>
92 <CommandDialogPopup aria-label={mode === "command" ? "Command palette" : "File picker"}>
93 {mode === "command" ? (
94 <CommandPaletteContent
95 key="command"
96 items={["Go to file"]}
97 inputProps={{
98 placeholder: "Search commands",
99 onKeyDown: (event) => {
100 if (event.key === "p" && event.ctrlKey) {
101 event.preventDefault();
102 setMode("files");
103 }
104 },
105 }}
106 >
107 <CommandList>
108 {(item: string) => <CommandItem value={item}>{item}</CommandItem>}
109 </CommandList>
110 </CommandPaletteContent>
111 ) : (
112 <CommandPaletteContent
113 key="files"
114 inputProps={{ disabled: true, placeholder: "Search files…" }}
115 mode="none"
116 value=""
117 >
118 <div>Open a project to search its files.</div>
119 </CommandPaletteContent>
120 )}
121 </CommandDialogPopup>
122 </CommandDialog>
123 );
125​
126function PaletteOverOpenCombobox() {
127 const [paletteOpen, setPaletteOpen] = useState(false);
128 return (
129 <>
130 <Combobox items={["main"]}>
131 <ComboboxTrigger>Branch</ComboboxTrigger>
132 <ComboboxPopup>
133 <ComboboxSearchInput
134 aria-label="Search refs"
135 onKeyDown={(event) => {
136 if (event.key === "k" && event.ctrlKey) setPaletteOpen(true);
137 }}
138 />
139 <ComboboxList>
140 {(item: string) => (
141 <ComboboxItem key={item} value={item}>
142 {item}
143 </ComboboxItem>
144 )}
145 </ComboboxList>
146 </ComboboxPopup>
147 </Combobox>
148 <CommandDialog open={paletteOpen} onOpenChange={setPaletteOpen}>
149 <CommandDialogPopup aria-label="Command palette">
150 <CommandPaletteContent items={["Project A"]} inputProps={{ placeholder: "Search" }}>
151 <CommandList>
152 {(item: string) => <CommandItem value={item}>{item}</CommandItem>}
153 </CommandList>
154 </CommandPaletteContent>
155 </CommandDialogPopup>
156 </CommandDialog>
157 </>
158 );
160​
161function PaletteOverOpenSelect() {
162 const [paletteOpen, setPaletteOpen] = useState(false);
163 return (
164 <>
165 <Select defaultValue="off">
166 <SelectTrigger aria-label="Thread notifications">
167 <SelectValue />
168 </SelectTrigger>
169 <SelectPopup
170 onKeyDown={(event) => {
171 if (event.key === "k" && event.ctrlKey) setPaletteOpen(true);
172 }}
173 >
174 <SelectItem value="off">Off</SelectItem>
175 <SelectItem value="always">Always</SelectItem>
176 </SelectPopup>
177 </Select>
178 <CommandDialog open={paletteOpen} onOpenChange={setPaletteOpen}>
179 <CommandDialogPopup aria-label="Command palette">
180 <CommandPaletteContent items={["Project A"]} inputProps={{ placeholder: "Search" }}>
181 <CommandList>
182 {(item: string) => (
183 <CommandItem key={item} value={item}>
184 {item}
185 </CommandItem>
186 )}
187 </CommandList>
188 </CommandPaletteContent>
189 </CommandDialogPopup>
190 </CommandDialog>
191 </>
192 );
194​
195describe("CommandPaletteContent", () => {
196 it("returns focus to the closed popup's trigger when the palette closes with Escape", async () => {
197 await using _screen = await render(<PaletteOverOpenSelect />);
198 const trigger = page.getByRole("combobox", { name: "Thread notifications" });
199 await userEvent.click(trigger);
200 await expect.element(page.getByRole("option", { name: "Off" })).toHaveFocus();
201​
202 await userEvent.keyboard("{Control>}k{/Control}");
203 await expect.element(page.getByPlaceholder("Search")).toHaveFocus();
204​
205 await userEvent.keyboard("{Escape}");
206 await expect.element(trigger).toHaveFocus();
207 });
208​
209 it("closes a popup whose search field had focus when the palette opened from the keyboard", async () => {
210 await using _screen = await render(<PaletteOverOpenCombobox />);
211 await userEvent.click(page.getByText("Branch"));
212 await expect.element(page.getByLabelText("Search refs")).toHaveFocus();
213 await expect.element(page.getByRole("option", { name: "main" })).toBeVisible();
214​
215 await userEvent.keyboard("{Control>}k{/Control}");
216​
217 await expect.element(page.getByPlaceholder("Search")).toHaveFocus();
218 // The modal palette hides the page from role queries, so look for the picker in the DOM.
219 await expect.poll(() => document.querySelector('[data-slot="combobox-positioner"]')).toBeNull();
220 });
221​
222 it("focuses the search input, not the Back button, when it opens into a submenu", async () => {
223 await using _screen = await render(<SubmenuPalette />);
224​
225 await userEvent.click(page.getByRole("button", { name: "Open" }));
226​
227 await expect.element(page.getByPlaceholder("Search projects")).toHaveFocus();
228 });
229​
230 it("focuses the dialog popup when its search input is disabled", async () => {
231 await using _screen = await render(<DisabledInputPalette />);
232​
233 await userEvent.click(page.getByRole("button", { name: "Open" }));
234​
235 await expect.element(page.getByRole("dialog", { name: "File picker" })).toHaveFocus();
236 });
237​
238 it("focuses the dialog popup when an open palette swaps to a disabled search input", async () => {
239 await using _screen = await render(<SwapToDisabledInputPalette />);
240 await expect.element(page.getByPlaceholder("Search commands")).toHaveFocus();
241​
242 await userEvent.keyboard("{Control>}p{/Control}");
243​
244 await expect.element(page.getByRole("dialog", { name: "File picker" })).toHaveFocus();
245 });
246​
247 it.each([
248 { height: 900, top: 225 },
249 { height: 620, top: 138 },
250 ])("sits low without shrinking in a $height px tall window", async ({ height, top }) => {
251 const viewport = { width: window.innerWidth, height: window.innerHeight };
252 await page.viewport(1440, height);
253 await using _viewport = {
254 [Symbol.asyncDispose]: () => page.viewport(viewport.width, viewport.height),
255 };
256 await using _screen = await render(<LongPalette />);
257​
258 const popup = page.getByRole("dialog", { name: "Command palette" });
259 await expect.element(popup).toBeVisible();
260 await expect
261 .poll(() => {
262 const bounds = popup.element().getBoundingClientRect();
263 return { top: Math.round(bounds.top), height: Math.round(bounds.height) };
264 })
265 .toEqual({ top, height: 420 });
266 });
267});