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

routeReveal.browser.test.tsx

212 lines8.4 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import { page, userEvent } from "vite-plus/test/browser";
3​
4import { render } from "../../../test/render";
5import { Sidebar, SidebarProvider, SidebarTrigger, useSidebar } from "../ui/sidebar";
6import { type SidebarRouteReveal, useActiveRowReveal } from "./routeReveal";
7​
8const ROW_HEIGHT_PX = 24;
9​
10function RevealList(props: {
11 readonly reveal: SidebarRouteReveal;
12 readonly rowKeys: ReadonlyArray<string>;
13 readonly activeKey: string | null;
14 readonly inPlace?: boolean;
15}) {
16 const revealActiveRow = useActiveRowReveal(props.reveal, props.activeKey, props.inPlace);
17 return (
18 <ul aria-label="Threads" style={{ height: ROW_HEIGHT_PX * 4, overflowY: "auto", margin: 0 }}>
19 {props.rowKeys.map((key) => (
20 <li
21 key={key}
22 ref={key === props.activeKey ? (row) => revealActiveRow(row, key) : undefined}
23 tabIndex={-1}
24 style={{ height: ROW_HEIGHT_PX }}
25 >
26 {key}
27 </li>
28 ))}
29 </ul>
30 );
32​
33// Reads the sidebar context like the real thread list, so opening the sheet re-renders it.
34function SheetRevealList(props: Parameters<typeof RevealList>[0]) {
35 useSidebar();
36 return <RevealList {...props} />;
38​
39const rowKeys = (count: number) => Array.from({ length: count }, (_, index) => `thread-${index}`);
40const newReveal = (): SidebarRouteReveal => ({ observedKey: null, pendingKey: null });
41​
42describe("useActiveRowReveal", () => {
43 it("scrolls an off-screen row into view when the route opens its thread", async () => {
44 const reveal = newReveal();
45 const keys = rowKeys(50);
46 await using screen = await render(
47 <RevealList reveal={reveal} rowKeys={keys} activeKey="thread-0" />,
48 // No router: the route arrives as the activeKey prop.
49 { router: false },
50 );
51 const target = screen.getByText("thread-40", { exact: true });
52 await expect.element(target).not.toBeInViewport();
53​
54 await screen.rerender(<RevealList reveal={reveal} rowKeys={keys} activeKey="thread-40" />);
55​
56 await expect.element(target).toBeInViewport();
57 });
58​
59 it("leaves the list where it is when the user clicked the row that opened the thread", async () => {
60 const reveal = newReveal();
61 const keys = rowKeys(50);
62 await using screen = await render(
63 <RevealList reveal={reveal} rowKeys={keys} activeKey="thread-0" />,
64 // No router: the route arrives as the activeKey prop.
65 { router: false },
66 );
67 await userEvent.hover(screen.getByText("thread-2", { exact: true }));
68 // Even a no-op scroll forces a layout of the thread being opened.
69 using scrollIntoView = vi.spyOn(Element.prototype, "scrollIntoView");
70​
71 await screen.rerender(<RevealList reveal={reveal} rowKeys={keys} activeKey="thread-2" />);
72​
73 await vi.waitFor(() => expect(reveal.pendingKey).toBeNull());
74 expect(scrollIntoView).not.toHaveBeenCalled();
75 });
76​
77 it("leaves the list where it is when the open draft's first send lists its thread", async () => {
78 const reveal = newReveal();
79 await using screen = await render(
80 <RevealList reveal={reveal} rowKeys={rowKeys(40)} activeKey="thread-0" />,
81 // No router: the route arrives as the activeKey prop.
82 { router: false },
83 );
84 // A new draft names no thread until it is sent.
85 await screen.rerender(<RevealList reveal={reveal} rowKeys={rowKeys(40)} activeKey={null} />);
86​
87 await screen.rerender(
88 <RevealList reveal={reveal} rowKeys={rowKeys(50)} activeKey="thread-45" inPlace />,
89 );
90​
91 await vi.waitFor(() => expect(reveal.pendingKey).toBeNull());
92 await expect.element(screen.getByText("thread-45", { exact: true })).not.toBeInViewport();
93 });
94​
95 it("reveals a focused row scrolled out of view when the keyboard opens its thread", async () => {
96 const reveal = newReveal();
97 const keys = rowKeys(50);
98 await using screen = await render(
99 <RevealList reveal={reveal} rowKeys={keys} activeKey="thread-0" />,
100 // No router: the route arrives as the activeKey prop.
101 { router: false },
102 );
103 const target = screen.getByText("thread-40", { exact: true });
104 target.element().focus({ preventScroll: true });
105 await expect.element(target).not.toBeInViewport();
106​
107 await screen.rerender(<RevealList reveal={reveal} rowKeys={keys} activeKey="thread-40" />);
108​
109 await expect.element(target).toBeInViewport();
110 });
111​
112 it("reveals the active row when it mounts after the shells arrive", async () => {
113 const reveal = newReveal();
114 // The route already names the thread, but its environment's shells have not
115 // listed it yet.
116 await using screen = await render(
117 <RevealList reveal={reveal} rowKeys={rowKeys(5)} activeKey="thread-40" />,
118 // No router: the route arrives as the activeKey prop.
119 { router: false },
120 );
121 await expect.element(screen.getByText("thread-4", { exact: true })).toBeInTheDocument();
122​
123 await screen.rerender(
124 <RevealList reveal={reveal} rowKeys={rowKeys(50)} activeKey="thread-40" />,
125 );
126​
127 await expect.element(screen.getByText("thread-40", { exact: true })).toBeInViewport();
128 });
129​
130 it("reveals a thread reopened after a thread whose row never mounted", async () => {
131 const reveal = newReveal();
132 const keys = rowKeys(50);
133 await using screen = await render(
134 <RevealList reveal={reveal} rowKeys={keys} activeKey="thread-40" />,
135 // No router: the route arrives as the activeKey prop.
136 { router: false },
137 );
138 const target = screen.getByText("thread-40", { exact: true });
139 await expect.element(target).toBeInViewport();
140 screen.getByRole("list", { name: "Threads" }).element().scrollTop = 0;
141 await expect.element(target).not.toBeInViewport();
142​
143 // The route moves to a thread the list does not hold yet, then back.
144 await screen.rerender(<RevealList reveal={reveal} rowKeys={keys} activeKey="thread-99" />);
145 await screen.rerender(<RevealList reveal={reveal} rowKeys={keys} activeKey="thread-40" />);
146​
147 await expect.element(target).toBeInViewport();
148 });
149​
150 it("reveals the open thread again when the sidebar remounts on the same route", async () => {
151 const reveal = newReveal();
152 const keys = rowKeys(50);
153 {
154 await using screen = await render(
155 <RevealList reveal={reveal} rowKeys={keys} activeKey="thread-40" />,
156 // No router: the route arrives as the activeKey prop.
157 { router: false },
158 );
159 await expect.element(screen.getByText("thread-40", { exact: true })).toBeInViewport();
160 }
161​
162 // A remount starts the list at the top; the record outlives it with no reveal pending.
163 await using screen = await render(
164 <RevealList reveal={reveal} rowKeys={keys} activeKey="thread-40" />,
165 // No router: the route arrives as the activeKey prop.
166 { router: false },
167 );
168 await expect.element(screen.getByText("thread-40", { exact: true })).toBeInViewport();
169 });
170​
171 it("stops watching a hidden active row once it unmounts", async () => {
172 const reveal = newReveal();
173 using observe = vi.spyOn(ResizeObserver.prototype, "observe");
174 using unobserve = vi.spyOn(ResizeObserver.prototype, "unobserve");
175 using disconnect = vi.spyOn(ResizeObserver.prototype, "disconnect");
176 await using screen = await render(
177 <div hidden>
178 <RevealList reveal={reveal} rowKeys={rowKeys(50)} activeKey="thread-40" />
179 </div>,
180 // No router: the route arrives as the activeKey prop.
181 { router: false },
182 );
183 const row = screen.getByText("thread-40", { exact: true }).element();
184 expect(observe).toHaveBeenCalledWith(row);
185​
186 // A widening viewport unmounts the closed sheet before it ever opens.
187 await screen.rerender(<div hidden />);
188​
189 expect(
190 unobserve.mock.calls.some(([target]) => target === row) || disconnect.mock.calls.length > 0,
191 ).toBe(true);
192 });
193​
194 it("reveals the open thread when the closed narrow sheet opens", async () => {
195 await page.viewport(390, 844);
196 const reveal = newReveal();
197 await using screen = await render(
198 <SidebarProvider>
199 <Sidebar>
200 <SheetRevealList reveal={reveal} rowKeys={rowKeys(50)} activeKey="thread-40" />
201 </Sidebar>
202 <SidebarTrigger />
203 </SidebarProvider>,
204 // No router: the route arrives as the activeKey prop.
205 { router: false },
206 );
207​
208 await userEvent.click(screen.getByRole("button", { name: "Toggle Sidebar" }));
209​
210 await expect.element(page.getByText("thread-40", { exact: true })).toBeInViewport();
211 });
212});