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

workspace-tabs-state.test.ts

316 lines13.6 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import { act, createElement, createContext, useState, type ReactNode } from "react";
3import { createRoot } from "react-dom/client";
4import { WorkspaceRootScreen } from "./WorkspaceRootScreen.android";
5import {
6 initialWorkspaceTabsState,
7 workspaceTabsReducer,
8 shouldRestoreNativeSearch,
9 workspaceTabForNativeSlot,
10 resolveWorkspaceTabOrder,
11 workspaceSearchBottomClearance,
12 workspaceDockBottomClearance,
13 workspaceUsesColumns,
14 type WorkspaceTab,
15 type WorkspaceTabsState,
16} from "./workspace-tabs-state";
17​
18describe("compact list clearance", () => {
19 it.each([
20 { bottomInset: 34, keyboardHeight: 300, clearance: 380 },
21 { bottomInset: 34, keyboardHeight: 0, clearance: 114 },
22 { bottomInset: 34, keyboardHeight: 20, clearance: 114 },
23 ])("keeps the last result above the keyboard and dock: %j", ({ clearance, ...input }) => {
24 expect(workspaceDockBottomClearance(input)).toBe(clearance);
25 });
26});
27​
28describe("Duo list clearance", () => {
29 it.each([18, 60])(
30 "scrolls the last row above search with a %s-point native bar inset",
31 (bottomBarInset) => {
32 const clearance = workspaceSearchBottomClearance({
33 bottomInset: 34,
34 bottomBarInset,
35 keyboardHeight: 0,
36 });
37 expect(clearance).toBe(bottomBarInset + 48 + 16);
38 },
39 );
40​
41 it("keeps the last row above search when the keyboard lifts the field", () => {
42 expect(
43 workspaceSearchBottomClearance({ bottomInset: 34, bottomBarInset: 18, keyboardHeight: 300 }),
44 ).toBe(300 + 8 + 48 + 16);
45 });
46​
47 it("falls back to the field's safe-area spacing before native bar metrics arrive", () => {
48 expect(workspaceSearchBottomClearance({ bottomInset: 34, keyboardHeight: 0 })).toBe(
49 34 + 48 + 16,
50 );
51 expect(workspaceSearchBottomClearance({ bottomInset: 0, keyboardHeight: 0 })).toBe(
52 12 + 48 + 16,
53 );
54 });
55});
56​
57describe("workspace root navigation", () => {
58 it("pairs Settings, chats and pull requests with a detail column, and keeps Usage one page", () => {
59 expect(workspaceUsesColumns("usage")).toBe(false);
60 expect(workspaceUsesColumns("usage", true)).toBe(true);
61 expect(workspaceUsesColumns("threads")).toBe(true);
62 expect(workspaceUsesColumns("pullRequests")).toBe(true);
63 });
64​
65 it("keeps the registered defaults while customization is unavailable", () => {
66 expect(resolveWorkspaceTabOrder(undefined)).toEqual(["threads", "pullRequests", "usage"]);
67 expect(resolveWorkspaceTabOrder(["home", "usage", "issues", "threads"])).toEqual(
68 resolveWorkspaceTabOrder(undefined),
69 );
70 });
71​
72 it("retains separate queries and positions across tabs, and cancels only the current search", () => {
73 let state = initialWorkspaceTabsState();
74 state = workspaceTabsReducer(state, { type: "scroll", tab: "threads", offset: 640 });
75 state = workspaceTabsReducer(state, { type: "search" });
76 state = workspaceTabsReducer(state, { type: "query", query: "keyboard" });
77 state = workspaceTabsReducer(state, { type: "scroll", tab: "threads", offset: 160 });
78 state = workspaceTabsReducer(state, { type: "expand" });
79 state = workspaceTabsReducer(state, { type: "select", tab: "pullRequests" });
80 expect(state.tabs.pullRequests.query).toBe("");
81 state = workspaceTabsReducer(state, { type: "scroll", tab: "pullRequests", offset: 240 });
82 state = workspaceTabsReducer(state, { type: "search" });
83 state = workspaceTabsReducer(state, { type: "query", query: "navigation" });
84 state = workspaceTabsReducer(state, { type: "select", tab: "threads" });
85 expect(state.tabs.threads).toMatchObject({
86 query: "keyboard",
87 scrollOffset: 160,
88 searchMode: "expanded",
89 });
90 state = workspaceTabsReducer(state, { type: "cancel" });
91 expect(state.tabs.threads).toMatchObject({
92 query: "",
93 scrollOffset: 640,
94 searchMode: "resting",
95 });
96 expect(state.tabs.pullRequests).toMatchObject({
97 query: "navigation",
98 scrollOffset: 240,
99 searchMode: "active",
100 });
101 });
102​
103 it("clear keeps search active without losing the position cancel restores", () => {
104 let state = initialWorkspaceTabsState();
105 state = workspaceTabsReducer(state, { type: "scroll", tab: "threads", offset: 500 });
106 state = workspaceTabsReducer(state, { type: "search" });
107 state = workspaceTabsReducer(state, { type: "query", query: "diff" });
108 state = workspaceTabsReducer(state, { type: "scroll", tab: "threads", offset: 80 });
109 state = workspaceTabsReducer(state, { type: "query", query: "" });
110 state = workspaceTabsReducer(state, { type: "search" });
111 expect(state.tabs.threads).toMatchObject({
112 query: "",
113 searchMode: "active",
114 searchEntryOffset: 500,
115 });
116 expect(workspaceTabsReducer(state, { type: "cancel" }).tabs.threads.scrollOffset).toBe(500);
117 });
118​
119 it("keeps the Settings search apart from the tab Settings opened over", () => {
120 let state = workspaceTabsReducer(initialWorkspaceTabsState(), {
121 type: "query",
122 query: "diff",
123 tab: "threads",
124 });
125 state = workspaceTabsReducer(state, { type: "search", tab: "settings" });
126 state = workspaceTabsReducer(state, { type: "query", query: "theme", tab: "settings" });
127 expect(state.tabs.settings).toMatchObject({ query: "theme", searchMode: "active" });
128 state = workspaceTabsReducer(state, { type: "cancel", tab: "settings" });
129 expect(state.tabs.settings).toMatchObject({ query: "", searchMode: "resting" });
130 expect(state.tabs.threads.query).toBe("diff");
131 });
132​
133 it("a late native blur cannot reopen a cancelled search", () => {
134 let state = workspaceTabsReducer(initialWorkspaceTabsState(), { type: "search" });
135 state = workspaceTabsReducer(state, { type: "cancel" });
136 expect(workspaceTabsReducer(state, { type: "expand" })).toBe(state);
137 });
138​
139 it("a blur from the previous native tab cannot open search in the selected tab", () => {
140 let state = workspaceTabsReducer(initialWorkspaceTabsState(), { type: "search" });
141 state = workspaceTabsReducer(state, { type: "query", query: "keyboard" });
142 state = workspaceTabsReducer(state, { type: "select", tab: "pullRequests" });
143 state = workspaceTabsReducer(state, { type: "expand", tab: "threads" });
144 expect(state.tabs.threads).toMatchObject({ query: "keyboard", searchMode: "expanded" });
145 expect(state.tabs.pullRequests.searchMode).toBe("resting");
146 expect(workspaceTabsReducer(state, { type: "select", tab: "pullRequests" })).toBe(state);
147 });
148​
149 it("keeps an empty search closed after iOS closes it with only a blur", () => {
150 const opened = workspaceTabsReducer(initialWorkspaceTabsState(), { type: "search" });
151 const closed = workspaceTabsReducer(opened, { type: "expand" });
152 const filtered = workspaceTabsReducer(closed, { type: "query", query: "skia" });
153​
154 expect(shouldRestoreNativeSearch(closed.tabs.threads, false)).toBe(false);
155 expect(shouldRestoreNativeSearch(closed.tabs.threads, true)).toBe(true);
156 expect(shouldRestoreNativeSearch(filtered.tabs.threads, false)).toBe(true);
157 expect(shouldRestoreNativeSearch(opened.tabs.threads, false)).toBe(true);
158 expect(shouldRestoreNativeSearch(initialWorkspaceTabsState().tabs.threads, true)).toBe(false);
159 });
160​
161 it("returns from Usage without losing list state", () => {
162 let state = workspaceTabsReducer(initialWorkspaceTabsState(), {
163 type: "query",
164 query: "retained",
165 tab: "threads",
166 });
167 for (const tab of ["usage"] as const) {
168 state = workspaceTabsReducer(state, { type: "select", tab });
169 expect(state.selectedTab).toBe(tab);
170 expect(workspaceTabsReducer(state, { type: "search" })).toBe(state);
171 }
172 state = workspaceTabsReducer(state, { type: "select", tab: "threads" });
173 expect(state.tabs.threads.query).toBe("retained");
174 });
175 it("opens Settings on native reselection instead of selecting its overflow section", () => {
176 const order = ["threads", "pullRequests", "usage"] as const;
177 expect(workspaceTabForNativeSlot(order, "settings")).toBe("settings");
178 expect(workspaceTabForNativeSlot(order, "first")).toBe("threads");
179 expect(workspaceTabForNativeSlot(order, "third")).toBe("usage");
180 expect(workspaceTabForNativeSlot(order, "removed")).toBeNull();
181 });
182});
183​
184const mocks = vi.hoisted(() => ({
185 split: false,
186 floating: false,
187 state: undefined as WorkspaceTabsState | undefined,
188 openWorkspaceTab: (_tab: string) => {},
189 backListeners: new Set<() => boolean>(),
190}));
191​
192vi.mock("../../settings/appearance/AppearancePreferencesProvider", () => ({
193 useAndroidFloatingTabs: () => mocks.floating,
194}));
195vi.mock("react-native", () => ({
196 BackHandler: {
197 addEventListener: (_event: string, listener: () => boolean) => {
198 mocks.backListeners.add(listener);
199 return { remove: () => mocks.backListeners.delete(listener) };
200 },
201 },
202 Keyboard: { dismiss: () => undefined },
203 useWindowDimensions: () => ({ width: 400, height: 800 }),
204 View: ({ children }: { children: ReactNode }) => createElement("div", null, children),
205}));
206vi.mock("react-native-safe-area-context", () => ({
207 SafeAreaInsetsContext: createContext({ top: 0, right: 0, bottom: 24, left: 0 }),
208 useSafeAreaInsets: () => ({ top: 0, right: 0, bottom: 24, left: 0 }),
209}));
210vi.mock("../AdaptiveWorkspaceLayout", () => ({
211 useAdaptiveWorkspaceLayout: () => ({
212 layout: { usesSplitView: mocks.split },
213 startNewTask: () => undefined,
214 }),
215}));
216vi.mock("@react-navigation/native", () => ({ useIsFocused: () => true }));
217vi.mock("../adaptive-workspace-context", () => ({
218 useAdaptiveWorkspaceLayout: () => ({ openWorkspaceTab: mocks.openWorkspaceTab }),
219}));
220vi.mock("./navigation-tabs", () => ({
221 useEnabledNavigationTabs: () => ["threads", "pullRequests", "usage"],
222}));
223vi.mock("./WorkspaceTabsContext", () => ({
224 WorkspaceTabsContext: createContext(null),
225 useWorkspaceTabs: () => ({ state: mocks.state, dispatch: () => undefined }),
226}));
227vi.mock("./WorkspaceExtraTab", () => ({
228 WorkspaceExtraTab: function UsageProbe() {
229 const [tab, setTab] = useState("Limits");
230 return createElement("button", { onClick: () => setTab("Usage") }, tab);
231 },
232}));
233vi.mock("../../../native/StackHeader", () => ({ NativeStackScreenOptions: () => null }));
234vi.mock("../../../components/ui/AppBar", () => ({ AppBar: () => null }));
235vi.mock("../WorkspaceEmptyDetail", () => ({ WorkspaceEmptyDetail: () => null }));
236vi.mock("../../../components/ui/ScreenContent", () => ({
237 ScreenContent: ({ children }: { children: ReactNode }) => createElement("div", null, children),
238}));
239vi.mock("../../../components/ui/useAndroidControlSizing", () => ({
240 useAndroidControlSizing: () => ({ fabClearance: 64, scale: 1, fabSize: 56 }),
241}));
242vi.mock("../../home/HomeRouteScreen", () => ({ HomeRouteScreen: () => null }));
243vi.mock("../../pull-requests/PullRequestFilterMenuContext", () => ({
244 PullRequestFilterMenuProvider: ({ children }: { children: ReactNode }) =>
245 createElement("div", null, children),
246}));
247vi.mock("../../pull-requests/PullRequestsScreen", () => ({ PullRequestsScreen: () => null }));
248vi.mock("./PullRequestToolbar.android", () => ({ PullRequestToolbar: () => null }));
249vi.mock("./AndroidWorkspaceNavigation", () => ({
250 AndroidWorkspaceNavigation: () => null,
251 FloatingWorkspaceTabs: () => null,
252}));
253vi.mock("./WorkspaceNoTabs", () => ({ WorkspaceNoTabs: () => null }));
254vi.mock("../../../components/ui/Pane", () => ({ usePaneRole: () => "screen" }));
255vi.mock("./AndroidWorkspaceSidebar.android", () => ({ AndroidWorkspaceSidebar: () => null }));
256vi.mock("../../../components/ui/WorkspaceAppBar.android", () => ({
257 WorkspaceAppBarScope: ({ children }: { children: ReactNode }) => children,
258}));
259​
260function setup(split: boolean, floating: boolean, state = selected("usage")) {
261 mocks.split = split;
262 mocks.floating = floating;
263 mocks.state = state;
264 mocks.openWorkspaceTab = () => undefined;
265 mocks.backListeners.clear();
266 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
267 return { [Symbol.dispose]: () => vi.unstubAllGlobals() };
269​
270function selected(tab: WorkspaceTab) {
271 return workspaceTabsReducer(initialWorkspaceTabsState(), { type: "select", tab });
273​
274/** Android calls the newest listener first and stops at the first that handles Back. */
275function pressBack() {
276 return [...mocks.backListeners].reverse().some((listener) => listener());
278​
279it("Back from the Pulls tab returns to Threads instead of leaving the app", async () => {
280 using _actEnvironment = setup(false, false, selected("pullRequests"));
281 const opened: string[] = [];
282 mocks.openWorkspaceTab = (tab) => opened.push(tab);
283 const root = createRoot(document.createElement("div"));
284 using _root = { [Symbol.dispose]: () => act(() => root.unmount()) };
285 await act(async () => root.render(createElement(WorkspaceRootScreen)));
286​
287 expect(pressBack()).toBe(true);
288 expect(opened).toEqual(["threads"]);
289});
290​
291it.each([
292 [false, false],
293 [true, false],
294 [false, true],
295 [true, true],
296])(
297 "keeps the Usage page mounted through layout transitions (split: %s, floating: %s)",
298 async (split, floating) => {
299 using _actEnvironment = setup(split, floating);
300 const container = document.createElement("div");
301 const root = createRoot(container);
302 using _root = { [Symbol.dispose]: () => act(() => root.unmount()) };
303 await act(async () => root.render(createElement(WorkspaceRootScreen)));
304​
305 const button = container.querySelector("button")!;
306 await act(async () => button.click());
307 expect(button.textContent).toBe("Usage");
308​
309 for (const nextSplit of [!split, split, !split, split]) {
310 mocks.split = nextSplit;
311 await act(async () => root.render(createElement(WorkspaceRootScreen)));
312 expect(container.querySelector("button")?.textContent).toBe("Usage");
313 expect(container.querySelector("button")).toBe(button);
314 }
315 },
316);