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

useCodeViewScrollRestore.test.ts

118 lines3.8 KB

1import type { CodeViewScrollTarget } from "@pierre/diffs";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { createCodeViewScrollRestore } from "./useCodeViewScrollRestore";
5​
6function viewer(scrollHeight: number, height = 500) {
7 const instance = {
8 scrollHeight,
9 height,
10 getHeight: () => instance.height,
11 getScrollHeight: () => instance.scrollHeight,
12 getRenderedItems: () => [{ id: "notes.txt" }],
13 getTopForItem: (id: string) => (id === "notes.txt" ? 120 : undefined),
14 };
15 // Where Pierre lands each request: an item target at its top less the offset, clamped.
16 const scrolls: number[] = [];
17 return {
18 instance,
19 scrolls,
20 getInstance: () => instance,
21 scrollTo: (target: CodeViewScrollTarget) => {
22 if (target.type !== "item") throw new Error(`unexpected ${target.type} target`);
23 const top = 120 - (target.offset ?? 0);
24 scrolls.push(Math.max(0, Math.min(top, instance.scrollHeight - instance.height)));
25 },
26 };
28​
29function mount(
30 scopeKey: string,
31 view: ReturnType<typeof viewer>,
32 settled = true,
33 loadMore: (() => void) | null = null,
34) {
35 const restore = createCodeViewScrollRestore();
36 restore.update(scopeKey, settled, loadMore);
37 restore.attach(view);
38 return restore;
40​
41describe("createCodeViewScrollRestore", () => {
42 // 9500 is the very end and 9469 sits within the last sticky header's height of it.
43 it.each([4000, 9469, 9500])(
44 "puts a remounted viewer back at its scope's position %i, not another scope's",
45 (scrollTop) => {
46 const first = viewer(10_000);
47 mount(`thread-a-${scrollTop}`, first).onScroll(scrollTop, first.instance);
48 const other = viewer(10_000);
49 mount(`thread-b-${scrollTop}`, other).onScroll(1200, other.instance);
50​
51 const again = viewer(10_000);
52 mount(`thread-a-${scrollTop}`, again);
53 expect(again.scrolls).toEqual([scrollTop]);
54 },
55 );
56​
57 it("loads more files until the position is reachable, then gives up once none remain", () => {
58 const first = viewer(10_000);
59 mount("thread-c", first).onScroll(4000, first.instance);
60​
61 const loading = viewer(700);
62 let batches = 0;
63 const restore = mount("thread-c", loading, false, () => {
64 batches += 1;
65 });
66 restore.onScroll(0, loading.instance);
67 restore.flush();
68 expect(loading.scrolls).toEqual([]);
69 expect(batches).toBe(2);
70 loading.instance.scrollHeight = 10_000;
71 restore.flush();
72 restore.flush();
73 expect(loading.scrolls).toEqual([4000]);
74​
75 const shrunk = viewer(2000);
76 const settling = mount("thread-c", shrunk, false);
77 settling.flush();
78 expect(shrunk.scrolls).toEqual([]);
79 settling.update("thread-c", true, null);
80 settling.flush();
81 expect(shrunk.scrolls).toEqual([1500]);
82 });
83​
84 it("keeps loading but stays where the user scrolls while the saved position is pending", () => {
85 const first = viewer(10_000);
86 mount("thread-e", first).onScroll(4000, first.instance);
87​
88 const loading = viewer(700);
89 let batches = 0;
90 const restore = mount("thread-e", loading, false, () => {
91 batches += 1;
92 });
93 restore.onScroll(0, loading.instance);
94 restore.onScroll(150, loading.instance);
95 restore.onScroll(0, loading.instance);
96 restore.flush();
97 expect(batches).toBe(2);
98 loading.instance.scrollHeight = 10_000;
99 restore.update("thread-e", true, null);
100 restore.flush();
101 expect(loading.scrolls).toEqual([]);
102​
103 const again = viewer(10_000);
104 mount("thread-e", again);
105 expect(again.scrolls).toEqual([]);
106 });
107​
108 it("forgets a position that a reveal or collapsing every file replaced", () => {
109 const first = viewer(10_000);
110 const restore = mount("thread-d", first);
111 restore.onScroll(4000, first.instance);
112 restore.discard();
113​
114 const again = viewer(10_000);
115 mount("thread-d", again);
116 expect(again.scrolls).toEqual([]);
117 });
118});