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

home-thread-navigation.test.ts

238 lines8.2 KB

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