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

android-workspace-navigation.test.ts

255 lines8.9 KB

1import * as NodeModule from "node:module";
2import { describe, expect, it } from "vite-plus/test";
3import {
4 androidWorkspaceTabFromNavigationState,
5 androidWorkspaceTabNavigationState,
6 type AndroidWorkspaceNavigationTab,
7} from "../features/workspace/android-workspace-tabs";
8import {
9 linkedThreadStep,
10 parentThreadIsBelow,
11 resetAndroidWorkspace,
12 returnToAndroidWorkspaceList,
13} from "./android-workspace-navigation";
14import type { RootStackResetAction } from "./root-stack-reset";
15​
16const require = NodeModule.createRequire(import.meta.url);
17const nativePackage = require.resolve("@react-navigation/native/package.json");
18const requireFromNative = NodeModule.createRequire(nativePackage);
19const corePackage = requireFromNative.resolve("@react-navigation/core/package.json");
20const requireFromCore = NodeModule.createRequire(corePackage);
21const { CommonActions, StackActions, StackRouter } = requireFromCore(
22 "@react-navigation/routers",
23) as {
24 readonly CommonActions: typeof import("@react-navigation/native").CommonActions;
25 readonly StackActions: typeof import("@react-navigation/native").StackActions;
26 readonly StackRouter: typeof import("@react-navigation/native").StackRouter;
27};
28const router = StackRouter({});
29const options = {
30 routeNames: [
31 "Home",
32 "Thread",
33 "ThreadFiles",
34 "ThreadTerminal",
35 "GitOverview",
36 "PullRequest",
37 "PullRequestFile",
38 "PullRequestReviewers",
39 "Settings",
40 "SettingsUsage",
41 "SettingsUsageAccount",
42 ],
43 routeParamList: {},
44 routeGetIdList: {},
45};
46const thread = {
47 name: "Thread",
48 params: { environmentId: "environment-a", threadId: "thread-a" },
49} as const;
50const pullRequest = {
51 name: "PullRequest",
52 params: {
53 environmentId: "environment-a",
54 projectId: "project-a",
55 repository: "owner/repository",
56 number: 42,
57 host: "github.com",
58 },
59} as const;
60​
61function apply(
62 state: ReturnType<typeof router.getInitialState>,
63 action: Parameters<typeof router.getStateForAction>[1],
64) {
65 const result = router.getStateForAction(state, action, options);
66 if (!result) throw new Error(`Router rejected ${action.type}`);
67 return router.getRehydratedState(result, options);
69​
70function withThread() {
71 return apply(router.getInitialState(options), StackActions.push(thread.name, thread.params));
73​
74function legacyHistory() {
75 let state = router.getInitialState(options);
76 state = apply(state, StackActions.push(thread.name, thread.params));
77 state = apply(state, StackActions.push(pullRequest.name, pullRequest.params));
78 return state;
80​
81function names(state: ReturnType<typeof router.getInitialState>) {
82 return state.routes.map((route) => route.name);
84​
85describe("Android workspace navigation", () => {
86 it("returns to a distinct previous thread, otherwise to the workspace list", () => {
87 const parent = withThread();
88 let state = apply(
89 parent,
90 StackActions.push("Thread", { ...thread.params, threadId: "child-a" }),
91 );
92 let listSelected = false;
93 const navigation = {
94 dispatch: (action: RootStackResetAction) => {
95 state = apply(state, action);
96 },
97 getParent: () => undefined,
98 getState: () => state,
99 goBack: () => {
100 state = apply(state, StackActions.pop());
101 },
102 };
103 const back = () =>
104 returnToAndroidWorkspaceList(navigation, () => {
105 listSelected = true;
106 });
107​
108 back();
109 expect(state.routes).toEqual(parent.routes);
110 expect(state.index).toBe(parent.index);
111 expect(listSelected).toBe(false);
112​
113 back();
114 expect(state.routes).toEqual([parent.routes[0]]);
115 expect(state.index).toBe(0);
116 expect(listSelected).toBe(true);
117​
118 state = apply(
119 parent,
120 StackActions.push("Thread", { ...thread.params, environmentId: "environment-b" }),
121 );
122 listSelected = false;
123 back();
124 expect(state.routes).toEqual(parent.routes);
125 expect(state.index).toBe(parent.index);
126 expect(listSelected).toBe(false);
127​
128 // Confirming a Git action from the inline inspector can duplicate the current thread.
129 state = apply(parent, StackActions.push("Thread", thread.params));
130 back();
131 expect({ routes: names(state), index: state.index, listSelected }).toEqual({
132 routes: ["Home"],
133 index: 0,
134 listSelected: true,
135 });
136 });
137​
138 it("opens a linked thread above the current one so Back returns to it", () => {
139 const parent = withThread();
140 const child = { ...thread.params, threadId: "child-a" };
141 const open = (
142 state: ReturnType<typeof router.getInitialState>,
143 target: ReactNavigation.RootParamList["Thread"],
144 ) => {
145 const step = linkedThreadStep(state, target);
146 if (step === "stay") return state;
147 return apply(
148 state,
149 step === "pop" ? StackActions.pop() : StackActions.push("Thread", target),
150 );
151 };
152​
153 const inChild = open(parent, child);
154 expect(inChild.routes.map((route) => route.params)).toEqual([undefined, thread.params, child]);
155 expect(open(inChild, child)).toBe(inChild);
156​
157 const backInParent = open(inChild, thread.params);
158 expect(backInParent.routes).toEqual(parent.routes);
159 expect(backInParent.index).toBe(parent.index);
160 });
161​
162 it("recognizes a parent only when its environment and thread are immediately below", () => {
163 const parent = withThread();
164 const child = apply(
165 parent,
166 StackActions.push("Thread", { ...thread.params, threadId: "child-a" }),
167 );
168​
169 expect(parentThreadIsBelow(child, thread.params)).toBe(true);
170 expect(parentThreadIsBelow(child, { ...thread.params, environmentId: "environment-b" })).toBe(
171 false,
172 );
173 expect(parentThreadIsBelow(child, { ...thread.params, threadId: "other" })).toBe(false);
174 expect(parentThreadIsBelow(parent, thread.params)).toBe(false);
175 expect(
176 parentThreadIsBelow(
177 apply(child, StackActions.push("Thread", { ...thread.params, threadId: "grandchild-a" })),
178 thread.params,
179 ),
180 ).toBe(false);
181 });
182​
183 it("resets the root stack when a nested stack has focus", () => {
184 const root = apply(router.getInitialState(options), StackActions.push("Thread", thread.params));
185 const nested = router.getInitialState({ ...options, routeNames: ["Nested", "NestedChild"] });
186 const actions: RootStackResetAction[] = [];
187 const rootNavigation = {
188 dispatch: () => undefined,
189 getParent: () => undefined,
190 getState: () => root,
191 };
192 resetAndroidWorkspace({
193 dispatch: (action) => actions.push(action),
194 getParent: () => rootNavigation,
195 getState: () => nested,
196 });
197 const [action] = actions;
198 expect(action?.target).toBe(root.key);
199 expect(names(apply(root, action!))).toEqual(["Home"]);
200 });
201​
202 it("resets from the navigation container before any navigation action", () => {
203 const root = router.getInitialState(options);
204 const actions: RootStackResetAction[] = [];
205 // The container only learns the root state after the first action.
206 resetAndroidWorkspace({
207 dispatch: (action) => actions.push(action),
208 getParent: () => undefined,
209 getState: () => undefined,
210 getRootState: () => root,
211 });
212 const [action] = actions;
213 expect(action?.target).toBe(root.key);
214 expect(names(apply(root, action!))).toEqual(["Home"]);
215 });
216});
217​
218describe("Android Settings tab", () => {
219 type State = ReturnType<typeof router.getInitialState>;
220 type Tab = AndroidWorkspaceNavigationTab;
221 const openTab = (state: State, tab: Tab) =>
222 apply(state, CommonActions.reset(androidWorkspaceTabNavigationState(state, tab)));
223 const selectedTab = (state: State, previous: Tab) =>
224 androidWorkspaceTabFromNavigationState(state, previous);
225​
226 it("opens over Home alone, dropping chat and PR history, and Back returns to Home", () => {
227 const previous = legacyHistory();
228 const settings = openTab(previous, "settings");
229 expect(names(settings)).toEqual(["Home", "Settings"]);
230 expect(settings.routes[0]?.key).toBe(previous.routes[0]?.key);
231 expect(selectedTab(settings, "threads")).toBe("settings");
232 const back = apply(settings, StackActions.pop());
233 expect(names(back)).toEqual(["Home"]);
234 expect(back.routes[0]?.key).toBe(previous.routes[0]?.key);
235 expect(selectedTab(back, "settings")).not.toBe("settings");
236 });
237​
238 it("stays selected under a settings page until a list tab returns to Home", () => {
239 const previous = legacyHistory();
240 const usage = apply(openTab(previous, "settings"), StackActions.push("SettingsUsage"));
241 expect(selectedTab(usage, "threads")).toBe("settings");
242 const list = openTab(usage, "pullRequests");
243 expect(names(list)).toEqual(["Home"]);
244 expect(list.routes[0]?.key).toBe(previous.routes[0]?.key);
245 expect(selectedTab(list, "pullRequests")).toBe("pullRequests");
246 });
247​
248 it("leaves a settings page opened from the Usage tab in that tab", () => {
249 const account = apply(
250 router.getInitialState(options),
251 StackActions.push("SettingsUsageAccount"),
252 );
253 expect(selectedTab(account, "usage")).toBe("usage");
254 });
255});