Open History to see the last commit
1import * as NodeModule from "node:module";
2import type {
3 StackNavigationState,
4 StackRouter as StackRouterType,
5 StackActions as StackActionsType,
6} from "@react-navigation/native";
7import { describe, expect, it, vi } from "vite-plus/test";
8
9function loadRouters() {
10 const require = NodeModule.createRequire(import.meta.url);
11 const nativePackage = require.resolve("@react-navigation/native/package.json");
12 const requireFromNative = NodeModule.createRequire(nativePackage);
13 const corePackage = requireFromNative.resolve("@react-navigation/core/package.json");
14 const requireFromCore = NodeModule.createRequire(corePackage);
15 return requireFromCore("@react-navigation/routers") as {
16 readonly CommonActions: typeof import("@react-navigation/native").CommonActions;
17 readonly StackActions: typeof StackActionsType;
18 readonly StackRouter: typeof StackRouterType;
19 };
20}
21
22vi.mock("react-native", () => ({ Platform: { OS: "ios" }, Keyboard: { dismiss: vi.fn() } }));
23
24vi.mock("@react-navigation/native", () => {
25 const { CommonActions, StackActions } = loadRouters();
26 return { CommonActions, StackActions };
27});
28
29import {
30 createHomeThreadRouteAction,
31 createHomeThreadNavigationAction,
32} from "./home-thread-navigation";
33
34const { StackActions, StackRouter } = loadRouters();
35const routeNames = ["Home", "Thread"];
36const routeParamList = {
37 Home: undefined,
38 Thread: undefined,
39};
40const router = StackRouter({});
41const routerOptions = {
42 routeNames,
43 routeParamList,
44 routeGetIdList: {},
45};
46
47type ThreadSelection = Parameters<typeof createHomeThreadNavigationAction>[0]["thread"];
48
49function thread(id: string): ThreadSelection {
50 return {
51 environmentId: "environment-1",
52 id,
53 } as ThreadSelection;
54}
55
56function initialState() {
57 return router.getInitialState(routerOptions);
58}
59
60function apply(
61 state: StackNavigationState<Record<string, object | undefined>>,
62 action: Parameters<typeof router.getStateForAction>[1],
63) {
64 const nextState = router.getStateForAction(state, action, routerOptions);
65 expect(nextState).not.toBeNull();
66 return nextState as StackNavigationState<Record<string, object | undefined>>;
67}
68
69function selectThread(
70 state: ReturnType<typeof initialState>,
71 selectedThread: ThreadSelection,
72 dismissingRouteKey: string | null = null,
73) {
74 return apply(
75 state,
76 createHomeThreadNavigationAction({
77 state,
78 dismissingRouteKey,
79 thread: selectedThread,
80 }),
81 );
82}
83
84function dismissRoute(state: ReturnType<typeof initialState>, routeKey: string) {
85 return apply(state, {
86 ...StackActions.pop(),
87 source: routeKey,
88 target: state.key,
89 });
90}
91
92describe("createHomeThreadNavigationAction", () => {
93 it("coalesces ordinary repeat selections onto the current thread route", () => {
94 const firstState = selectThread(initialState(), thread("thread-a"));
95 const threadRouteKey = firstState.routes[firstState.index]?.key;
96 const secondAction = createHomeThreadNavigationAction({
97 state: firstState,
98 dismissingRouteKey: null,
99 thread: thread("thread-b"),
100 });
101
102 const secondState = apply(firstState, secondAction);
103 expect(secondState.routes).toHaveLength(2);
104 expect(secondState.routes[secondState.index]).toMatchObject({
105 key: threadRouteKey,
106 name: "Thread",
107 params: { environmentId: "environment-1", threadId: "thread-b" },
108 });
109 });
110
111 it("keeps an overlap selection after native dismisses the outgoing route", () => {
112 const outgoingState = selectThread(initialState(), thread("thread-a"));
113 const outgoingRouteKey = outgoingState.routes[outgoingState.index]?.key;
114 expect(outgoingRouteKey).toBeDefined();
115
116 const overlapAction = createHomeThreadNavigationAction({
117 state: outgoingState,
118 dismissingRouteKey: outgoingRouteKey ?? null,
119 thread: thread("thread-b"),
120 });
121 const overlapState = apply(outgoingState, overlapAction);
122 const incomingRoute = overlapState.routes[overlapState.index];
123 expect(incomingRoute?.key).not.toBe(outgoingRouteKey);
124
125 const dismissedState = dismissRoute(overlapState, outgoingRouteKey!);
126 expect(dismissedState.routes).toHaveLength(2);
127 expect(dismissedState.routes[dismissedState.index]).toMatchObject({
128 key: incomingRoute?.key,
129 name: "Thread",
130 params: { environmentId: "environment-1", threadId: "thread-b" },
131 });
132 });
133
134 it("uses a fresh key for the same thread selected during dismissal", () => {
135 const outgoingState = selectThread(initialState(), thread("thread-a"));
136 const outgoingRouteKey = outgoingState.routes[outgoingState.index]?.key;
137 expect(outgoingRouteKey).toBeDefined();
138
139 const overlapState = selectThread(outgoingState, thread("thread-a"), outgoingRouteKey ?? null);
140 const incomingRouteKey = overlapState.routes[overlapState.index]?.key;
141 expect(incomingRouteKey).not.toBe(outgoingRouteKey);
142
143 const dismissedState = dismissRoute(overlapState, outgoingRouteKey!);
144 expect(dismissedState.routes[dismissedState.index]).toMatchObject({
145 key: incomingRouteKey,
146 params: { environmentId: "environment-1", threadId: "thread-a" },
147 });
148 });
149
150 it("coalesces a second overlap selection onto the fresh incoming route", () => {
151 const outgoingState = selectThread(initialState(), thread("thread-a"));
152 const outgoingRouteKey = outgoingState.routes[outgoingState.index]?.key;
153 expect(outgoingRouteKey).toBeDefined();
154
155 const firstOverlapState = selectThread(
156 outgoingState,
157 thread("thread-b"),
158 outgoingRouteKey ?? null,
159 );
160 const incomingRouteKey = firstOverlapState.routes[firstOverlapState.index]?.key;
161 const secondOverlapAction = createHomeThreadNavigationAction({
162 state: firstOverlapState,
163 dismissingRouteKey: outgoingRouteKey ?? null,
164 thread: thread("thread-c"),
165 });
166
167 const secondOverlapState = apply(firstOverlapState, secondOverlapAction);
168 expect(secondOverlapState.routes).toHaveLength(3);
169 expect(secondOverlapState.routes[secondOverlapState.index]).toMatchObject({
170 key: incomingRouteKey,
171 params: { environmentId: "environment-1", threadId: "thread-c" },
172 });
173
174 const dismissedState = dismissRoute(secondOverlapState, outgoingRouteKey!);
175 expect(dismissedState.routes).toHaveLength(2);
176 expect(dismissedState.routes[dismissedState.index]).toMatchObject({
177 key: incomingRouteKey,
178 params: { environmentId: "environment-1", threadId: "thread-c" },
179 });
180 });
181
182 it("uses ordinary navigation when native pops before the selection", () => {
183 const outgoingState = selectThread(initialState(), thread("thread-a"));
184 const outgoingRouteKey = outgoingState.routes[outgoingState.index]?.key;
185 expect(outgoingRouteKey).toBeDefined();
186
187 const poppedState = dismissRoute(outgoingState, outgoingRouteKey!);
188 const action = createHomeThreadNavigationAction({
189 state: poppedState,
190 dismissingRouteKey: outgoingRouteKey ?? null,
191 thread: thread("thread-b"),
192 });
193
194 const selectedState = apply(poppedState, action);
195 expect(selectedState.routes).toHaveLength(2);
196 expect(selectedState.routes[selectedState.index]).toMatchObject({
197 name: "Thread",
198 params: { environmentId: "environment-1", threadId: "thread-b" },
199 });
200 });
201});
202
203describe("draft and live thread selection", () => {
204 it("keeps the screen key and clears old identity when switching both ways", () => {
205 const live = selectThread(initialState(), thread("thread-a"));
206 const key = live.routes[live.index]!.key;
207 const draft = apply(
208 live,
209 createHomeThreadRouteAction({
210 state: live,
211 dismissingRouteKey: null,
212 params: { draft: { draftId: "new-task:draft-a" } },
213 }),
214 );
215 expect(draft.routes[draft.index]).toMatchObject({
216 key,
217 params: {
218 environmentId: undefined,
219 threadId: undefined,
220 draft: { draftId: "new-task:draft-a" },
221 },
222 });
223 const restored = selectThread(draft, thread("thread-b"));
224 expect(restored.routes[restored.index]).toMatchObject({
225 key,
226 params: {
227 environmentId: "environment-1",
228 threadId: "thread-b",
229 draft: undefined,
230 },
231 });
232 expect(restored.routes).toHaveLength(live.routes.length);
233 });
234});