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