Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { render } from "../../test/render";
3import { createSidebarListMotion } from "./Sidebar.motion";
4
5function createList(container: HTMLElement) {
6 const viewport = document.createElement("div");
7 viewport.dataset.slot = "scroll-area-viewport";
8 viewport.style.cssText = "height: 200px; overflow-y: auto; overflow-anchor: none";
9 const list = document.createElement("ul");
10 list.style.cssText = "position: relative; margin: 0; padding: 0; list-style: none";
11 const rows = Array.from({ length: 10 }, () => {
12 const row = document.createElement("li");
13 row.style.height = "64px";
14 list.append(row);
15 return row;
16 });
17 viewport.append(list);
18 container.append(viewport);
19 const motion = createSidebarListMotion(list);
20 motion.update(false);
21 const scroll = (top: number) => {
22 viewport.scrollTop = top;
23 viewport.dispatchEvent(new Event("scroll"));
24 };
25 return { viewport, list, rows, motion, scroll };
26}
27
28describe("sidebar list motion", () => {
29 it("slides rows above a removal at the bottom instead of jumping with the scroll clamp", async () => {
30 await using screen = await render(null);
31 const { viewport, rows, motion, scroll } = createList(screen.container);
32 using _motion = { [Symbol.dispose]: () => motion.dispose() };
33 scroll(viewport.scrollHeight);
34 const above = rows[7]!;
35 const before = above.getBoundingClientRect().top;
36
37 rows[8]!.remove();
38 motion.update(true);
39 for (const animation of above.getAnimations()) animation.pause();
40
41 expect(above.getBoundingClientRect().top).toBe(before);
42 });
43
44 it("keeps the release scroll position after a reorder at the bottom of the list", async () => {
45 await using screen = await render(null);
46 const { viewport, list, motion, scroll } = createList(screen.container);
47 using _motion = { [Symbol.dispose]: () => motion.dispose() };
48 const restingMax = viewport.scrollHeight - viewport.clientHeight;
49 scroll(restingMax);
50
51 const reorder = async () => {
52 motion.suspend();
53 const overflow = document.createElement("li");
54 overflow.style.cssText = "position: absolute; bottom: -120px; height: 1px; width: 1px";
55 list.append(overflow);
56 motion.update(false);
57 const dragMax = viewport.scrollHeight - viewport.clientHeight;
58 scroll(viewport.scrollTop + 64);
59 const releaseTop = viewport.scrollTop;
60 motion.release();
61 overflow.remove();
62 list.insertBefore(list.lastElementChild!, list.children[7]!);
63 motion.update(false);
64 await Promise.allSettled(
65 list.getAnimations({ subtree: true }).map((animation) => animation.finished),
66 );
67 expect(viewport.scrollTop).toBe(releaseTop);
68 return dragMax;
69 };
70
71 const dragMax = await reorder();
72 expect(await reorder()).toBeLessThanOrEqual(dragMax);
73 scroll(restingMax - 100);
74 expect(viewport.scrollTop).toBe(restingMax - 100);
75 expect(viewport.scrollHeight - viewport.clientHeight).toBe(restingMax);
76 });
77});