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

ProjectIconPickerDialog.browser.test.tsx

50 lines1.8 KB

1import { page, userEvent } from "vite-plus/test/browser";
2import { expect, it, vi } from "vite-plus/test";
3​
4import { act, render } from "../../../test/render";
5import { ProjectIconPickerDialog } from "./ProjectIconPickerDialog";
6​
7import "~/index.css";
8​
9it("Escape in the icon search clears it without closing the dialog", async () => {
10 const onOpenChange = vi.fn();
11 await using _screen = await render(
12 <ProjectIconPickerDialog
13 current={null}
14 projectName="Project"
15 open
16 onOpenChange={onOpenChange}
17 onSelect={() => {}}
18 />,
19 );
20 const search = page.getByRole("searchbox", { name: "Search Lucide icons" });
21 await expect.element(search).toHaveAttribute("type", "text");
22 await userEvent.fill(search, "rocket");
23 for (const composition of [
24 { key: "Escape", isComposing: true, keyCode: 27 },
25 { key: "Escape", isComposing: false, keyCode: 229 },
26 ]) {
27 // Safari ends the composition just before the Escape keydown.
28 await act(() => {
29 search.element().dispatchEvent(new CompositionEvent("compositionend", { bubbles: true }));
30 search
31 .element()
32 .dispatchEvent(new KeyboardEvent("keydown", { ...composition, bubbles: true }));
33 });
34 await expect.element(search).toHaveValue("rocket");
35 }
36​
37 await userEvent.keyboard("{Escape}");
38 await expect.element(search).toHaveValue("");
39 await expect.element(search).toHaveFocus();
40 // Holding Escape repeats the keydown, which must not also close the dialog.
41 await act(() => {
42 search
43 .element()
44 .dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", repeat: true, bubbles: true }));
45 });
46 expect(onOpenChange).not.toHaveBeenCalled();
47​
48 await userEvent.keyboard("{Escape}");
49 expect(onOpenChange.mock.calls.map(([open]) => open)).toEqual([false]);
50});