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

home-thread-navigation.test.ts

196 lines7.0 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-navigation/native", () => {
23 const { CommonActions, StackActions } = loadRouters();
24 return { CommonActions, StackActions };
25});
26​
27import { createHomeThreadNavigationAction } from "./home-thread-navigation";
28​
29const { StackActions, StackRouter } = loadRouters();
30const routeNames = ["Home", "Thread"];
31const routeParamList = {
32 Home: undefined,
33 Thread: undefined,
34};
35const router = StackRouter({});
36const routerOptions = {
37 routeNames,
38 routeParamList,
39 routeGetIdList: {},
40};
41​
42type ThreadSelection = Parameters<typeof createHomeThreadNavigationAction>[0]["thread"];
43​
44function thread(id: string): ThreadSelection {
45 return {
46 environmentId: "environment-1",
47 id,
48 } as ThreadSelection;
50​
51function initialState() {
52 return router.getInitialState(routerOptions);
54​
55function apply(
56 state: StackNavigationState<Record<string, object | undefined>>,
57 action: Parameters<typeof router.getStateForAction>[1],
58) {
59 const nextState = router.getStateForAction(state, action, routerOptions);
60 expect(nextState).not.toBeNull();
61 return nextState as StackNavigationState<Record<string, object | undefined>>;
63​
64function selectThread(
65 state: ReturnType<typeof initialState>,
66 selectedThread: ThreadSelection,
67 dismissingRouteKey: string | null = null,
68) {
69 return apply(
70 state,
71 createHomeThreadNavigationAction({
72 state,
73 dismissingRouteKey,
74 thread: selectedThread,
75 }),
76 );
78​
79function dismissRoute(state: ReturnType<typeof initialState>, routeKey: string) {
80 return apply(state, {
81 ...StackActions.pop(),
82 source: routeKey,
83 target: state.key,
84 });
86​
87describe("createHomeThreadNavigationAction", () => {
88 it("coalesces ordinary repeat selections onto the current thread route", () => {
89 const firstState = selectThread(initialState(), thread("thread-a"));
90 const threadRouteKey = firstState.routes[firstState.index]?.key;
91 const secondAction = createHomeThreadNavigationAction({
92 state: firstState,
93 dismissingRouteKey: null,
94 thread: thread("thread-b"),
95 });
96​
97 const secondState = apply(firstState, secondAction);
98 expect(secondState.routes).toHaveLength(2);
99 expect(secondState.routes[secondState.index]).toMatchObject({
100 key: threadRouteKey,
101 name: "Thread",
102 params: { environmentId: "environment-1", threadId: "thread-b" },
103 });
104 });
105​
106 it("keeps an overlap selection after native dismisses the outgoing route", () => {
107 const outgoingState = selectThread(initialState(), thread("thread-a"));
108 const outgoingRouteKey = outgoingState.routes[outgoingState.index]?.key;
109 expect(outgoingRouteKey).toBeDefined();
110​
111 const overlapAction = createHomeThreadNavigationAction({
112 state: outgoingState,
113 dismissingRouteKey: outgoingRouteKey ?? null,
114 thread: thread("thread-b"),
115 });
116 const overlapState = apply(outgoingState, overlapAction);
117 const incomingRoute = overlapState.routes[overlapState.index];
118 expect(incomingRoute?.key).not.toBe(outgoingRouteKey);
119​
120 const dismissedState = dismissRoute(overlapState, outgoingRouteKey!);
121 expect(dismissedState.routes).toHaveLength(2);
122 expect(dismissedState.routes[dismissedState.index]).toMatchObject({
123 key: incomingRoute?.key,
124 name: "Thread",
125 params: { environmentId: "environment-1", threadId: "thread-b" },
126 });
127 });
128​
129 it("uses a fresh key for the same thread selected during dismissal", () => {
130 const outgoingState = selectThread(initialState(), thread("thread-a"));
131 const outgoingRouteKey = outgoingState.routes[outgoingState.index]?.key;
132 expect(outgoingRouteKey).toBeDefined();
133​
134 const overlapState = selectThread(outgoingState, thread("thread-a"), outgoingRouteKey ?? null);
135 const incomingRouteKey = overlapState.routes[overlapState.index]?.key;
136 expect(incomingRouteKey).not.toBe(outgoingRouteKey);
137​
138 const dismissedState = dismissRoute(overlapState, outgoingRouteKey!);
139 expect(dismissedState.routes[dismissedState.index]).toMatchObject({
140 key: incomingRouteKey,
141 params: { environmentId: "environment-1", threadId: "thread-a" },
142 });
143 });
144​
145 it("coalesces a second overlap selection onto the fresh incoming route", () => {
146 const outgoingState = selectThread(initialState(), thread("thread-a"));
147 const outgoingRouteKey = outgoingState.routes[outgoingState.index]?.key;
148 expect(outgoingRouteKey).toBeDefined();
149​
150 const firstOverlapState = selectThread(
151 outgoingState,
152 thread("thread-b"),
153 outgoingRouteKey ?? null,
154 );
155 const incomingRouteKey = firstOverlapState.routes[firstOverlapState.index]?.key;
156 const secondOverlapAction = createHomeThreadNavigationAction({
157 state: firstOverlapState,
158 dismissingRouteKey: outgoingRouteKey ?? null,
159 thread: thread("thread-c"),
160 });
161​
162 const secondOverlapState = apply(firstOverlapState, secondOverlapAction);
163 expect(secondOverlapState.routes).toHaveLength(3);
164 expect(secondOverlapState.routes[secondOverlapState.index]).toMatchObject({
165 key: incomingRouteKey,
166 params: { environmentId: "environment-1", threadId: "thread-c" },
167 });
168​
169 const dismissedState = dismissRoute(secondOverlapState, outgoingRouteKey!);
170 expect(dismissedState.routes).toHaveLength(2);
171 expect(dismissedState.routes[dismissedState.index]).toMatchObject({
172 key: incomingRouteKey,
173 params: { environmentId: "environment-1", threadId: "thread-c" },
174 });
175 });
176​
177 it("uses ordinary navigation when native pops before the selection", () => {
178 const outgoingState = selectThread(initialState(), thread("thread-a"));
179 const outgoingRouteKey = outgoingState.routes[outgoingState.index]?.key;
180 expect(outgoingRouteKey).toBeDefined();
181​
182 const poppedState = dismissRoute(outgoingState, outgoingRouteKey!);
183 const action = createHomeThreadNavigationAction({
184 state: poppedState,
185 dismissingRouteKey: outgoingRouteKey ?? null,
186 thread: thread("thread-b"),
187 });
188​
189 const selectedState = apply(poppedState, action);
190 expect(selectedState.routes).toHaveLength(2);
191 expect(selectedState.routes[selectedState.index]).toMatchObject({
192 name: "Thread",
193 params: { environmentId: "environment-1", threadId: "thread-b" },
194 });
195 });
196});