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

combobox.browser.test.tsx

81 lines2.7 KB

1import { expect, it, vi } from "vite-plus/test";
2import { page, userEvent } from "vite-plus/test/browser";
3​
4import "~/index.css";
5​
6import { render } from "../../../test/render";
7import {
8 Autocomplete,
9 AutocompleteInput,
10 AutocompleteItem,
11 AutocompleteList,
12 AutocompletePopup,
13} from "./autocomplete";
14import { Combobox, ComboboxInput, ComboboxItem, ComboboxList, ComboboxPopup } from "./combobox";
15​
16const items = [
17 ...Array.from({ length: 40 }, (_, index) => `option-${index}`),
18 ...Array.from({ length: 12 }, (_, index) => `match-${index}`),
19];
20​
21const pickers = {
22 combobox: (
23 <Combobox defaultOpen items={items}>
24 <ComboboxInput aria-label="Search options" />
25 <ComboboxPopup>
26 <ComboboxList scrollAreaClassName="max-h-72">
27 {(item: string) => (
28 <ComboboxItem key={item} value={item}>
29 {item}
30 </ComboboxItem>
31 )}
32 </ComboboxList>
33 </ComboboxPopup>
34 </Combobox>
35 ),
36 autocomplete: (
37 <Autocomplete defaultOpen items={items}>
38 <AutocompleteInput aria-label="Search options" />
39 <AutocompletePopup>
40 <AutocompleteList scrollAreaClassName="max-h-72">
41 {(item: string) => (
42 <AutocompleteItem key={item} value={item}>
43 {item}
44 </AutocompleteItem>
45 )}
46 </AutocompleteList>
47 </AutocompletePopup>
48 </Autocomplete>
49 ),
50};
51​
52it.each(["combobox", "autocomplete"] as const)(
53 "resizes the capped %s scrollbar thumb when filtering shortens the list",
54 async (kind) => {
55 await using _screen = await render(pickers[kind], {
56 router: false /* filtering a popup list does not involve routing */,
57 });
58​
59 const viewport = page
60 .getByRole("listbox")
61 .element()
62 .closest("[data-slot=scroll-area-viewport]");
63 if (!viewport) throw new Error("List is not inside a scroll area");
64 await vi.waitFor(() => expect(viewport.clientHeight).toBeLessThanOrEqual(288));
65 const thumbHeight = () =>
66 document
67 .querySelector(
68 "[data-slot=scroll-area-scrollbar][data-orientation=vertical] [data-slot=scroll-area-thumb]",
69 )
70 ?.getBoundingClientRect().height ?? 0;
71 await vi.waitFor(() => expect(thumbHeight()).toBeGreaterThan(0));
72 const unfilteredThumbHeight = thumbHeight();
73​
74 await userEvent.click(page.getByRole("combobox", { name: "Search options" }));
75 await userEvent.keyboard("match");
76 await vi.waitFor(() => expect(page.getByRole("option").elements()).toHaveLength(12));
77 await vi.waitFor(() => expect(viewport.scrollHeight).toBeGreaterThan(viewport.clientHeight));
78​
79 await vi.waitFor(() => expect(thumbHeight()).toBeGreaterThan(unfilteredThumbHeight * 2));
80 },
81);