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

SettingsSidebarNav.composition.browser.test.tsx

114 lines4.7 KB

1import { expect, it, vi } from "vite-plus/test";
2import { page, userEvent } from "vite-plus/test/browser";
3​
4import { act, render } from "../../../test/render";
5import { SidebarProvider } from "../ui/sidebar";
6import { SettingsSidebarNav } from "./SettingsSidebarNav";
7import { settingsNavGroups, SETTINGS_SECTION_LABELS } from "./settingsSearch";
8​
9vi.mock("./useAvailableSettingsSearchItems", () => ({
10 useAvailableSettingsSearchItems: () => [
11 { id: "theme", title: "Theme", to: "/settings/appearance" },
12 ],
13 useHasSecretProvidersEnvironment: () => false,
14}));
15​
16it("ignores composing keys in the search", async () => {
17 await using screen = await render(
18 <SidebarProvider defaultOpen>
19 <SettingsSidebarNav pathname="/settings/general" />
20 </SidebarProvider>,
21 { router: "/settings/general" },
22 );
23 const search = page.getByRole("combobox", { name: "Search settings" });
24 await search.fill("theme");
25 await expect.element(page.getByRole("option", { name: /Theme/ })).toBeVisible();
26​
27 for (const composition of [
28 { key: "Enter", isComposing: true, keyCode: 13 },
29 { key: "Enter", isComposing: false, keyCode: 229 },
30 { key: "Escape", isComposing: true, keyCode: 27 },
31 { key: "Escape", isComposing: false, keyCode: 229 },
32 { key: "ArrowDown", isComposing: true, keyCode: 40 },
33 { key: "ArrowUp", isComposing: true, keyCode: 38 },
34 ]) {
35 const event = new KeyboardEvent("keydown", { ...composition, bubbles: true, cancelable: true });
36 await act(() => search.element().dispatchEvent(event));
37 expect(event.defaultPrevented, JSON.stringify(composition)).toBe(false);
38 expect(screen.router?.state.location.pathname).toBe("/settings/general");
39 await expect.element(search).toHaveValue("theme");
40 }
41​
42 await userEvent.keyboard("{Enter}");
43 await vi.waitFor(() =>
44 expect(screen.router?.state.location.pathname).toBe("/settings/appearance"),
45 );
46});
47​
48function renderNav() {
49 return render(
50 <SidebarProvider defaultOpen>
51 <SettingsSidebarNav pathname="/settings/general" />
52 </SidebarProvider>,
53 { router: "/settings/general" },
54 );
56​
57it("opens search from the physical Slash and F keys on non-US layouts", async () => {
58 await using _screen = await renderNav();
59 const search = page.getByRole("combobox", { name: "Search settings" });
60​
61 for (const { init, opens } of [
62 { init: { key: "/", code: "Digit7", shiftKey: true }, opens: true }, // German Shift+7
63 { init: { key: ".", code: "Slash" }, opens: true }, // Russian Slash key
64 { init: { key: "а", code: "KeyF", ctrlKey: true }, opens: true }, // Russian Ctrl+F
65 { init: { key: "?", code: "Slash", shiftKey: true }, opens: false },
66 ]) {
67 if (document.activeElement instanceof HTMLElement) document.activeElement.blur();
68 const event = new KeyboardEvent("keydown", { ...init, bubbles: true, cancelable: true });
69 await act(() => document.body.dispatchEvent(event));
70 expect(event.defaultPrevented, JSON.stringify(init)).toBe(opens);
71 if (opens) await expect.element(search).toHaveFocus();
72 }
73});
74​
75it("moves Tab through every settings section", async () => {
76 await page.viewport(1280, 800);
77 await using _screen = await renderNav();
78 await userEvent.click(page.getByRole("combobox", { name: "Search settings" }));
79​
80 const sections = settingsNavGroups(false)
81 .flatMap((group) => group.paths)
82 .map((to) => SETTINGS_SECTION_LABELS[to]);
83 const tabbed: string[] = [];
84 for (let step = 0; step < sections.length; step += 1) {
85 await userEvent.tab();
86 const section = document.activeElement?.closest("[data-settings-section]");
87 if (!section) break;
88 tabbed.push(section.textContent ?? "");
89 }
90 expect(tabbed).toEqual(sections);
91});
92​
93it.each([1280, 390])("names each group by a heading outside it at %ipx wide", async (width) => {
94 await page.viewport(width, 800);
95 await using _screen = await renderNav();
96​
97 for (const name of ["Agents", "Code", "Connections"]) {
98 const heading = page.getByRole("heading", { name, level: 3 });
99 await expect.element(heading).toBeInTheDocument();
100 const group = page.getByRole("group", { name }).element();
101 expect(group.getAttribute("aria-labelledby")).toBe(heading.element().id);
102 expect(group.contains(heading.element()), `the ${name} heading is inside its group`).toBe(
103 false,
104 );
105 }
106 const groups = page.getByRole("group").elements();
107 expect(groups).toHaveLength(settingsNavGroups(false).length);
108 for (const group of groups) {
109 const first = group.querySelector("[data-settings-section]")?.textContent;
110 const heading = document.getElementById(group.getAttribute("aria-labelledby") ?? "");
111 expect(heading?.tagName, `the group starting at ${first} has no heading`).toBe("H3");
112 }
113 expect(page.getByRole("tree").query()).toBeNull();
114});