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

android-workspace-navigation.test.ts

275 lines9.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 "SettingsEnvironments",
41 "SettingsAppearance",
42 "SettingsOpenSourceLicenses",
43 "SettingsUsage",
44 "SettingsUsageAccount",
45 ],
46 routeParamList: {},
47 routeGetIdList: {},
48};
49const thread = {
50 name: "Thread",
51 params: { environmentId: "environment-a", threadId: "thread-a" },
52} as const;
53const pullRequest = {
54 name: "PullRequest",
55 params: {
56 environmentId: "environment-a",
57 projectId: "project-a",
58 repository: "owner/repository",
59 number: 42,
60 host: "github.com",
61 },
62} as const;
63​
64function apply(
65 state: ReturnType<typeof router.getInitialState>,
66 action: Parameters<typeof router.getStateForAction>[1],
67) {
68 const result = router.getStateForAction(state, action, options);
69 if (!result) throw new Error(`Router rejected ${action.type}`);
70 return router.getRehydratedState(result, options);
72​
73function withThread() {
74 return apply(router.getInitialState(options), StackActions.push(thread.name, thread.params));
76​
77function legacyHistory() {
78 let state = router.getInitialState(options);
79 state = apply(state, StackActions.push(thread.name, thread.params));
80 state = apply(state, StackActions.push(pullRequest.name, pullRequest.params));
81 return state;
83​
84function names(state: ReturnType<typeof router.getInitialState>) {
85 return state.routes.map((route) => route.name);
87​
88describe("Android workspace navigation", () => {
89 it("returns to a distinct previous thread, otherwise to the workspace list", () => {
90 const parent = withThread();
91 let state = apply(
92 parent,
93 StackActions.push("Thread", { ...thread.params, threadId: "child-a" }),
94 );
95 let listSelected = false;
96 const navigation = {
97 dispatch: (action: RootStackResetAction) => {
98 state = apply(state, action);
99 },
100 getParent: () => undefined,
101 getState: () => state,
102 goBack: () => {
103 state = apply(state, StackActions.pop());
104 },
105 };
106 const back = () =>
107 returnToAndroidWorkspaceList(navigation, () => {
108 listSelected = true;
109 });
110​
111 back();
112 expect(state.routes).toEqual(parent.routes);
113 expect(state.index).toBe(parent.index);
114 expect(listSelected).toBe(false);
115​
116 back();
117 expect(state.routes).toEqual([parent.routes[0]]);
118 expect(state.index).toBe(0);
119 expect(listSelected).toBe(true);
120​
121 state = apply(
122 parent,
123 StackActions.push("Thread", { ...thread.params, environmentId: "environment-b" }),
124 );
125 listSelected = false;
126 back();
127 expect(state.routes).toEqual(parent.routes);
128 expect(state.index).toBe(parent.index);
129 expect(listSelected).toBe(false);
130​
131 // Confirming a Git action from the inline inspector can duplicate the current thread.
132 state = apply(parent, StackActions.push("Thread", thread.params));
133 back();
134 expect({ routes: names(state), index: state.index, listSelected }).toEqual({
135 routes: ["Home"],
136 index: 0,
137 listSelected: true,
138 });
139 });
140​
141 it("opens a linked thread above the current one so Back returns to it", () => {
142 const parent = withThread();
143 const child = { ...thread.params, threadId: "child-a" };
144 const open = (
145 state: ReturnType<typeof router.getInitialState>,
146 target: ReactNavigation.RootParamList["Thread"],
147 ) => {
148 const step = linkedThreadStep(state, target);
149 if (step === "stay") return state;
150 return apply(
151 state,
152 step === "pop" ? StackActions.pop() : StackActions.push("Thread", target),
153 );
154 };
155​
156 const inChild = open(parent, child);
157 expect(inChild.routes.map((route) => route.params)).toEqual([undefined, thread.params, child]);
158 expect(open(inChild, child)).toBe(inChild);
159​
160 const backInParent = open(inChild, thread.params);
161 expect(backInParent.routes).toEqual(parent.routes);
162 expect(backInParent.index).toBe(parent.index);
163 });
164​
165 it("recognizes a parent only when its environment and thread are immediately below", () => {
166 const parent = withThread();
167 const child = apply(
168 parent,
169 StackActions.push("Thread", { ...thread.params, threadId: "child-a" }),
170 );
171​
172 expect(parentThreadIsBelow(child, thread.params)).toBe(true);
173 expect(parentThreadIsBelow(child, { ...thread.params, environmentId: "environment-b" })).toBe(
174 false,
175 );
176 expect(parentThreadIsBelow(child, { ...thread.params, threadId: "other" })).toBe(false);
177 expect(parentThreadIsBelow(parent, thread.params)).toBe(false);
178 expect(
179 parentThreadIsBelow(
180 apply(child, StackActions.push("Thread", { ...thread.params, threadId: "grandchild-a" })),
181 thread.params,
182 ),
183 ).toBe(false);
184 });
185​
186 it("resets the root stack when a nested stack has focus", () => {
187 const root = apply(router.getInitialState(options), StackActions.push("Thread", thread.params));
188 const nested = router.getInitialState({ ...options, routeNames: ["Nested", "NestedChild"] });
189 const actions: RootStackResetAction[] = [];
190 const rootNavigation = {
191 dispatch: () => undefined,
192 getParent: () => undefined,
193 getState: () => root,
194 };
195 resetAndroidWorkspace({
196 dispatch: (action) => actions.push(action),
197 getParent: () => rootNavigation,
198 getState: () => nested,
199 });
200 const [action] = actions;
201 expect(action?.target).toBe(root.key);
202 expect(names(apply(root, action!))).toEqual(["Home"]);
203 });
204​
205 it("resets from the navigation container before any navigation action", () => {
206 const root = router.getInitialState(options);
207 const actions: RootStackResetAction[] = [];
208 // The container only learns the root state after the first action.
209 resetAndroidWorkspace({
210 dispatch: (action) => actions.push(action),
211 getParent: () => undefined,
212 getState: () => undefined,
213 getRootState: () => root,
214 });
215 const [action] = actions;
216 expect(action?.target).toBe(root.key);
217 expect(names(apply(root, action!))).toEqual(["Home"]);
218 });
219});
220​
221describe("Android Settings tab", () => {
222 type State = ReturnType<typeof router.getInitialState>;
223 type Tab = AndroidWorkspaceNavigationTab;
224 const openTab = (state: State, tab: Tab, columns = false) =>
225 apply(state, CommonActions.reset(androidWorkspaceTabNavigationState(state, tab, columns)));
226 const selectedTab = (state: State, previous: Tab) =>
227 androidWorkspaceTabFromNavigationState(state, previous);
228​
229 it("opens a default Settings page beside the category list in wide layouts", () => {
230 const settings = openTab(legacyHistory(), "settings", true);
231 expect(names(settings)).toEqual(["Home", "Settings", "SettingsEnvironments"]);
232 expect(selectedTab(settings, "threads")).toBe("settings");
233 expect(names(apply(settings, StackActions.pop()))).toEqual(["Home", "Settings"]);
234 });
235​
236 it("keeps the selected category mounted when reselecting wide Settings", () => {
237 let settings = openTab(legacyHistory(), "settings");
238 settings = apply(settings, StackActions.push("SettingsAppearance"));
239 const category = settings.routes.at(-1);
240 settings = apply(settings, StackActions.push("SettingsOpenSourceLicenses"));
241 const reselected = openTab(settings, "settings", true);
242 expect(names(reselected)).toEqual(["Home", "Settings", "SettingsAppearance"]);
243 expect(reselected.routes.at(-1)?.key).toBe(category?.key);
244 });
245​
246 it("opens over Home alone, dropping chat and PR history, and Back returns to Home", () => {
247 const previous = legacyHistory();
248 const settings = openTab(previous, "settings");
249 expect(names(settings)).toEqual(["Home", "Settings"]);
250 expect(settings.routes[0]?.key).toBe(previous.routes[0]?.key);
251 expect(selectedTab(settings, "threads")).toBe("settings");
252 const back = apply(settings, StackActions.pop());
253 expect(names(back)).toEqual(["Home"]);
254 expect(back.routes[0]?.key).toBe(previous.routes[0]?.key);
255 expect(selectedTab(back, "settings")).not.toBe("settings");
256 });
257​
258 it("stays selected under a settings page until a list tab returns to Home", () => {
259 const previous = legacyHistory();
260 const usage = apply(openTab(previous, "settings"), StackActions.push("SettingsUsage"));
261 expect(selectedTab(usage, "threads")).toBe("settings");
262 const list = openTab(usage, "pullRequests");
263 expect(names(list)).toEqual(["Home"]);
264 expect(list.routes[0]?.key).toBe(previous.routes[0]?.key);
265 expect(selectedTab(list, "pullRequests")).toBe("pullRequests");
266 });
267​
268 it("leaves a settings page opened from the Usage tab in that tab", () => {
269 const account = apply(
270 router.getInitialState(options),
271 StackActions.push("SettingsUsageAccount"),
272 );
273 expect(selectedTab(account, "usage")).toBe("usage");
274 });
275});