Open History to see the last commit
1import { expect, it } from "vite-plus/test";
2import { page, userEvent } from "vite-plus/test/browser";
3
4import { render } from "../../../test/render";
5import { Combobox, ComboboxSearchInput } from "./combobox";
6import { Menu, MenuItem, MenuPopup, MenuSearchInput, MenuTrigger } from "./menu";
7
8it.each(["combobox", "menu"] as const)(
9 "focuses the %s search when clicking anywhere in its row",
10 async (kind) => {
11 await using screen = await render(
12 <div className="w-96" data-testid="search-row">
13 {kind === "combobox" ? (
14 <Combobox>
15 <ComboboxSearchInput aria-label="Search options" />
16 </Combobox>
17 ) : (
18 <MenuSearchInput aria-label="Search options" />
19 )}
20 </div>,
21 { router: false /* input focus does not involve navigation */ },
22 );
23 const row = screen.getByTestId("search-row");
24 const field = screen.getByRole(kind === "combobox" ? "combobox" : "textbox", {
25 name: "Search options",
26 });
27 const { width, height } = row.element().getBoundingClientRect();
28
29 for (const x of [4, width / 2, width - 20, width - 4]) {
30 field.element().blur();
31 await userEvent.click(row, { position: { x, y: height / 2 } });
32 await expect.element(field).toHaveFocus();
33 }
34 await userEvent.keyboard("project");
35 await expect.element(field).toHaveValue("project");
36 },
37);
38
39it("moves focus from MenuSearchInput to the last item on ArrowUp", async () => {
40 await using _screen = await render(
41 <Menu defaultOpen>
42 <MenuTrigger>Filter</MenuTrigger>
43 <MenuPopup>
44 <MenuSearchInput aria-label="Search options" />
45 <MenuItem>Alpha</MenuItem>
46 <MenuItem>Gamma</MenuItem>
47 </MenuPopup>
48 </Menu>,
49 { router: false /* menu keyboard navigation does not involve routing */ },
50 );
51
52 await userEvent.click(page.getByRole("textbox", { name: "Search options" }));
53 await userEvent.keyboard("{ArrowUp}");
54
55 await expect.element(page.getByRole("menuitem", { name: "Gamma" })).toHaveFocus();
56});