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

Sidebar.motion.test.ts

339 lines11.1 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2​
3import { createSidebarListMotion } from "./Sidebar.motion";
4​
5class TestAnimation {
6 progress: number | null = 0;
7 playState: AnimationPlayState = "running";
8 effect = { getComputedTiming: () => ({ progress: this.progress }) };
9 cancel = vi.fn(() => {
10 this.playState = "idle";
11 });
12 private onFinish: (() => void) | undefined;
13 addEventListener(_type: string, listener: () => void) {
14 this.onFinish = listener;
15 }
16 finish() {
17 this.playState = "finished";
18 this.onFinish?.();
19 }
21​
22class TestRow {
23 offsetTop = 0;
24 offsetLeft = 4;
25 offsetWidth = 260;
26 namespaceURI = "http://www.w3.org/1999/xhtml";
27 dragTranslate = 0;
28 style: Record<string, string> = {};
29 inert = false;
30 attributes: { name: string; value: string }[] = [];
31 children: TestRow[] = [];
32 clones: TestRow[] = [];
33 remove = vi.fn();
34 animations: TestAnimation[] = [];
35 constructor(
36 readonly name: string,
37 public offsetHeight = 82,
38 ) {}
39 getBoundingClientRect() {
40 return { top: this.offsetTop + this.dragTranslate, height: this.offsetHeight };
41 }
42 setAttribute(name: string, value: string) {
43 this.removeAttribute(name);
44 this.attributes.push({ name, value });
45 }
46 removeAttribute(name: string) {
47 this.attributes = this.attributes.filter((attribute) => attribute.name !== name);
48 }
49 querySelectorAll(_selector: string): TestRow[] {
50 return this.children.flatMap((child) => [child, ...child.querySelectorAll("*")]);
51 }
52 cloneNode(_deep: boolean): TestRow {
53 const clone = new TestRow(`${this.name} clone`, this.offsetHeight);
54 clone.namespaceURI = this.namespaceURI;
55 clone.style = { ...this.style };
56 clone.attributes = this.attributes.map((attribute) => ({ ...attribute }));
57 clone.children = this.children.map((child) => child.cloneNode(true));
58 this.clones.push(clone);
59 return clone;
60 }
61 animate = vi.fn((_frames: Keyframe[], _options: KeyframeAnimationOptions) => {
62 const animation = new TestAnimation();
63 this.animations.push(animation);
64 return animation;
65 });
67​
68function fixture(rows: TestRow[]) {
69 const media = { matches: false };
70 const parent = {
71 children: rows,
72 ownerDocument: { defaultView: { matchMedia: () => media } },
73 append(node: TestRow) {
74 parent.children.push(node);
75 node.remove.mockImplementation(() => {
76 parent.children = parent.children.filter((child) => child !== node);
77 });
78 },
79 };
80 function layout(next: TestRow[]) {
81 let top = 8;
82 for (const row of next) {
83 row.offsetTop = top;
84 top += row.offsetHeight + 1;
85 }
86 parent.children = [
87 ...next,
88 ...parent.children.filter((row) => row.style.position === "absolute"),
89 ];
90 }
91 layout(rows);
92 const motion = createSidebarListMotion(parent as unknown as HTMLUListElement);
93 return { motion, layout, media, parent };
95​
96function expectMove(row: TestRow, offset: number) {
97 expect(row.animate).toHaveBeenLastCalledWith(
98 [{ transform: `translateY(${offset}px)` }, { transform: "translateY(0px)" }],
99 { duration: 150, easing: "ease-out" },
100 );
102​
103beforeEach(() => vi.stubGlobal("HTMLElement", TestRow));
104afterEach(() => vi.unstubAllGlobals());
105​
106describe("sidebar list motion", () => {
107 it("moves a retained Active row into Settled with its displaced peers", () => {
108 const pinnedHeader = new TestRow("Pinned", 0);
109 const pinned = new TestRow("pin");
110 const divider = new TestRow("Active", 0);
111 const a = new TestRow("a");
112 const b = new TestRow("b");
113 const settledHeader = new TestRow("Settled", 32);
114 const settled = new TestRow("settled", 36);
115 const rows = [pinnedHeader, pinned, divider, a, b, settledHeader, settled];
116 const { motion, layout } = fixture(rows);
117 motion.update(true);
118 expect(rows.every((row) => row.animations.length === 0)).toBe(true);
119​
120 a.offsetHeight = 36;
121 layout([pinnedHeader, pinned, divider, b, settledHeader, settled, a]);
122 motion.update(true);
123 expectMove(a, -153);
124 expectMove(b, 83);
125 expectMove(settledHeader, 83);
126 expectMove(settled, 83);
127 expect(pinned.animate).not.toHaveBeenCalled();
128 expect(divider.animate).not.toHaveBeenCalled();
129 });
130​
131 it("refreshes the drop baseline without replay and animates the next ordinary move", () => {
132 const [a, b, c] = [new TestRow("a"), new TestRow("b"), new TestRow("c")];
133 const { motion, layout } = fixture([a, b, c]);
134 motion.update(true);
135 motion.suspend();
136 a.dragTranslate = 300;
137 b.dragTranslate = -83;
138 motion.update(false);
139 motion.suspend();
140 layout([b, a, c]);
141 a.dragTranslate = b.dragTranslate = 0;
142 motion.update(true);
143 expect([a, b, c].every((row) => row.animations.length === 0)).toBe(true);
144​
145 layout([c, b, a]);
146 motion.update(true);
147 expectMove(c, 166);
148 expectMove(b, -83);
149 expectMove(a, -83);
150 });
151​
152 it("does not carry a canceled drag's transformed position into the next move", () => {
153 const a = new TestRow("a");
154 const b = new TestRow("b");
155 const { motion, layout } = fixture([a, b]);
156 motion.update(true);
157 motion.suspend();
158 a.dragTranslate = 500;
159 b.dragTranslate = -83;
160 motion.update(false);
161 motion.suspend();
162 a.dragTranslate = b.dragTranslate = 0;
163 motion.update(true);
164 expect(a.animate).not.toHaveBeenCalled();
165 expect(b.animate).not.toHaveBeenCalled();
166​
167 layout([b, a]);
168 motion.update(true);
169 expectMove(a, -83);
170 expectMove(b, 83);
171 });
172​
173 it("retargets rapid changes from the current visual position", () => {
174 const [a, b, c] = [new TestRow("a", 99), new TestRow("b", 99), new TestRow("c", 99)];
175 const { motion, layout } = fixture([a, b, c]);
176 motion.update(true);
177 layout([b, c, a]);
178 motion.update(true);
179 expectMove(a, -200);
180 const first = a.animations[0]!;
181 first.progress = 0.25;
182​
183 layout([b, a, c]);
184 motion.update(true);
185 expect(first.cancel).toHaveBeenCalledOnce();
186 expectMove(a, -50);
187 first.finish();
188 motion.suspend();
189 expect(a.animations[1]!.cancel).toHaveBeenCalledOnce();
190 });
191​
192 it("keeps an uninterrupted movement when the layout position does not change", () => {
193 const a = new TestRow("a");
194 const b = new TestRow("b");
195 const { motion, layout } = fixture([a, b]);
196 motion.update(true);
197 layout([b, a]);
198 motion.update(true);
199 a.animations[0]!.progress = 0.5;
200 motion.update(true);
201 expect(a.animate).toHaveBeenCalledOnce();
202 expect(a.animations[0]!.cancel).not.toHaveBeenCalled();
203 });
204​
205 it("cancels owned motion on suspension and never animates a disposed list", () => {
206 const a = new TestRow("a");
207 const b = new TestRow("b");
208 const { motion, layout } = fixture([a, b]);
209 motion.update(true);
210 layout([b, a]);
211 motion.update(true);
212 motion.suspend();
213 expect(a.animations[0]!.cancel).toHaveBeenCalledOnce();
214 expect(b.animations[0]!.cancel).toHaveBeenCalledOnce();
215 motion.update(false);
216 layout([a, b]);
217 motion.update(true);
218 motion.dispose();
219 expect(a.animations[1]!.cancel).toHaveBeenCalledOnce();
220 layout([b, a]);
221 motion.update(true);
222 expect(a.animate).toHaveBeenCalledTimes(2);
223 });
224​
225 it("fades a collapsed-shelf exit at its current visual box and a new wake in", () => {
226 const a = new TestRow("a");
227 const b = new TestRow("b");
228 const fresh = new TestRow("new");
229 a.setAttribute("data-thread-item", "a");
230 a.children = [new TestRow("button")];
231 a.children[0]!.setAttribute("id", "thread-control");
232 a.children[0]!.setAttribute("data-testid", "thread-control");
233 a.children[0]!.setAttribute("data-state", "open");
234 const icon = new TestRow("provider icon");
235 icon.namespaceURI = "http://www.w3.org/2000/svg";
236 icon.setAttribute("id", "provider-mask");
237 icon.setAttribute("mask", "url(#provider-mask)");
238 a.children.push(icon);
239 const { motion, layout, parent } = fixture([a, b]);
240 motion.update(true);
241 layout([b, a]);
242 motion.update(true);
243 a.animations[0]!.progress = 0.5;
244 layout([b, fresh]);
245 motion.update(true);
246 expect(a.animations[0]!.cancel).toHaveBeenCalledOnce();
247 expect(fresh.animate).toHaveBeenLastCalledWith([{ opacity: 0 }, { opacity: 1 }], {
248 duration: 150,
249 easing: "ease-out",
250 });
251 const clone = a.clones[0]!;
252 expect(clone.style).toMatchObject({
253 position: "absolute",
254 top: "49.5px",
255 left: "4px",
256 width: "260px",
257 height: "82px",
258 transform: "none",
259 pointerEvents: "none",
260 });
261 expect(clone.inert).toBe(true);
262 expect(clone.attributes).toEqual([{ name: "aria-hidden", value: "true" }]);
263 expect(clone.children[0]!.attributes).toEqual([{ name: "data-state", value: "open" }]);
264 expect(clone.children[1]!.attributes).toEqual(icon.attributes);
265 expect(clone.animate).toHaveBeenCalledWith([{ opacity: 1 }, { opacity: 0 }], {
266 duration: 150,
267 easing: "ease-out",
268 });
269 expect(parent.children.includes(clone)).toBe(true);
270 motion.update(true);
271 expect(clone.animations).toHaveLength(1);
272 expect(clone.clones).toHaveLength(0);
273 clone.animations[0]!.finish();
274 expect(parent.children.includes(clone)).toBe(false);
275 });
276​
277 it("clears exit clones on pickup and does not fade the release commit", () => {
278 const [a, b, c] = [new TestRow("a"), new TestRow("b"), new TestRow("c")];
279 const { motion, layout, parent } = fixture([a, b]);
280 motion.update(false);
281 layout([b]);
282 motion.update(true);
283 const clone = a.clones[0]!;
284 motion.suspend();
285 expect(clone.animations[0]!.cancel).toHaveBeenCalledOnce();
286 expect(parent.children.includes(clone)).toBe(false);
287 motion.update(false);
288 motion.suspend();
289 layout([c]);
290 motion.update(true);
291 expect(b.clones).toHaveLength(0);
292 expect(c.animations).toHaveLength(0);
293 layout([c, a]);
294 motion.update(true);
295 expect(a.animate).toHaveBeenCalledWith([{ opacity: 0 }, { opacity: 1 }], {
296 duration: 150,
297 easing: "ease-out",
298 });
299 motion.dispose();
300 expect(a.animations.at(-1)!.cancel).toHaveBeenCalledOnce();
301 });
302​
303 it("carries entry opacity into a quick exit and removes artifacts on a silent update", () => {
304 const a = new TestRow("a");
305 const marker = new TestRow("boundary", 0);
306 const { motion, layout, parent } = fixture([marker]);
307 motion.update(false);
308 layout([marker, a]);
309 motion.update(true);
310 a.animations[0]!.progress = 0.4;
311 layout([]);
312 motion.update(true);
313 expect(marker.clones).toHaveLength(0);
314 const clone = a.clones[0]!;
315 expect(clone.animate).toHaveBeenCalledWith([{ opacity: 0.4 }, { opacity: 0 }], {
316 duration: 150,
317 easing: "ease-out",
318 });
319 motion.update(false);
320 expect(parent.children).toEqual([]);
321 expect(clone.animations[0]!.cancel).toHaveBeenCalledOnce();
322 });
323​
324 it("respects reduced motion while keeping the next baseline fresh", () => {
325 const a = new TestRow("a");
326 const b = new TestRow("b");
327 const { motion, layout, media } = fixture([a, b]);
328 motion.update(true);
329 media.matches = true;
330 layout([b, a]);
331 motion.update(true);
332 expect(a.animate).not.toHaveBeenCalled();
333 media.matches = false;
334 layout([a, b]);
335 motion.update(true);
336 expectMove(a, 83);
337 expectMove(b, -83);
338 });
339});