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

workspace-stack-projection.test.ts

175 lines6.8 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 nativeWorkspacePopAction,
7 nativeWorkspacePopCount,
8 projectWorkspaceStack,
9 partitionStackPresentations,
10 reconcileStackScreens,
11} from "./workspace-stack-projection";
12​
13// Load the same router as native navigation without importing React Native into Node.
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("v5 stack handoff", () => {
132 it("releases a completed native pop when the router acknowledges it", () => {
133 const completed = new Set([files.key]);
134 expect(reconcileStackScreens([home, thread, files], [home, thread, files], completed)).toEqual([
135 home,
136 thread,
137 files,
138 ]);
139 expect(reconcileStackScreens([home, thread, files], [home, thread], completed)).toEqual([
140 home,
141 thread,
142 ]);
143 });
144 it("still retains an unfinished JS pop beside a completed native pop", () => {
145 expect(reconcileStackScreens([home, thread, files], [home], new Set([files.key]))).toEqual([
146 thread,
147 home,
148 ]);
149 });
150 it("retains removed native screens through a pop followed immediately by a push", () => {
151 const popped = reconcileStackScreens([home, thread, files], [home, thread]);
152 const next = { ...files, key: "new-files" };
153 const screens = reconcileStackScreens(popped, [home, thread, next]);
154 expect(screens.map((route) => route.key)).toEqual(["files", "home", "thread", "new-files"]);
155 expect(screens.filter((route) => [home, thread, next].includes(route))).toEqual([
156 home,
157 thread,
158 next,
159 ]);
160 expect(screens[0]).toBe(files);
161 });
162 it("updates params without retaining duplicate copies of the same screen", () => {
163 const updated = { ...thread, params: { threadId: "another-thread" } };
164 expect(reconcileStackScreens([home, thread], [home, updated])).toEqual([home, updated]);
165 });
166 it("keeps card pushes inside the modal they belong to", () => {
167 const secondModal = { ...settings, key: "another-settings" };
168 expect(
169 partitionStackPresentations(
170 [home, thread, settings, legal, secondModal],
171 (route) => route.name === "SettingsSheet",
172 ),
173 ).toEqual([[home, thread], [settings, legal], [secondModal]]);
174 });
175});