Open History to see the last commit
1import * as NodeModule from "node:module";
2import type { ParamListBase, StackNavigationState } from "@react-navigation/native";
3import { describe, expect, it } from "vite-plus/test";
4
5import {
6 collapsedSplitTransition,
7 nativeStackPopAction,
8 nativeWorkspacePopAction,
9 nativeWorkspacePopCount,
10 projectStackPresentation,
11 projectWorkspaceStack,
12 partitionStackPresentations,
13 reconcileStackScreens,
14 type CollapsedSplitColumn,
15} from "./workspace-stack-projection";
16
17const requireNavigation = NodeModule.createRequire(
18 NodeModule.createRequire(import.meta.url).resolve("@react-navigation/native/package.json"),
19);
20const { StackRouter } = requireNavigation("@react-navigation/routers") as {
21 StackRouter: typeof import("@react-navigation/native").StackRouter;
22};
23
24const home = { key: "home", name: "Home" };
25const thread = { key: "thread", name: "Thread", params: { threadId: "draft-thread" } };
26const files = { key: "files", name: "ThreadFiles", params: thread.params };
27const settings = { key: "settings", name: "Settings" };
28const legal = { key: "legal", name: "SettingsLegal" };
29
30function history(
31 routes: StackNavigationState<ParamListBase>["routes"],
32): StackNavigationState<ParamListBase> {
33 return {
34 key: "router",
35 type: "stack",
36 stale: false,
37 index: routes.length - 1,
38 routeNames: ["Home", "Thread", "ThreadFiles", "Settings", "SettingsLegal"],
39 routes,
40 preloadedRoutes: [],
41 };
42}
43
44describe("workspace presentation envelope", () => {
45 const isOverlay = (route: { name: string }) => route.name === "ReviewSheet";
46
47 it("keeps card settings in the detail history and restores it after system Back closes a sheet", () => {
48 const router = StackRouter({});
49 const state = history([home, thread, settings]);
50 const routerOptions = {
51 routeNames: [...state.routeNames, "ReviewSheet", "ReviewDetails"],
52 routeParamList: {},
53 routeGetIdList: {},
54 };
55 const opened = router.getStateForAction(
56 { ...state, routeNames: routerOptions.routeNames },
57 { type: "PUSH", payload: { name: "ReviewSheet" } },
58 routerOptions,
59 );
60 if (!opened || opened.stale !== false) throw new Error("Expected an opened sheet history");
61 const projection = projectStackPresentation(opened, isOverlay);
62 expect(projection.workspace).toEqual(state.routes);
63 expect(projection.workspace.at(-1)).toBe(settings);
64 expect(projection.overlays.map((route) => route.name)).toEqual(["ReviewSheet"]);
65 const closed = router.getStateForAction(opened, { type: "GO_BACK" }, routerOptions);
66 if (!closed || closed.stale !== false) throw new Error("Expected a restored workspace history");
67 expect(projectStackPresentation(closed, isOverlay)).toEqual({
68 workspace: state.routes,
69 overlays: [],
70 });
71 });
72
73 it("keeps pushed sheet pages above the workspace and dismisses the entire sheet group", () => {
74 const sheet = { key: "review", name: "ReviewSheet" };
75 const page = { key: "review-details", name: "ReviewDetails" };
76 const state = history([home, thread, sheet, page]);
77 expect(projectStackPresentation(state, isOverlay)).toEqual({
78 workspace: [home, thread],
79 overlays: [sheet, page],
80 });
81 const closed = StackRouter({}).getStateForAction(
82 state,
83 nativeWorkspacePopAction(state, sheet.key)!,
84 { routeNames: state.routeNames, routeParamList: {}, routeGetIdList: {} },
85 );
86 if (!closed || closed.stale !== false) throw new Error("Expected a dismissed sheet history");
87 expect(projectStackPresentation(closed, isOverlay)).toEqual({
88 workspace: [home, thread],
89 overlays: [],
90 });
91 expect(closed.routes[1]).toBe(thread);
92 });
93
94 it("preserves a cold-linked detail history without requiring a Home route", () => {
95 const sheet = { key: "review", name: "ReviewSheet" };
96 expect(projectStackPresentation(history([thread, files, sheet]), isOverlay)).toEqual({
97 workspace: [thread, files],
98 overlays: [sheet],
99 });
100 });
101});
102
103describe("workspace router projection", () => {
104 it("keeps workspace flows beside Home and restores them after a modal closes", () => {
105 const draft = { key: "draft", name: "Thread", params: { draft: {} } };
106 const modal = { key: "connect", name: "ConnectOnboarding" };
107 const routes = [home, thread, draft, legal];
108 const isOverlay = (route: { name: string }) => route.name === "ConnectOnboarding";
109 expect(projectWorkspaceStack(history([...routes, modal]), isOverlay)).toEqual({
110 primary: home,
111 detail: [thread, draft, legal],
112 overlays: [modal],
113 });
114 expect(projectWorkspaceStack(history(routes), isOverlay)).toEqual({
115 primary: home,
116 detail: [thread, draft, legal],
117 overlays: [],
118 });
119 });
120
121 it("shows the Settings list as the primary content and its pages as the detail", () => {
122 const environments = { key: "environments", name: "SettingsEnvironments" };
123 expect(projectWorkspaceStack(history([home, settings, environments]), () => false)).toEqual({
124 primary: home,
125 detail: [environments],
126 overlays: [],
127 });
128 });
129
130 it("returns from a Settings page to the Settings list that Home is showing", () => {
131 const environments = { key: "environments", name: "SettingsEnvironments" };
132 expect(
133 nativeStackPopAction(history([home, settings, environments]), environments.key, home.key),
134 ).toMatchObject({ payload: { count: 1 } });
135 });
136
137 it("retains a cold-linked detail route without changing the router history", () => {
138 const state = history([thread, files]);
139 expect(projectWorkspaceStack(state, () => false)).toEqual({
140 primary: undefined,
141 detail: [thread, files],
142 overlays: [],
143 });
144 expect(state.routes[0]).toBe(thread);
145 });
146});
147
148describe("native workspace dismissal", () => {
149 it("dismisses a sheet and its pushed pages without removing the underlying draft", () => {
150 const state = history([home, thread, settings, legal]);
151 const action = nativeWorkspacePopAction(state, settings.key)!;
152 const next = StackRouter({}).getStateForAction(state, action, {
153 routeNames: state.routeNames,
154 routeParamList: {},
155 routeGetIdList: {},
156 });
157 expect(next?.routes).toEqual([home, thread]);
158 expect(next?.routes[1]).toBe(thread);
159 expect(next?.index).toBe(1);
160 });
161
162 it("does not dismiss retained sheets beyond the active index", () => {
163 const state = { ...history([home, thread, settings, legal]), index: 1 };
164 expect(nativeWorkspacePopAction(state, settings.key)).toBeNull();
165 expect(nativeWorkspacePopAction(state, legal.key)).toBeNull();
166 });
167
168 it("removes the descendants when UIKit dismisses their parent conversation", () => {
169 expect(nativeWorkspacePopCount(history([home, thread, files]), thread.key)).toBe(2);
170 });
171
172 it("ignores delayed callbacks from JS removal and replacement", () => {
173 expect(nativeWorkspacePopCount(history([home, thread]), files.key)).toBe(0);
174 expect(
175 nativeWorkspacePopCount(history([home, { ...thread, key: "replacement" }]), thread.key),
176 ).toBe(0);
177 });
178
179 it("never removes the root or a route ahead of the active index", () => {
180 const state = history([home, thread, files]);
181 expect(nativeWorkspacePopCount(state, home.key)).toBe(0);
182 expect(nativeWorkspacePopCount({ ...state, index: 1 }, files.key)).toBe(0);
183 });
184});
185
186describe("native card pop handoff", () => {
187 const routerOptions = {
188 routeNames: history([home]).routeNames,
189 routeParamList: {},
190 routeGetIdList: {},
191 };
192 it("keeps a newly opened screen when an earlier swipe dismissal arrives late", () => {
193 const state = history([home, thread, files, settings]);
194 const next = StackRouter({}).getStateForAction(
195 state,
196 nativeStackPopAction(state, files.key)!,
197 routerOptions,
198 );
199 expect(next?.routes).toEqual([home, thread, settings]);
200 expect(next?.index).toBe(2);
201 if (!next || next.stale !== false) throw new Error("Expected a rehydrated router history");
202 expect(nativeStackPopAction(next, files.key)).toBeNull();
203 });
204 it("acknowledges a native pop to an earlier page without consuming a newer push", () => {
205 const state = history([home, thread, files, settings]);
206 const next = StackRouter({}).getStateForAction(
207 state,
208 nativeStackPopAction(state, files.key, home.key)!,
209 routerOptions,
210 );
211 expect(next?.routes).toEqual([home, settings]);
212 expect(next?.index).toBe(1);
213 });
214 it("allows reopening the same page after its destination finishes appearing", () => {
215 const state = history([home, thread, files]);
216 const router = StackRouter({});
217 const popped = router.getStateForAction(
218 state,
219 nativeStackPopAction(state, files.key, thread.key)!,
220 routerOptions,
221 );
222 if (!popped || popped.stale !== false) throw new Error("Expected a rehydrated router history");
223 const reopened = router.getStateForAction(
224 popped,
225 { type: "NAVIGATE", payload: { name: files.name, params: files.params } },
226 routerOptions,
227 );
228 expect(reopened?.routes.map((route) => route.name)).toEqual(["Home", "Thread", "ThreadFiles"]);
229 expect(reopened?.routes.at(-1)?.key).not.toBe(files.key);
230 if (!reopened || reopened.stale !== false)
231 throw new Error("Expected a rehydrated router history");
232 expect(nativeStackPopAction(reopened, files.key)).toBeNull();
233 });
234 it("leaves history intact when a swipe is cancelled or its source is already gone", () => {
235 const state = history([home, thread, files]);
236 expect(nativeStackPopAction(state, files.key, files.key)).toBeNull();
237 expect(nativeStackPopAction(state, files.key, settings.key)).toBeNull();
238 expect(nativeStackPopAction(state, home.key)).toBeNull();
239 expect(nativeStackPopAction({ ...state, index: 1 }, files.key)).toBeNull();
240 });
241});
242
243describe("collapsed split Back", () => {
244 const routerOptions = {
245 routeNames: history([home]).routeNames,
246 routeParamList: {},
247 routeGetIdList: {},
248 };
249 type Appearance = "willAppear" | "didAppear" | "willDisappear";
250 // UIKit's appearance callbacks for one collapsed transition, against the router at each step.
251 function replay(
252 steps: ReadonlyArray<readonly [Appearance, string, StackNavigationState<ParamListBase>]>,
253 ) {
254 let column: CollapsedSplitColumn = { visible: "secondary", leaving: null };
255 let pop: ReturnType<typeof nativeStackPopAction> = null;
256 for (const [type, key, state] of steps) {
257 const transition = collapsedSplitTransition(column, { type, key }, state, home.key);
258 column = transition.column;
259 pop ??= transition.pop;
260 }
261 return { visible: column.visible, pop };
262 }
263
264 it("returns the router to the list so the next selection opens a new detail", () => {
265 const state = history([home, thread]);
266 const back = replay([
267 ["willDisappear", thread.key, state],
268 ["willAppear", home.key, state],
269 ["didAppear", home.key, state],
270 ]);
271 expect(back.visible).toBe("primary");
272 const router = StackRouter({});
273 const listed = router.getStateForAction(state, back.pop!, routerOptions);
274 expect(listed?.routes).toEqual([home]);
275 if (!listed || listed.stale !== false) throw new Error("Expected a rehydrated router history");
276 const select = {
277 type: "NAVIGATE",
278 payload: { name: "Thread", params: { threadId: "next" } },
279 } as const;
280 expect(router.getStateForAction(listed, select, routerOptions)?.routes[1]?.key).not.toBe(
281 thread.key,
282 );
283 // Left on the hidden detail, the selection would only rename it and never show the column.
284 expect(router.getStateForAction(state, select, routerOptions)?.routes[1]?.key).toBe(thread.key);
285 });
286
287 it("clears every detail while keeping a presentation opened during the Back animation", () => {
288 const leaving = history([home, thread, files]);
289 const presented = history([home, thread, files, settings]);
290 const { pop } = replay([
291 ["willDisappear", files.key, leaving],
292 ["didAppear", home.key, presented],
293 ]);
294 expect(StackRouter({}).getStateForAction(presented, pop!, routerOptions)?.routes).toEqual([
295 home,
296 settings,
297 ]);
298 });
299
300 it("keeps the detail when it appears again before the list finishes appearing", () => {
301 const state = history([home, thread]);
302 const cancelledSwipe = replay([
303 ["willDisappear", thread.key, state],
304 ["willAppear", home.key, state],
305 ["willAppear", thread.key, state],
306 ["didAppear", thread.key, state],
307 ]);
308 expect(cancelledSwipe).toEqual({ visible: "secondary", pop: null });
309 expect(
310 replay([
311 ["willDisappear", thread.key, state],
312 ["willAppear", thread.key, state],
313 ["didAppear", home.key, state],
314 ]).pop,
315 ).toBeNull();
316 });
317
318 it("does not mistake JS navigation or a cold link for native Back", () => {
319 const pushed = history([home, thread, files]);
320 expect(
321 replay([
322 ["willDisappear", thread.key, pushed],
323 ["didAppear", files.key, pushed],
324 ]),
325 ).toEqual({ visible: "secondary", pop: null });
326 const presented = history([home, thread, settings]);
327 expect(
328 replay([
329 ["willDisappear", thread.key, presented],
330 ["didAppear", home.key, presented],
331 ]).pop,
332 ).toBeNull();
333 const popped = history([home]);
334 expect(
335 replay([
336 ["willDisappear", thread.key, popped],
337 ["didAppear", home.key, popped],
338 ]).pop,
339 ).toBeNull();
340 // A linked thread whose list appears first still needs its detail column shown.
341 expect(replay([["didAppear", home.key, history([home, thread])]])).toEqual({
342 visible: "primary",
343 pop: null,
344 });
345 });
346});
347
348describe("v5 stack handoff", () => {
349 it("releases a completed native pop when the router acknowledges it", () => {
350 const completed = new Set([files.key]);
351 expect(reconcileStackScreens([home, thread, files], [home, thread, files], completed)).toEqual([
352 home,
353 thread,
354 files,
355 ]);
356 expect(reconcileStackScreens([home, thread, files], [home, thread], completed)).toEqual([
357 home,
358 thread,
359 ]);
360 });
361 it("still retains an unfinished JS pop beside a completed native pop", () => {
362 expect(reconcileStackScreens([home, thread, files], [home], new Set([files.key]))).toEqual([
363 home,
364 thread,
365 ]);
366 });
367 it("retains removed native screens through a pop followed immediately by a push", () => {
368 const popped = reconcileStackScreens([home, thread, files], [home, thread]);
369 const next = { ...files, key: "new-files" };
370 const screens = reconcileStackScreens(popped, [home, thread, next]);
371 expect(screens.map((route) => route.key)).toEqual(["home", "thread", "files", "new-files"]);
372 expect(screens.filter((route) => [home, thread, next].includes(route))).toEqual([
373 home,
374 thread,
375 next,
376 ]);
377 expect(screens[2]).toBe(files);
378 });
379 it("keeps the root and outgoing settings page in place until the JS pop finishes", () => {
380 const environments = { ...files, key: "environments", name: "SettingsEnvironments" };
381 const popped = reconcileStackScreens([settings, environments], [settings]);
382 expect(popped).toEqual([settings, environments]);
383 expect(reconcileStackScreens(popped, [settings])).toEqual(popped);
384 const next = { ...environments, key: "next-environments" };
385 expect(reconcileStackScreens(popped, [settings, next])).toEqual([settings, environments, next]);
386 expect(reconcileStackScreens(popped, [settings], new Set([environments.key]))).toEqual([
387 settings,
388 ]);
389 });
390 // regression: #17231 keep native screens ordered during stack pops
391 it("retains a multi-page pop in order without changing the active router order", () => {
392 const replacement = { ...thread, key: "replacement" };
393 const screens = reconcileStackScreens([home, thread, files], [home, replacement]);
394 expect(screens).toEqual([home, thread, files, replacement]);
395 expect(screens.filter((route) => route === home || route === replacement)).toEqual([
396 home,
397 replacement,
398 ]);
399 });
400 it("updates params without retaining duplicate copies of the same screen", () => {
401 const updated = { ...thread, params: { threadId: "another-thread" } };
402 expect(reconcileStackScreens([home, thread], [home, updated])).toEqual([home, updated]);
403 });
404 it("keeps card pushes inside the modal they belong to", () => {
405 const secondModal = { ...settings, key: "another-settings" };
406 expect(
407 partitionStackPresentations(
408 [home, thread, settings, legal, secondModal],
409 (route) => route.name === "Settings",
410 ),
411 ).toEqual([[home, thread], [settings, legal], [secondModal]]);
412 });
413});