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

workspace-stack-projection.test.ts

252 lines10.2 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 nativeStackPopAction,
7 nativeWorkspacePopAction,
8 nativeWorkspacePopCount,
9 projectWorkspaceStack,
10 partitionStackPresentations,
11 reconcileStackScreens,
12} from "./workspace-stack-projection";
13​
14const requireNavigation = NodeModule.createRequire(
15 NodeModule.createRequire(import.meta.url).resolve("@react-navigation/native/package.json"),
16);
17const { StackRouter } = requireNavigation("@react-navigation/routers") as {
18 StackRouter: typeof import("@react-navigation/native").StackRouter;
19};
20​
21const home = { key: "home", name: "Home" };
22const thread = { key: "thread", name: "Thread", params: { threadId: "draft-thread" } };
23const files = { key: "files", name: "ThreadFiles", params: thread.params };
24const settings = { key: "settings", name: "SettingsSheet" };
25const legal = { key: "legal", name: "SettingsLegal" };
26​
27function history(
28 routes: StackNavigationState<ParamListBase>["routes"],
29): StackNavigationState<ParamListBase> {
30 return {
31 key: "router",
32 type: "stack",
33 stale: false,
34 index: routes.length - 1,
35 routeNames: ["Home", "Thread", "ThreadFiles", "SettingsSheet", "SettingsLegal"],
36 routes,
37 preloadedRoutes: [],
38 };
40​
41describe("workspace router projection", () => {
42 it("keeps workspace flows beside Home and restores them after a modal closes", () => {
43 const draft = { key: "draft", name: "NewTaskSheet" };
44 const modal = { key: "connect", name: "ConnectOnboarding" };
45 const routes = [home, thread, draft, settings, legal];
46 const isOverlay = (route: { name: string }) => route.name === "ConnectOnboarding";
47 expect(projectWorkspaceStack(history([...routes, modal]), isOverlay)).toEqual({
48 primary: home,
49 detail: [thread, draft, settings, legal],
50 overlays: [modal],
51 });
52 expect(projectWorkspaceStack(history(routes), isOverlay)).toEqual({
53 primary: home,
54 detail: [thread, draft, settings, legal],
55 overlays: [],
56 });
57 });
58​
59 it("keeps the thread and file history in the detail column when a modal is opened", () => {
60 const state = history([home, thread, files, settings, legal]);
61 const projection = projectWorkspaceStack(state, (route) => route.name === "SettingsSheet");
62 expect(projection).toEqual({
63 primary: home,
64 detail: [thread, files],
65 overlays: [settings, legal],
66 });
67 expect(projection.detail[0]).toBe(thread);
68 expect(state.routes).toEqual([home, thread, files, settings, legal]);
69 });
70​
71 it("retains a cold-linked detail route without changing the router history", () => {
72 const state = history([thread, files]);
73 expect(projectWorkspaceStack(state, () => false)).toEqual({
74 primary: undefined,
75 detail: [thread, files],
76 overlays: [],
77 });
78 expect(state.routes[0]).toBe(thread);
79 });
80​
81 it("restores the empty detail column after Back reaches the thread list", () => {
82 expect(projectWorkspaceStack(history([home]), () => false)).toEqual({
83 primary: home,
84 detail: [],
85 overlays: [],
86 });
87 });
88});
89​
90describe("native workspace dismissal", () => {
91 it("dismisses a sheet and its pushed pages without removing the underlying draft", () => {
92 const state = history([home, thread, settings, legal]);
93 const action = nativeWorkspacePopAction(state, settings.key)!;
94 const next = StackRouter({}).getStateForAction(state, action, {
95 routeNames: state.routeNames,
96 routeParamList: {},
97 routeGetIdList: {},
98 });
99 expect(next?.routes).toEqual([home, thread]);
100 expect(next?.routes[1]).toBe(thread);
101 expect(next?.index).toBe(1);
102 });
103​
104 it("does not dismiss retained sheets beyond the active index", () => {
105 const state = { ...history([home, thread, settings, legal]), index: 1 };
106 expect(nativeWorkspacePopAction(state, settings.key)).toBeNull();
107 expect(nativeWorkspacePopAction(state, legal.key)).toBeNull();
108 });
109 it("pops a native dismissed file while keeping the conversation and its draft mounted", () => {
110 expect(nativeWorkspacePopCount(history([home, thread, files]), files.key)).toBe(1);
111 });
112​
113 it("removes the descendants when UIKit dismisses their parent conversation", () => {
114 expect(nativeWorkspacePopCount(history([home, thread, files]), thread.key)).toBe(2);
115 });
116​
117 it("ignores delayed callbacks from JS removal and replacement", () => {
118 expect(nativeWorkspacePopCount(history([home, thread]), files.key)).toBe(0);
119 expect(
120 nativeWorkspacePopCount(history([home, { ...thread, key: "replacement" }]), thread.key),
121 ).toBe(0);
122 });
123​
124 it("never removes the root or a route ahead of the active index", () => {
125 const state = history([home, thread, files]);
126 expect(nativeWorkspacePopCount(state, home.key)).toBe(0);
127 expect(nativeWorkspacePopCount({ ...state, index: 1 }, files.key)).toBe(0);
128 });
129});
130​
131describe("native card pop handoff", () => {
132 const routerOptions = {
133 routeNames: history([home]).routeNames,
134 routeParamList: {},
135 routeGetIdList: {},
136 };
137 it("keeps a newly opened screen when an earlier swipe dismissal arrives late", () => {
138 const state = history([home, thread, files, settings]);
139 const next = StackRouter({}).getStateForAction(
140 state,
141 nativeStackPopAction(state, files.key)!,
142 routerOptions,
143 );
144 expect(next?.routes).toEqual([home, thread, settings]);
145 expect(next?.index).toBe(2);
146 if (!next || next.stale !== false) throw new Error("Expected a rehydrated router history");
147 expect(nativeStackPopAction(next, files.key)).toBeNull();
148 });
149 it("acknowledges a native pop to an earlier page without consuming a newer push", () => {
150 const state = history([home, thread, files, settings]);
151 const next = StackRouter({}).getStateForAction(
152 state,
153 nativeStackPopAction(state, files.key, home.key)!,
154 routerOptions,
155 );
156 expect(next?.routes).toEqual([home, settings]);
157 expect(next?.index).toBe(1);
158 });
159 it("allows reopening the same page after its destination finishes appearing", () => {
160 const state = history([home, thread, files]);
161 const router = StackRouter({});
162 const popped = router.getStateForAction(
163 state,
164 nativeStackPopAction(state, files.key, thread.key)!,
165 routerOptions,
166 );
167 if (!popped || popped.stale !== false) throw new Error("Expected a rehydrated router history");
168 const reopened = router.getStateForAction(
169 popped,
170 { type: "NAVIGATE", payload: { name: files.name, params: files.params } },
171 routerOptions,
172 );
173 expect(reopened?.routes.map((route) => route.name)).toEqual(["Home", "Thread", "ThreadFiles"]);
174 expect(reopened?.routes.at(-1)?.key).not.toBe(files.key);
175 if (!reopened || reopened.stale !== false)
176 throw new Error("Expected a rehydrated router history");
177 expect(nativeStackPopAction(reopened, files.key)).toBeNull();
178 });
179 it("leaves history intact when a swipe is cancelled or its source is already gone", () => {
180 const state = history([home, thread, files]);
181 expect(nativeStackPopAction(state, files.key, files.key)).toBeNull();
182 expect(nativeStackPopAction(state, files.key, settings.key)).toBeNull();
183 expect(nativeStackPopAction(state, home.key)).toBeNull();
184 expect(nativeStackPopAction({ ...state, index: 1 }, files.key)).toBeNull();
185 });
186});
187​
188describe("v5 stack handoff", () => {
189 it("releases a completed native pop when the router acknowledges it", () => {
190 const completed = new Set([files.key]);
191 expect(reconcileStackScreens([home, thread, files], [home, thread, files], completed)).toEqual([
192 home,
193 thread,
194 files,
195 ]);
196 expect(reconcileStackScreens([home, thread, files], [home, thread], completed)).toEqual([
197 home,
198 thread,
199 ]);
200 });
201 it("still retains an unfinished JS pop beside a completed native pop", () => {
202 expect(reconcileStackScreens([home, thread, files], [home], new Set([files.key]))).toEqual([
203 home,
204 thread,
205 ]);
206 });
207 it("retains removed native screens through a pop followed immediately by a push", () => {
208 const popped = reconcileStackScreens([home, thread, files], [home, thread]);
209 const next = { ...files, key: "new-files" };
210 const screens = reconcileStackScreens(popped, [home, thread, next]);
211 expect(screens.map((route) => route.key)).toEqual(["home", "thread", "files", "new-files"]);
212 expect(screens.filter((route) => [home, thread, next].includes(route))).toEqual([
213 home,
214 thread,
215 next,
216 ]);
217 expect(screens[2]).toBe(files);
218 });
219 it("keeps the root and outgoing settings page in place until the JS pop finishes", () => {
220 const environments = { ...files, key: "environments", name: "SettingsEnvironments" };
221 const popped = reconcileStackScreens([settings, environments], [settings]);
222 expect(popped).toEqual([settings, environments]);
223 expect(reconcileStackScreens(popped, [settings])).toEqual(popped);
224 const next = { ...environments, key: "next-environments" };
225 expect(reconcileStackScreens(popped, [settings, next])).toEqual([settings, environments, next]);
226 expect(reconcileStackScreens(popped, [settings], new Set([environments.key]))).toEqual([
227 settings,
228 ]);
229 });
230 it("retains a multi-page pop in order without changing the active router order", () => {
231 const replacement = { ...thread, key: "replacement" };
232 const screens = reconcileStackScreens([home, thread, files], [home, replacement]);
233 expect(screens).toEqual([home, thread, files, replacement]);
234 expect(screens.filter((route) => route === home || route === replacement)).toEqual([
235 home,
236 replacement,
237 ]);
238 });
239 it("updates params without retaining duplicate copies of the same screen", () => {
240 const updated = { ...thread, params: { threadId: "another-thread" } };
241 expect(reconcileStackScreens([home, thread], [home, updated])).toEqual([home, updated]);
242 });
243 it("keeps card pushes inside the modal they belong to", () => {
244 const secondModal = { ...settings, key: "another-settings" };
245 expect(
246 partitionStackPresentations(
247 [home, thread, settings, legal, secondModal],
248 (route) => route.name === "SettingsSheet",
249 ),
250 ).toEqual([[home, thread], [settings, legal], [secondModal]]);
251 });
252});