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

source-file-scroll.test.ts

156 lines4.6 KB

1import { expect, it, vi } from "vite-plus/test";
2​
3import { createSourceLineScroll } from "./source-file-scroll";
4​
5const ROW_HEIGHT = 24;
6​
7// Mirrors VirtualizedList.scrollToIndex without getItemLayout: indices past the highest
8// measured cell fail, and unmeasured indices below it scroll to an averaged estimate.
9function setup({
10 lineCount = 1_203,
11 measuredThrough = 79,
12 rowHeight = (_index: number): number => ROW_HEIGHT,
13} = {}) {
14 vi.useFakeTimers();
15 vi.stubGlobal("requestAnimationFrame", (callback: () => void) => setTimeout(callback, 16));
16 vi.stubGlobal("cancelAnimationFrame", (frame: ReturnType<typeof setTimeout>) =>
17 clearTimeout(frame),
18 );
19 const measured = new Set<number>();
20 const state = {
21 lineCount,
22 offset: 0,
23 measure(from: number, to: number) {
24 for (let index = from; index <= to; index += 1) measured.add(index);
25 },
26 };
27 state.measure(0, measuredThrough);
28 const offsetOf = (index: number) => {
29 let offset = 0;
30 for (let row = 0; row < index; row += 1) offset += rowHeight(row);
31 return offset;
32 };
33 const averageItemLength = () => {
34 let total = 0;
35 for (const index of measured) total += rowHeight(index);
36 return total / measured.size;
37 };
38 const scrollToIndex = vi.fn((params: { index: number }) => {
39 if (params.index < 0 || params.index >= state.lineCount) {
40 throw new Error(`scrollToIndex out of range: requested index ${params.index}`);
41 }
42 const highestMeasuredFrameIndex = Math.max(...measured);
43 if (params.index > highestMeasuredFrameIndex) {
44 scroll.onScrollToIndexFailed({
45 index: params.index,
46 averageItemLength: averageItemLength(),
47 highestMeasuredFrameIndex,
48 });
49 return;
50 }
51 state.offset = measured.has(params.index)
52 ? offsetOf(params.index)
53 : averageItemLength() * params.index;
54 });
55 const scrollToOffset = vi.fn((params: { offset: number }) => {
56 state.offset = params.offset;
57 });
58 const list = {
59 get props() {
60 return { data: { length: state.lineCount } };
61 },
62 scrollToIndex,
63 scrollToOffset,
64 };
65 const scroll = createSourceLineScroll();
66 return {
67 scroll,
68 list,
69 state,
70 offsetOf,
71 scrollToIndex,
72 scrollToOffset,
73 [Symbol.dispose]: () => {
74 vi.unstubAllGlobals();
75 vi.useRealTimers();
76 },
77 };
79​
80it("lands on an already measured line", () => {
81 using t = setup();
82​
83 t.scroll.scrollToLine(t.list, 40);
84 vi.runAllTimers();
85​
86 expect(t.state.offset).toBe(t.offsetOf(40));
87 expect(t.scrollToOffset).not.toHaveBeenCalled();
88 expect(vi.getTimerCount()).toBe(0);
89});
90​
91it("jumps near an unmeasured wrapped line, then lands on it once rows measure", () => {
92 using t = setup({ rowHeight: (index) => (index >= 80 ? ROW_HEIGHT * 3 : ROW_HEIGHT) });
93​
94 t.scroll.scrollToLine(t.list, 899);
95 expect(() => vi.advanceTimersToNextTimer()).not.toThrow();
96 expect(t.state.offset).toBeGreaterThan(0);
97​
98 // The render batch after the estimated jump measures rows past the target first, so
99 // the next scrollToIndex is accepted against an averaged estimate.
100 t.state.measure(900, 979);
101 vi.advanceTimersToNextTimer();
102 expect(t.state.offset).not.toBe(t.offsetOf(899));
103​
104 t.state.measure(820, 899);
105 vi.runAllTimers();
106​
107 expect(t.state.offset).toBe(t.offsetOf(899));
108 expect(vi.getTimerCount()).toBe(0);
109});
110​
111it("stops retrying when the target never measures", () => {
112 using t = setup({ rowHeight: (index) => (index === 899 ? ROW_HEIGHT * 400 : ROW_HEIGHT) });
113​
114 t.scroll.scrollToLine(t.list, 899);
115 expect(() => vi.runAllTimers()).not.toThrow();
116​
117 expect(t.scrollToIndex.mock.calls.length).toBeLessThanOrEqual(10);
118 expect(vi.getTimerCount()).toBe(0);
119});
120​
121it("ignores a line outside the document", () => {
122 using t = setup({ lineCount: 12 });
123​
124 expect(() => t.scroll.scrollToLine(t.list, 899)).not.toThrow();
125 vi.runAllTimers();
126 expect(t.scrollToIndex).not.toHaveBeenCalled();
127 expect(t.scrollToOffset).not.toHaveBeenCalled();
128});
129​
130it("drops a pending jump when the document shrinks or the jump is cancelled", () => {
131 using t = setup();
132​
133 t.scroll.scrollToLine(t.list, 899);
134 vi.advanceTimersToNextTimer();
135 t.state.lineCount = 12;
136 expect(() => vi.runAllTimers()).not.toThrow();
137​
138 t.state.lineCount = 1_203;
139 t.scroll.scrollToLine(t.list, 899);
140 vi.advanceTimersToNextTimer();
141 const calls = t.scrollToIndex.mock.calls.length;
142 t.scroll.cancel();
143 t.state.measure(80, 999);
144 vi.runAllTimers();
145 expect(t.scrollToIndex).toHaveBeenCalledTimes(calls);
146});
147​
148it("a drag before the first frame cancels the jump", () => {
149 using t = setup();
150​
151 t.scroll.scrollToLine(t.list, 40);
152 t.scroll.cancel();
153 vi.runAllTimers();
154​
155 expect(t.scrollToIndex).not.toHaveBeenCalled();
156});