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

workspace-stack-projection.test.ts

353 lines13.9 KB

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