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