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

sidebarTree.browser.test.ts

195 lines6.9 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import { userEvent } from "vite-plus/test/browser";
3import { installSidebarTree } from "./sidebarTree";
4​
5function setup() {
6 const root = document.createElement("div");
7 root.setAttribute("role", "tree");
8 document.body.append(root);
9 let dispose: (() => void) | undefined;
10 const row = (id: string, parent?: string) => {
11 const element = document.createElement("button");
12 element.dataset.sidebarRow = id;
13 if (parent) element.dataset.sidebarParent = parent;
14 element.setAttribute("role", "treeitem");
15 element.textContent = id;
16 root.append(element);
17 return element;
18 };
19 return {
20 root,
21 row,
22 install: () => {
23 dispose = installSidebarTree(root);
24 },
25 [Symbol.dispose]() {
26 dispose?.();
27 root.remove();
28 },
29 };
31​
32function press(target: HTMLElement, key: string, init: KeyboardEventInit = {}) {
33 const event = new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true, ...init });
34 target.dispatchEvent(event);
35 return event;
37​
38describe("sidebar keyboard navigation", () => {
39 it("moves focus without activating rows, clamps at the ends, and keeps one Tab stop", () => {
40 using tree = setup();
41 const first = tree.row("first");
42 const second = tree.row("second");
43 const last = tree.row("last");
44 const activate = vi.fn();
45 tree.root.addEventListener("click", activate);
46 tree.install();
47 first.focus();
48 press(first, "ArrowDown");
49 expect(document.activeElement).toBe(second);
50 press(second, "End");
51 expect(document.activeElement).toBe(last);
52 press(last, "ArrowDown");
53 expect(document.activeElement).toBe(last);
54 press(last, "Home");
55 expect(document.activeElement).toBe(first);
56 expect(activate).not.toHaveBeenCalled();
57 expect(tree.root.querySelectorAll('[tabindex="0"]')).toHaveLength(1);
58 });
59​
60 it("expands a group before entering its first child and returns to the parent", async () => {
61 using tree = setup();
62 const group = tree.row("project");
63 group.setAttribute("aria-expanded", "false");
64 group.addEventListener("click", () => {
65 const expanded = group.getAttribute("aria-expanded") === "true";
66 group.setAttribute("aria-expanded", String(!expanded));
67 if (expanded) tree.root.querySelector('[data-sidebar-row="thread"]')?.remove();
68 else tree.row("thread", "project");
69 });
70 tree.install();
71 group.focus();
72 press(group, "ArrowRight");
73 expect(group.getAttribute("aria-expanded")).toBe("true");
74 expect(document.activeElement).toBe(group);
75 // The tree picks up the new child from its mutation observer.
76 await vi.waitFor(() =>
77 expect(
78 tree.root.querySelector('[data-sidebar-parent="project"]')?.getAttribute("aria-level"),
79 ).toBe("2"),
80 );
81 press(group, "ArrowRight");
82 const child = document.activeElement as HTMLElement;
83 expect(child.dataset.sidebarRow).toBe("thread");
84 press(child, "ArrowLeft");
85 expect(document.activeElement).toBe(group);
86 press(group, "ArrowLeft");
87 expect(group.getAttribute("aria-expanded")).toBe("false");
88 });
89​
90 it("opens the row menu on Shift+F10 release without moving focus", () => {
91 using tree = setup();
92 tree.row("first");
93 const selected = tree.row("selected");
94 const contextMenu = vi.fn((event: MouseEvent) => event.preventDefault());
95 selected.addEventListener("contextmenu", contextMenu);
96 tree.install();
97 selected.focus();
98 expect(press(selected, "F10", { shiftKey: true }).defaultPrevented).toBe(true);
99 expect(contextMenu).not.toHaveBeenCalled();
100 selected.dispatchEvent(
101 new KeyboardEvent("keyup", { key: "F10", shiftKey: true, bubbles: true, cancelable: true }),
102 );
103 expect(contextMenu).toHaveBeenCalledOnce();
104 expect(document.activeElement).toBe(selected);
105 });
106​
107 it("lets Tab reach only the focused row's buttons", () => {
108 using tree = setup();
109 const first = tree.row("first");
110 const second = tree.row("second");
111 const action = (owner: string) => {
112 const button = document.createElement("button");
113 button.textContent = `Archive ${owner}`;
114 button.dataset.sidebarActionFor = owner;
115 tree.root.append(button);
116 return button;
117 };
118 const firstAction = action("first");
119 const secondAction = action("second");
120 tree.install();
121 first.focus();
122 expect([firstAction.tabIndex, secondAction.tabIndex]).toEqual([0, -1]);
123 press(first, "ArrowDown");
124 expect(document.activeElement).toBe(second);
125 expect([firstAction.tabIndex, secondAction.tabIndex]).toEqual([-1, 0]);
126 });
127​
128 it("keeps a rendered row under a collapsed group reachable", () => {
129 using tree = setup();
130 const shelf = tree.row("shelf:settled");
131 shelf.setAttribute("aria-expanded", "false");
132 // A collapsed shelf still renders the open thread.
133 const open = tree.row("thread", "shelf:settled");
134 tree.install();
135 shelf.focus();
136 press(shelf, "ArrowDown");
137 expect(document.activeElement).toBe(open);
138 });
139​
140 it("skips disabled rows and leaves controls outside rows in the Tab order", () => {
141 using tree = setup();
142 const first = tree.row("first");
143 const loading = tree.row("settled:more");
144 loading.disabled = true;
145 const undo = document.createElement("button");
146 undo.textContent = "Undo";
147 tree.root.append(undo);
148 tree.install();
149 first.focus();
150 press(first, "ArrowDown");
151 expect(document.activeElement).toBe(first);
152 expect(undo.tabIndex).toBe(0);
153 });
154​
155 it("gives row buttons back their own tab order when the tree reinstalls", () => {
156 using tree = setup();
157 tree.row("first");
158 const second = tree.row("second");
159 const action = (owner: string) => {
160 const button = document.createElement("button");
161 button.dataset.sidebarActionFor = owner;
162 tree.root.append(button);
163 return button;
164 };
165 const firstAction = action("first");
166 const secondAction = action("second");
167 const dispose = installSidebarTree(tree.root);
168 second.focus();
169 dispose();
170 expect([firstAction.tabIndex, secondAction.tabIndex]).toEqual([0, 0]);
171 tree.install();
172 expect([firstAction.tabIndex, secondAction.tabIndex]).toEqual([-1, 0]);
173 });
174​
175 it("does not pull focus back after a click outside the tree", async () => {
176 using tree = setup();
177 const first = tree.row("first");
178 tree.install();
179 const text = document.createElement("p");
180 text.textContent = "Chat text";
181 document.body.append(text);
182 using _text = { [Symbol.dispose]: () => text.remove() };
183 first.focus();
184 await userEvent.click(text);
185 expect(document.activeElement).toBe(document.body);
186 // A status change re-renders the tree while the user works elsewhere.
187 tree.row("second");
188 await vi.waitFor(() =>
189 expect(
190 tree.root.querySelector('[data-sidebar-row="second"]')?.getAttribute("aria-posinset"),
191 ).toBe("2"),
192 );
193 expect(document.activeElement).toBe(document.body);
194 });
195});