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

Sidebar.motion.test.ts

401 lines13.5 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 is lifted 130px below its slot; b previews upward into a's place
159 // and c holds a 16px label gap open below the divider.
160 a.dragTranslate = 130;
161 b.dragTranslate = -83;
162 c.dragTranslate = 16;
163 motion.update(false);
164 motion.release();
165 layout([b, a, c]);
166 a.dragTranslate = b.dragTranslate = c.dragTranslate = 0;
167 motion.update(true);
168 // Lifted top 138, committed slot top 91: glide the remaining 47px.
169 expectMove(a, 47);
170 // b already sits where it lands; c closes its 16px gap.
171 expect(b.animations).toHaveLength(0);
172 expectMove(c, 16);
173 });
174​
175 it("does not glide on release when motion is reduced", () => {
176 const [a, b] = [new TestRow("a"), new TestRow("b")];
177 const { motion, layout, media } = fixture([a, b]);
178 motion.update(true);
179 media.matches = true;
180 a.dragTranslate = 100;
181 motion.release();
182 layout([b, a]);
183 a.dragTranslate = 0;
184 motion.update(true);
185 expect(a.animations).toHaveLength(0);
186 expect(b.animations).toHaveLength(0);
187 });
188​
189 it("does not carry a canceled drag's transformed position into the next move", () => {
190 const a = new TestRow("a");
191 const b = new TestRow("b");
192 const { motion, layout } = fixture([a, b]);
193 motion.update(true);
194 motion.suspend();
195 a.dragTranslate = 500;
196 b.dragTranslate = -83;
197 motion.update(false);
198 motion.suspend();
199 a.dragTranslate = b.dragTranslate = 0;
200 motion.update(true);
201 expect(a.animate).not.toHaveBeenCalled();
202 expect(b.animate).not.toHaveBeenCalled();
203​
204 layout([b, a]);
205 motion.update(true);
206 expectMove(a, -83);
207 expectMove(b, 83);
208 });
209​
210 it("retargets rapid changes from the current visual position", () => {
211 const [a, b, c] = [new TestRow("a", 99), new TestRow("b", 99), new TestRow("c", 99)];
212 const { motion, layout } = fixture([a, b, c]);
213 motion.update(true);
214 layout([b, c, a]);
215 motion.update(true);
216 expectMove(a, -200);
217 const first = a.animations[0]!;
218 first.progress = 0.25;
219​
220 layout([b, a, c]);
221 motion.update(true);
222 expect(first.cancel).toHaveBeenCalledOnce();
223 expectMove(a, -50);
224 first.finish();
225 motion.suspend();
226 expect(a.animations[1]!.cancel).toHaveBeenCalledOnce();
227 });
228​
229 it("keeps an uninterrupted movement when the layout position does not change", () => {
230 const a = new TestRow("a");
231 const b = new TestRow("b");
232 const { motion, layout } = fixture([a, b]);
233 motion.update(true);
234 layout([b, a]);
235 motion.update(true);
236 a.animations[0]!.progress = 0.5;
237 motion.update(true);
238 expect(a.animate).toHaveBeenCalledOnce();
239 expect(a.animations[0]!.cancel).not.toHaveBeenCalled();
240 });
241​
242 it("cancels owned motion on suspension and never animates a disposed list", () => {
243 const a = new TestRow("a");
244 const b = new TestRow("b");
245 const { motion, layout } = fixture([a, b]);
246 motion.update(true);
247 layout([b, a]);
248 motion.update(true);
249 motion.suspend();
250 expect(a.animations[0]!.cancel).toHaveBeenCalledOnce();
251 expect(b.animations[0]!.cancel).toHaveBeenCalledOnce();
252 motion.update(false);
253 layout([a, b]);
254 motion.update(true);
255 motion.dispose();
256 expect(a.animations[1]!.cancel).toHaveBeenCalledOnce();
257 layout([b, a]);
258 motion.update(true);
259 expect(a.animate).toHaveBeenCalledTimes(2);
260 });
261​
262 it("fades a collapsed-shelf exit at its current visual box and a new wake in", () => {
263 const a = new TestRow("a");
264 const b = new TestRow("b");
265 const fresh = new TestRow("new");
266 a.setAttribute("data-thread-item", "a");
267 a.children = [new TestRow("button")];
268 a.children[0]!.setAttribute("id", "thread-control");
269 a.children[0]!.setAttribute("data-testid", "thread-control");
270 a.children[0]!.setAttribute("data-state", "open");
271 const icon = new TestRow("provider icon");
272 icon.namespaceURI = "http://www.w3.org/2000/svg";
273 icon.setAttribute("id", "provider-mask");
274 icon.setAttribute("mask", "url(#provider-mask)");
275 a.children.push(icon);
276 const { motion, layout, parent } = fixture([a, b]);
277 motion.update(true);
278 layout([b, a]);
279 motion.update(true);
280 a.animations[0]!.progress = 0.5;
281 layout([b, fresh]);
282 motion.update(true);
283 expect(a.animations[0]!.cancel).toHaveBeenCalledOnce();
284 expect(fresh.animate).toHaveBeenLastCalledWith([{ opacity: 0 }, { opacity: 1 }], {
285 duration: 150,
286 easing: "ease-out",
287 });
288 const clone = a.clones[0]!;
289 expect(clone.style).toMatchObject({
290 position: "absolute",
291 top: "49.5px",
292 left: "4px",
293 width: "260px",
294 height: "82px",
295 transform: "none",
296 pointerEvents: "none",
297 });
298 expect(clone.inert).toBe(true);
299 expect(clone.attributes).toEqual([{ name: "aria-hidden", value: "true" }]);
300 expect(clone.children[0]!.attributes).toEqual([{ name: "data-state", value: "open" }]);
301 expect(clone.children[1]!.attributes).toEqual(icon.attributes);
302 expect(clone.animate).toHaveBeenCalledWith([{ opacity: 1 }, { opacity: 0 }], {
303 duration: 150,
304 easing: "ease-out",
305 });
306 expect(parent.children.includes(clone)).toBe(true);
307 motion.update(true);
308 expect(clone.animations).toHaveLength(1);
309 expect(clone.clones).toHaveLength(0);
310 clone.animations[0]!.finish();
311 expect(parent.children.includes(clone)).toBe(false);
312 });
313​
314 it("clears exit clones on pickup and does not fade the release commit", () => {
315 const [a, b, c] = [new TestRow("a"), new TestRow("b"), new TestRow("c")];
316 const { motion, layout, parent } = fixture([a, b]);
317 motion.update(false);
318 layout([b]);
319 motion.update(true);
320 const clone = a.clones[0]!;
321 motion.suspend();
322 expect(clone.animations[0]!.cancel).toHaveBeenCalledOnce();
323 expect(parent.children.includes(clone)).toBe(false);
324 motion.update(false);
325 motion.suspend();
326 layout([c]);
327 motion.update(true);
328 expect(b.clones).toHaveLength(0);
329 expect(c.animations).toHaveLength(0);
330 layout([c, a]);
331 motion.update(true);
332 expect(a.animate).toHaveBeenCalledWith([{ opacity: 0 }, { opacity: 1 }], {
333 duration: 150,
334 easing: "ease-out",
335 });
336 motion.dispose();
337 expect(a.animations.at(-1)!.cancel).toHaveBeenCalledOnce();
338 });
339​
340 it("carries entry opacity into a quick exit and removes artifacts on a silent update", () => {
341 const a = new TestRow("a");
342 const marker = new TestRow("boundary", 0);
343 const { motion, layout, parent } = fixture([marker]);
344 motion.update(false);
345 layout([marker, a]);
346 motion.update(true);
347 a.animations[0]!.progress = 0.4;
348 layout([]);
349 motion.update(true);
350 expect(marker.clones).toHaveLength(0);
351 const clone = a.clones[0]!;
352 expect(clone.animate).toHaveBeenCalledWith([{ opacity: 0.4 }, { opacity: 0 }], {
353 duration: 150,
354 easing: "ease-out",
355 });
356 motion.update(false);
357 expect(parent.children).toEqual([]);
358 expect(clone.animations[0]!.cancel).toHaveBeenCalledOnce();
359 });
360​
361 it("skips fades when a large list change would clone too many rows", () => {
362 const rows = Array.from({ length: 41 }, (_, index) => new TestRow(`row-${index}`));
363 const { motion, layout, parent } = fixture(rows);
364 motion.update(true);
365 layout([]);
366 motion.update(true);
367 expect(rows.every((row) => row.clones.length === 0)).toBe(true);
368 expect(parent.children).toHaveLength(0);
369 const entering = Array.from({ length: 41 }, (_, index) => new TestRow(`new-${index}`));
370 layout(entering);
371 motion.update(true);
372 expect(entering.every((row) => row.animate.mock.calls.length === 0)).toBe(true);
373 });
374​
375 it("still slides rows when one removal displaces a large list", () => {
376 const rows = Array.from({ length: 60 }, (_, index) => new TestRow(`row-${index}`));
377 const { motion, layout } = fixture(rows);
378 motion.update(true);
379 const [removed, ...rest] = rows;
380 layout(rest);
381 motion.update(true);
382 expect(removed!.clones).toHaveLength(1);
383 expect(rest.every((row) => row.animations.length === 1)).toBe(true);
384 });
385​
386 it("respects reduced motion while keeping the next baseline fresh", () => {
387 const a = new TestRow("a");
388 const b = new TestRow("b");
389 const { motion, layout, media } = fixture([a, b]);
390 motion.update(true);
391 media.matches = true;
392 layout([b, a]);
393 motion.update(true);
394 expect(a.animate).not.toHaveBeenCalled();
395 media.matches = false;
396 layout([a, b]);
397 motion.update(true);
398 expectMove(a, 83);
399 expectMove(b, -83);
400 });
401});