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

Sidebar.motion.test.ts

535 lines17.6 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 getBoundingClientRect: () => ({ top: 0 }),
74 append(node: TestRow) {
75 parent.children.push(node);
76 node.remove.mockImplementation(() => {
77 parent.children = parent.children.filter((child) => child !== node);
78 });
79 },
80 };
81 function layout(next: TestRow[]) {
82 let top = 8;
83 for (const row of next) {
84 row.offsetTop = top;
85 top += row.offsetHeight + 1;
86 }
87 parent.children = [
88 ...next,
89 ...parent.children.filter((row) => row.style.position === "absolute"),
90 ];
91 }
92 layout(rows);
93 const motion = createSidebarListMotion(parent as unknown as HTMLUListElement);
94 return { motion, layout, media, parent };
96​
97function expectMove(row: TestRow, offset: number) {
98 expect(row.animate).toHaveBeenLastCalledWith(
99 [{ transform: `translateY(${offset}px)` }, { transform: "translateY(0px)" }],
100 { duration: 150, easing: "ease-out" },
101 );
103​
104beforeEach(() => vi.stubGlobal("HTMLElement", TestRow));
105afterEach(() => vi.unstubAllGlobals());
106​
107describe("sidebar list motion", () => {
108 it("moves a retained Active row into Settled with its displaced peers", () => {
109 const pinnedHeader = new TestRow("Pinned", 0);
110 const pinned = new TestRow("pin");
111 const divider = new TestRow("Active", 0);
112 const a = new TestRow("a");
113 const b = new TestRow("b");
114 const settledHeader = new TestRow("Settled", 32);
115 const settled = new TestRow("settled", 36);
116 const rows = [pinnedHeader, pinned, divider, a, b, settledHeader, settled];
117 const { motion, layout } = fixture(rows);
118 motion.update(true);
119 expect(rows.every((row) => row.animations.length === 0)).toBe(true);
120​
121 a.offsetHeight = 36;
122 layout([pinnedHeader, pinned, divider, b, settledHeader, settled, a]);
123 motion.update(true);
124 expectMove(a, -153);
125 expectMove(b, 83);
126 expectMove(settledHeader, 83);
127 expectMove(settled, 83);
128 expect(pinned.animate).not.toHaveBeenCalled();
129 expect(divider.animate).not.toHaveBeenCalled();
130 });
131​
132 it("refreshes the drop baseline without replay and animates the next ordinary move", () => {
133 const [a, b, c] = [new TestRow("a"), new TestRow("b"), new TestRow("c")];
134 const { motion, layout } = fixture([a, b, c]);
135 motion.update(true);
136 motion.suspend();
137 a.dragTranslate = 300;
138 b.dragTranslate = -83;
139 motion.update(false);
140 motion.suspend();
141 layout([b, a, c]);
142 a.dragTranslate = b.dragTranslate = 0;
143 motion.update(true);
144 expect([a, b, c].every((row) => row.animations.length === 0)).toBe(true);
145​
146 layout([c, b, a]);
147 motion.update(true);
148 expectMove(c, 166);
149 expectMove(b, -83);
150 expectMove(a, -83);
151 });
152​
153 it("glides every row from its released position into its committed slot", () => {
154 const [a, b, c] = [new TestRow("a"), new TestRow("b"), new TestRow("c")];
155 const { motion, layout } = fixture([a, b, c]);
156 motion.update(true);
157 motion.suspend();
158 a.dragTranslate = 130;
159 b.dragTranslate = -83;
160 c.dragTranslate = 16;
161 motion.update(false);
162 motion.release();
163 layout([b, a, c]);
164 a.dragTranslate = b.dragTranslate = c.dragTranslate = 0;
165 motion.update(true);
166 expectMove(a, 47);
167 expect(b.animations).toHaveLength(0);
168 expectMove(c, 16);
169 });
170​
171 it("does not glide on release when motion is reduced", () => {
172 const [a, b] = [new TestRow("a"), new TestRow("b")];
173 const { motion, layout, media } = fixture([a, b]);
174 motion.update(true);
175 media.matches = true;
176 a.dragTranslate = 100;
177 motion.release();
178 layout([b, a]);
179 a.dragTranslate = 0;
180 motion.update(true);
181 expect(a.animations).toHaveLength(0);
182 expect(b.animations).toHaveLength(0);
183 });
184​
185 it("does not carry a canceled drag's transformed position into the next move", () => {
186 const a = new TestRow("a");
187 const b = new TestRow("b");
188 const { motion, layout } = fixture([a, b]);
189 motion.update(true);
190 motion.suspend();
191 a.dragTranslate = 500;
192 b.dragTranslate = -83;
193 motion.update(false);
194 motion.suspend();
195 a.dragTranslate = b.dragTranslate = 0;
196 motion.update(true);
197 expect(a.animate).not.toHaveBeenCalled();
198 expect(b.animate).not.toHaveBeenCalled();
199​
200 layout([b, a]);
201 motion.update(true);
202 expectMove(a, -83);
203 expectMove(b, 83);
204 });
205​
206 it("retargets rapid changes from the current visual position", () => {
207 const [a, b, c] = [new TestRow("a", 99), new TestRow("b", 99), new TestRow("c", 99)];
208 const { motion, layout } = fixture([a, b, c]);
209 motion.update(true);
210 layout([b, c, a]);
211 motion.update(true);
212 expectMove(a, -200);
213 const first = a.animations[0]!;
214 first.progress = 0.25;
215​
216 layout([b, a, c]);
217 motion.update(true);
218 expect(first.cancel).toHaveBeenCalledOnce();
219 expectMove(a, -50);
220 first.finish();
221 motion.suspend();
222 expect(a.animations[1]!.cancel).toHaveBeenCalledOnce();
223 });
224​
225 it("keeps an uninterrupted movement when the layout position does not change", () => {
226 const a = new TestRow("a");
227 const b = new TestRow("b");
228 const { motion, layout } = fixture([a, b]);
229 motion.update(true);
230 layout([b, a]);
231 motion.update(true);
232 a.animations[0]!.progress = 0.5;
233 motion.update(true);
234 expect(a.animate).toHaveBeenCalledOnce();
235 expect(a.animations[0]!.cancel).not.toHaveBeenCalled();
236 });
237​
238 it("cancels owned motion on suspension and never animates a disposed list", () => {
239 const a = new TestRow("a");
240 const b = new TestRow("b");
241 const { motion, layout } = fixture([a, b]);
242 motion.update(true);
243 layout([b, a]);
244 motion.update(true);
245 motion.suspend();
246 expect(a.animations[0]!.cancel).toHaveBeenCalledOnce();
247 expect(b.animations[0]!.cancel).toHaveBeenCalledOnce();
248 motion.update(false);
249 layout([a, b]);
250 motion.update(true);
251 motion.dispose();
252 expect(a.animations[1]!.cancel).toHaveBeenCalledOnce();
253 layout([b, a]);
254 motion.update(true);
255 expect(a.animate).toHaveBeenCalledTimes(2);
256 });
257​
258 it("fades a collapsed-shelf exit at its current visual box and a new wake in", () => {
259 const a = new TestRow("a");
260 const b = new TestRow("b");
261 const fresh = new TestRow("new");
262 a.setAttribute("data-thread-item", "a");
263 a.children = [new TestRow("button")];
264 a.children[0]!.setAttribute("id", "thread-control");
265 a.children[0]!.setAttribute("data-testid", "thread-control");
266 a.children[0]!.setAttribute("data-state", "open");
267 const icon = new TestRow("provider icon");
268 icon.namespaceURI = "http://www.w3.org/2000/svg";
269 icon.setAttribute("id", "provider-mask");
270 icon.setAttribute("mask", "url(#provider-mask)");
271 a.children.push(icon);
272 const { motion, layout, parent } = fixture([a, b]);
273 motion.update(true);
274 layout([b, a]);
275 motion.update(true);
276 a.animations[0]!.progress = 0.5;
277 layout([b, fresh]);
278 motion.update(true);
279 expect(a.animations[0]!.cancel).toHaveBeenCalledOnce();
280 expect(fresh.animate).toHaveBeenLastCalledWith(
281 [
282 { opacity: 0, transform: "translateY(83px)" },
283 { opacity: 1, transform: "translateY(0px)" },
284 ],
285 {
286 duration: 150,
287 easing: "ease-out",
288 },
289 );
290 const clone = a.clones[0]!;
291 expect(clone.style).toMatchObject({
292 position: "absolute",
293 top: "49.5px",
294 left: "4px",
295 width: "260px",
296 height: "82px",
297 transform: "none",
298 pointerEvents: "none",
299 });
300 expect(clone.inert).toBe(true);
301 expect(clone.attributes).toEqual([{ name: "aria-hidden", value: "true" }]);
302 expect(clone.children[0]!.attributes).toEqual([{ name: "data-state", value: "open" }]);
303 expect(clone.children[1]!.attributes).toEqual(icon.attributes);
304 expect(clone.animate).toHaveBeenCalledWith(
305 [
306 { opacity: 1, transform: "translateY(0px)" },
307 { opacity: 0, transform: "translateY(-83px)" },
308 ],
309 {
310 duration: 150,
311 easing: "ease-out",
312 },
313 );
314 expect(parent.children.includes(clone)).toBe(true);
315 motion.update(true);
316 expect(clone.animations).toHaveLength(1);
317 expect(clone.clones).toHaveLength(0);
318 clone.animations[0]!.finish();
319 expect(parent.children.includes(clone)).toBe(false);
320 });
321​
322 it("rides entering rows on the shelf displacement so an opened shelf moves as one block", () => {
323 const a = new TestRow("a", 40);
324 const header = new TestRow("header", 32);
325 const x = new TestRow("x", 36);
326 const y = new TestRow("y", 36);
327 const z = new TestRow("z", 36);
328 const { motion, layout } = fixture([a, header, x]);
329 motion.update(true);
330 layout([a, header, x, y, z]);
331 for (const row of [header, x, y, z]) row.offsetTop -= 72;
332 motion.update(true);
333 expectMove(header, 72);
334 expectMove(x, 72);
335 expect(a.animate).not.toHaveBeenCalled();
336 expect(y.animate).toHaveBeenLastCalledWith(
337 [
338 { opacity: 0, transform: "translateY(72px)" },
339 { opacity: 1, transform: "translateY(0px)" },
340 ],
341 { duration: 150, easing: "ease-out" },
342 );
343 expect(z.animate).toHaveBeenLastCalledWith(
344 [
345 { opacity: 0, transform: "translateY(72px)" },
346 { opacity: 1, transform: "translateY(0px)" },
347 ],
348 { duration: 150, easing: "ease-out" },
349 );
350 });
351​
352 it("rides new rows on a retained header's unfinished travel", () => {
353 const header = new TestRow("header", 32);
354 const above = new TestRow("above", 39);
355 const incoming = new TestRow("incoming", 36);
356 const { motion, layout } = fixture([header]);
357 motion.update(true);
358 layout([above, header]);
359 motion.update(true);
360 header.animations[0]!.progress = 0.25;
361 layout([above, header, incoming]);
362 motion.update(true);
363 expect(incoming.animate).toHaveBeenLastCalledWith(
364 [
365 { opacity: 0, transform: "translateY(-30px)" },
366 { opacity: 1, transform: "translateY(0px)" },
367 ],
368 { duration: 150, easing: "ease-out" },
369 );
370 });
371​
372 it("cancels in-flight row travel when a bulk update skips animation", () => {
373 const a = new TestRow("a");
374 const b = new TestRow("b");
375 const { motion, layout } = fixture([a, b]);
376 motion.update(true);
377 layout([b, a]);
378 motion.update(true);
379 const first = a.animations[0]!;
380 first.progress = 0.4;
381 const incoming = Array.from({ length: 41 }, (_, index) => new TestRow(`new-${index}`));
382 layout(incoming);
383 motion.update(true);
384 expect(first.cancel).toHaveBeenCalledOnce();
385 expect(incoming.every((row) => row.animate.mock.calls.length === 0)).toBe(true);
386 });
387​
388 it("keeps in-flight shelf entry travel when the shelf closes mid-animation", () => {
389 const a = new TestRow("a", 40);
390 const header = new TestRow("header", 32);
391 const x = new TestRow("x", 36);
392 const y = new TestRow("y", 36);
393 const { motion, layout } = fixture([a, header, x]);
394 motion.update(true);
395 layout([a, header, x, y]);
396 for (const row of [header, x, y]) row.offsetTop -= 36;
397 motion.update(true);
398 const entry = y.animations[0]!;
399 header.animations[0]!.progress = 0.25;
400 x.animations[0]!.progress = 0.25;
401 entry.progress = 0.25;
402 layout([a, header, x]);
403 motion.update(true);
404 expect(entry.cancel).toHaveBeenCalledOnce();
405 const clone = y.clones[0]!;
406 expect(clone.style.top).toBe("110px");
407 expect(clone.animate).toHaveBeenCalledWith(
408 [
409 { opacity: 0.25, transform: "translateY(0px)" },
410 { opacity: 0, transform: "translateY(9px)" },
411 ],
412 { duration: 150, easing: "ease-out" },
413 );
414 expectMove(header, -9);
415 expectMove(x, -9);
416 });
417​
418 it("clears exit clones on pickup and does not fade the release commit", () => {
419 const [a, b, c] = [new TestRow("a"), new TestRow("b"), new TestRow("c")];
420 const { motion, layout, parent } = fixture([a, b]);
421 motion.update(false);
422 layout([b]);
423 motion.update(true);
424 const clone = a.clones[0]!;
425 motion.suspend();
426 expect(clone.animations[0]!.cancel).toHaveBeenCalledOnce();
427 expect(parent.children.includes(clone)).toBe(false);
428 motion.update(false);
429 motion.suspend();
430 layout([c]);
431 motion.update(true);
432 expect(b.clones).toHaveLength(0);
433 expect(c.animations).toHaveLength(0);
434 layout([c, a]);
435 motion.update(true);
436 expect(a.animate).toHaveBeenCalledWith(
437 [
438 { opacity: 0, transform: "translateY(0px)" },
439 { opacity: 1, transform: "translateY(0px)" },
440 ],
441 {
442 duration: 150,
443 easing: "ease-out",
444 },
445 );
446 motion.dispose();
447 expect(a.animations.at(-1)!.cancel).toHaveBeenCalledOnce();
448 });
449​
450 it("carries entry opacity into a quick exit and removes artifacts on a silent update", () => {
451 const a = new TestRow("a");
452 const marker = new TestRow("boundary", 0);
453 const { motion, layout, parent } = fixture([marker]);
454 motion.update(false);
455 layout([marker, a]);
456 motion.update(true);
457 a.animations[0]!.progress = 0.4;
458 layout([]);
459 motion.update(true);
460 expect(marker.clones).toHaveLength(0);
461 const clone = a.clones[0]!;
462 expect(clone.animate).toHaveBeenCalledWith(
463 [
464 { opacity: 0.4, transform: "translateY(0px)" },
465 { opacity: 0, transform: "translateY(40px)" },
466 ],
467 {
468 duration: 150,
469 easing: "ease-out",
470 },
471 );
472 motion.update(false);
473 expect(parent.children).toEqual([]);
474 expect(clone.animations[0]!.cancel).toHaveBeenCalledOnce();
475 });
476​
477 it("retargets an in-flight entry when a later layout shift moves the row", () => {
478 const a = new TestRow("a", 40);
479 const header = new TestRow("header", 32);
480 const x = new TestRow("x", 36);
481 const y = new TestRow("y", 36);
482 const { motion, layout } = fixture([a, header, x]);
483 motion.update(true);
484 layout([a, header, x, y]);
485 for (const row of [header, x, y]) row.offsetTop -= 36;
486 motion.update(true);
487 const entry = y.animations[0]!;
488 entry.progress = 0.25;
489 for (const row of [header, x, y]) row.offsetTop += 40;
490 motion.update(true);
491 expect(entry.cancel).not.toHaveBeenCalled();
492 expectMove(y, -13);
493 });
494​
495 it("skips fades when a large list change would clone too many rows", () => {
496 const rows = Array.from({ length: 41 }, (_, index) => new TestRow(`row-${index}`));
497 const { motion, layout, parent } = fixture(rows);
498 motion.update(true);
499 layout([]);
500 motion.update(true);
501 expect(rows.every((row) => row.clones.length === 0)).toBe(true);
502 expect(parent.children).toHaveLength(0);
503 const entering = Array.from({ length: 41 }, (_, index) => new TestRow(`new-${index}`));
504 layout(entering);
505 motion.update(true);
506 expect(entering.every((row) => row.animate.mock.calls.length === 0)).toBe(true);
507 });
508​
509 it("still slides rows when one removal displaces a large list", () => {
510 const rows = Array.from({ length: 60 }, (_, index) => new TestRow(`row-${index}`));
511 const { motion, layout } = fixture(rows);
512 motion.update(true);
513 const [removed, ...rest] = rows;
514 layout(rest);
515 motion.update(true);
516 expect(removed!.clones).toHaveLength(1);
517 expect(rest.every((row) => row.animations.length === 1)).toBe(true);
518 });
519​
520 it("respects reduced motion while keeping the next baseline fresh", () => {
521 const a = new TestRow("a");
522 const b = new TestRow("b");
523 const { motion, layout, media } = fixture([a, b]);
524 motion.update(true);
525 media.matches = true;
526 layout([b, a]);
527 motion.update(true);
528 expect(a.animate).not.toHaveBeenCalled();
529 media.matches = false;
530 layout([a, b]);
531 motion.update(true);
532 expectMove(a, 83);
533 expectMove(b, -83);
534 });
535});