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

legend-list-initial-reveal.test.ts

214 lines7.3 KB

1// @effect-diagnostics nodeBuiltinImport:off - reads the installed native JS bundle for synchronous VM tests.
2import * as NodeFS from "node:fs";
3import * as NodeVM from "node:vm";
4import { describe, expect, it, vi } from "vite-plus/test";
5​
6// Exercise the shipped patch without loading React Native in the Node runner.
7// Native scroll delivery and animation frames advance independently here.
8function createList(bundle: string) {
9 const source = NodeFS.readFileSync(
10 new URL(`../apps/mobile/node_modules/@legendapp/list/${bundle}`, import.meta.url),
11 "utf8",
12 );
13 const renderState = source.slice(
14 source.indexOf("function setInitialRenderState("),
15 source.indexOf("// src/core/finishInitialScroll.ts"),
16 );
17 const watchdog = source.slice(
18 source.indexOf("var INSET_END_SETTLE_WATCHDOG_FRAMES"),
19 source.indexOf("function dispatchInitialScroll("),
20 );
21 const frames: Array<() => void> = [];
22 const values = new Map<string, unknown>();
23 const scrollTo = vi.fn();
24 const onLoad = vi.fn();
25 const state = {
26 props: { data: ["message"], onLoad, drawDistance: 500 },
27 initialScroll: { index: 0, viewPosition: 1 },
28 loadStartTime: 0,
29 didContainersLayout: true,
30 didFinishInitialScroll: true,
31 didLoad: false,
32 didUserDrag: false,
33 scrollLength: 800,
34 scroll: 400,
35 lastNativeScroll: 200 as number | undefined,
36 scrollingTo: undefined as { offset: number } | undefined,
37 maintainingScrollAtEnd: false,
38 refScroller: { current: { scrollTo } },
39 };
40 const ctx = { state };
41 let contentSize = 1200;
42 let inset = 100;
43 const api = NodeVM.runInNewContext(
44 `${renderState}\n${watchdog}\n({ start: startInsetEndSettleWatchdog, complete: setInitialRenderState })`,
45 {
46 requestAnimationFrame: (callback: () => void) => frames.push(callback),
47 getContentSize: () => contentSize,
48 getContentInsetStartAdjustment: () => inset,
49 peek$: (_ctx: unknown, key: string) => values.get(key),
50 set$: (_ctx: unknown, key: string, value: unknown) => values.set(key, value),
51 setAdaptiveRender: vi.fn(),
52 scheduleFullDrawDistancePrewarm: vi.fn(),
53 INITIAL_DRAW_DISTANCE: 250,
54 },
55 ) as {
56 start: (context: typeof ctx) => void;
57 complete: (context: typeof ctx, flags: Record<string, boolean>) => void;
58 };
59 return {
60 state,
61 scrollTo,
62 onLoad,
63 start: () => api.start(ctx),
64 complete: () => api.complete(ctx, {}),
65 ready: () => values.get("readyToRender") === true,
66 resize: (size: number) => {
67 contentSize = size;
68 },
69 setInset: (value: number) => {
70 inset = value;
71 },
72 advance(count: number) {
73 for (let index = 0; index < count; index++) {
74 const batch = frames.splice(0);
75 for (const frame of batch) frame();
76 }
77 },
78 };
80​
81for (const bundle of ["react-native.js", "react-native.mjs"]) {
82 describe(`initial inset end reveal (${bundle})`, () => {
83 it("waits for the native offset instead of the optimistic scroll target", () => {
84 const list = createList(bundle);
85 list.start();
86 list.advance(8);
87 expect(list.ready()).toBe(false);
88 expect(list.scrollTo).toHaveBeenCalledWith({ animated: false, x: 0, y: 400 });
89 list.state.lastNativeScroll = 400;
90 list.advance(7);
91 expect(list.ready()).toBe(true);
92 expect(list.onLoad).toHaveBeenCalledTimes(1);
93 });
94​
95 it("gates the seeded contentOffset completion path too", () => {
96 const list = createList(bundle);
97 list.complete();
98 expect(list.ready()).toBe(false);
99 list.state.lastNativeScroll = 400;
100 list.advance(7);
101 expect(list.ready()).toBe(true);
102 });
103​
104 it("preserves an initial index before the last item", () => {
105 const list = createList(bundle);
106 list.state.props.data = ["requested message", "later message"];
107 list.state.scroll = 200;
108 list.complete();
109 list.advance(8);
110 expect(list.ready()).toBe(true);
111 expect(list.scrollTo).not.toHaveBeenCalled();
112 });
113​
114 it("does not count an in-flight scroll as stability", () => {
115 const list = createList(bundle);
116 list.state.lastNativeScroll = 400;
117 list.state.scrollingTo = { offset: 400 };
118 list.start();
119 list.advance(8);
120 expect(list.ready()).toBe(false);
121 list.state.scrollingTo = undefined;
122 list.state.maintainingScrollAtEnd = true;
123 list.advance(8);
124 expect(list.ready()).toBe(false);
125 list.state.maintainingScrollAtEnd = false;
126 list.advance(7);
127 expect(list.ready()).toBe(true);
128 });
129​
130 it("waits for a stable end target even when native scrolling follows each measurement", () => {
131 const list = createList(bundle);
132 list.start();
133 for (let index = 0; index < 10; index++) {
134 list.resize(1200 + index * 10);
135 list.state.lastNativeScroll = 400 + index * 10;
136 list.advance(1);
137 }
138 expect(list.ready()).toBe(false);
139 list.advance(7);
140 expect(list.ready()).toBe(true);
141 });
142​
143 it("accounts for header insets when content is shorter than the full viewport", () => {
144 const list = createList(bundle);
145 list.resize(780);
146 list.state.scroll = -20;
147 list.state.lastNativeScroll = -100;
148 list.start();
149 list.advance(8);
150 expect(list.ready()).toBe(false);
151 expect(list.scrollTo).toHaveBeenCalledWith({ animated: false, x: 0, y: -20 });
152 list.state.lastNativeScroll = -20;
153 list.advance(7);
154 expect(list.ready()).toBe(true);
155 });
156​
157 it("keeps the reveal bounded when native events never arrive", () => {
158 const list = createList(bundle);
159 list.state.lastNativeScroll = undefined;
160 list.start();
161 list.advance(8);
162 expect(list.ready()).toBe(false);
163 expect(list.scrollTo).not.toHaveBeenCalled();
164 list.advance(32);
165 expect(list.ready()).toBe(true);
166 });
167​
168 it("releases control on drag and never starts another initial hold", () => {
169 const list = createList(bundle);
170 list.start();
171 list.state.didUserDrag = true;
172 list.advance(1);
173 expect(list.ready()).toBe(true);
174 expect(list.scrollTo).not.toHaveBeenCalled();
175 list.complete();
176 list.start();
177 list.advance(20);
178 expect(list.state.didUserDrag).toBe(true);
179 expect(list.scrollTo).not.toHaveBeenCalled();
180 expect(list.onLoad).toHaveBeenCalledTimes(1);
181 });
182​
183 it("preserves a drag that started before initial layout completed", () => {
184 const list = createList(bundle);
185 list.state.didUserDrag = true;
186 list.complete();
187 list.advance(1);
188 expect(list.ready()).toBe(true);
189 expect(list.state.didUserDrag).toBe(true);
190 expect(list.scrollTo).not.toHaveBeenCalled();
191 });
192​
193 it("reveals short content at UIKit's resting offset without awaiting a scroll event", () => {
194 const list = createList(bundle);
195 list.resize(700);
196 list.state.lastNativeScroll = undefined;
197 list.complete();
198 expect(list.ready()).toBe(true);
199 list.advance(10);
200 expect(list.scrollTo).not.toHaveBeenCalled();
201 });
202​
203 it("does not gate empty lists or lists without an automatic header inset", () => {
204 const list = createList(bundle);
205 list.setInset(0);
206 list.complete();
207 expect(list.ready()).toBe(true);
208 const empty = createList(bundle);
209 empty.state.props.data = [];
210 empty.complete();
211 expect(empty.ready()).toBe(true);
212 });
213 });