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

android-workspace-navigation.test.ts

259 lines9.2 KB

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