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

routeReveal.browser.test.tsx

144 lines5.7 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import { userEvent } from "vite-plus/test/browser";
3​
4import { render } from "../../../test/render";
5import { type SidebarRouteReveal, useActiveRowReveal } from "./routeReveal";
6​
7const ROW_HEIGHT_PX = 24;
8​
9function RevealList(props: {
10 readonly reveal: SidebarRouteReveal;
11 readonly rowKeys: ReadonlyArray<string>;
12 readonly activeKey: string | null;
13}) {
14 const revealActiveRow = useActiveRowReveal(props.reveal, props.activeKey);
15 return (
16 <ul aria-label="Threads" style={{ height: ROW_HEIGHT_PX * 4, overflowY: "auto", margin: 0 }}>
17 {props.rowKeys.map((key) => (
18 <li
19 key={key}
20 ref={key === props.activeKey ? (row) => revealActiveRow(row, key) : undefined}
21 tabIndex={-1}
22 style={{ height: ROW_HEIGHT_PX }}
23 >
24 {key}
25 </li>
26 ))}
27 </ul>
28 );
30​
31const rowKeys = (count: number) => Array.from({ length: count }, (_, index) => `thread-${index}`);
32const newReveal = (): SidebarRouteReveal => ({ observedKey: null, pendingKey: null });
33​
34describe("useActiveRowReveal", () => {
35 it("scrolls an off-screen row into view when the route opens its thread", async () => {
36 const reveal = newReveal();
37 const keys = rowKeys(50);
38 await using screen = await render(
39 <RevealList reveal={reveal} rowKeys={keys} activeKey="thread-0" />,
40 // No router: the route arrives as the activeKey prop.
41 { router: false },
42 );
43 const target = screen.getByText("thread-40", { exact: true });
44 await expect.element(target).not.toBeInViewport();
45​
46 await screen.rerender(<RevealList reveal={reveal} rowKeys={keys} activeKey="thread-40" />);
47​
48 await expect.element(target).toBeInViewport();
49 });
50​
51 it("leaves the list where it is when the user clicked the row that opened the thread", async () => {
52 const reveal = newReveal();
53 const keys = rowKeys(50);
54 await using screen = await render(
55 <RevealList reveal={reveal} rowKeys={keys} activeKey="thread-0" />,
56 // No router: the route arrives as the activeKey prop.
57 { router: false },
58 );
59 await userEvent.hover(screen.getByText("thread-2", { exact: true }));
60 // Even a no-op scroll forces a layout of the thread being opened.
61 using scrollIntoView = vi.spyOn(Element.prototype, "scrollIntoView");
62​
63 await screen.rerender(<RevealList reveal={reveal} rowKeys={keys} activeKey="thread-2" />);
64​
65 await vi.waitFor(() => expect(reveal.pendingKey).toBeNull());
66 expect(scrollIntoView).not.toHaveBeenCalled();
67 });
68​
69 it("reveals a focused row scrolled out of view when the keyboard opens its thread", async () => {
70 const reveal = newReveal();
71 const keys = rowKeys(50);
72 await using screen = await render(
73 <RevealList reveal={reveal} rowKeys={keys} activeKey="thread-0" />,
74 // No router: the route arrives as the activeKey prop.
75 { router: false },
76 );
77 const target = screen.getByText("thread-40", { exact: true });
78 target.element().focus({ preventScroll: true });
79 await expect.element(target).not.toBeInViewport();
80​
81 await screen.rerender(<RevealList reveal={reveal} rowKeys={keys} activeKey="thread-40" />);
82​
83 await expect.element(target).toBeInViewport();
84 });
85​
86 it("reveals the active row when it mounts after the shells arrive", async () => {
87 const reveal = newReveal();
88 // The route already names the thread, but its environment's shells have not
89 // listed it yet.
90 await using screen = await render(
91 <RevealList reveal={reveal} rowKeys={rowKeys(5)} activeKey="thread-40" />,
92 // No router: the route arrives as the activeKey prop.
93 { router: false },
94 );
95 await expect.element(screen.getByText("thread-4", { exact: true })).toBeInTheDocument();
96​
97 await screen.rerender(
98 <RevealList reveal={reveal} rowKeys={rowKeys(50)} activeKey="thread-40" />,
99 );
100​
101 await expect.element(screen.getByText("thread-40", { exact: true })).toBeInViewport();
102 });
103​
104 it("reveals a thread reopened after a thread whose row never mounted", async () => {
105 const reveal = newReveal();
106 const keys = rowKeys(50);
107 await using screen = await render(
108 <RevealList reveal={reveal} rowKeys={keys} activeKey="thread-40" />,
109 // No router: the route arrives as the activeKey prop.
110 { router: false },
111 );
112 const target = screen.getByText("thread-40", { exact: true });
113 await expect.element(target).toBeInViewport();
114 screen.getByRole("list", { name: "Threads" }).element().scrollTop = 0;
115 await expect.element(target).not.toBeInViewport();
116​
117 // The route moves to a thread the list does not hold yet, then back.
118 await screen.rerender(<RevealList reveal={reveal} rowKeys={keys} activeKey="thread-99" />);
119 await screen.rerender(<RevealList reveal={reveal} rowKeys={keys} activeKey="thread-40" />);
120​
121 await expect.element(target).toBeInViewport();
122 });
123​
124 it("reveals the open thread again when the sidebar remounts on the same route", async () => {
125 const reveal = newReveal();
126 const keys = rowKeys(50);
127 {
128 await using screen = await render(
129 <RevealList reveal={reveal} rowKeys={keys} activeKey="thread-40" />,
130 // No router: the route arrives as the activeKey prop.
131 { router: false },
132 );
133 await expect.element(screen.getByText("thread-40", { exact: true })).toBeInViewport();
134 }
135​
136 // A remount starts the list at the top; the record outlives it with no reveal pending.
137 await using screen = await render(
138 <RevealList reveal={reveal} rowKeys={keys} activeKey="thread-40" />,
139 // No router: the route arrives as the activeKey prop.
140 { router: false },
141 );
142 await expect.element(screen.getByText("thread-40", { exact: true })).toBeInViewport();
143 });
144});