Open History to see the last commit
1import { act, type ReactNode } from "react";
2import { createRoot } from "react-dom/client";
3import { expect, it, vi } from "vite-plus/test";
4
5type InputProps = {
6 accessibilityLabel: string;
7 value: string;
8 onChangeText: (value: string) => void;
9};
10type ActionProps = { label: string; disabled: boolean; onPress: () => void };
11const state = vi.hoisted(() => ({
12 refName: null as string | null,
13 branch: null as string | null,
14 inputs: new Map<string, InputProps>(),
15 actions: new Map<string, ActionProps>(),
16 worktrees: [] as Array<{ baseBranch: string; newBranch: string }>,
17 focused: true,
18 goBackCalls: 0,
19 gitResult: Promise.resolve<object | null>(null),
20}));
21
22vi.mock("@react-navigation/native", () => ({
23 useFocusEffect: () => {},
24 useNavigation: () => ({
25 goBack: () => state.goBackCalls++,
26 isFocused: () => state.focused,
27 }),
28}));
29vi.mock("../../../components/ui/SheetHeader", () => ({ SheetHeader: () => null }));
30vi.mock("../../../components/ui/FitSheet", () => ({
31 FitSheet: ({ children }: { children: ReactNode }) => children,
32}));
33vi.mock("../../../native/StackHeader", () => ({ NativeStackScreenOptions: () => null }));
34vi.mock("../../../components/ui/FormSection", () => ({
35 FormSection: ({ children }: { children: ReactNode }) => children,
36 FormField: (props: InputProps) => {
37 state.inputs.set(props.accessibilityLabel, props);
38 return null;
39 },
40}));
41vi.mock("../../../components/ui/ListSection", () => ({
42 ListSection: ({ children }: { children: ReactNode }) => children,
43}));
44vi.mock("../../../components/ui/ListChoiceRow", () => ({
45 ListChoiceRow: (props: ActionProps) => {
46 state.actions.set(props.label, props);
47 return null;
48 },
49}));
50vi.mock("../../../state/query", () => ({
51 useEnvironmentQuery: () => ({
52 data: state.refName === null ? undefined : { refName: state.refName },
53 }),
54}));
55vi.mock("../../../state/use-thread-selection", () => ({
56 useThreadSelection: () => ({
57 selectedThread: { environmentId: "environment", branch: state.branch },
58 }),
59}));
60vi.mock("../../../state/use-selected-thread-worktree", () => ({
61 useSelectedThreadWorktree: () => ({
62 selectedThreadCwd: "/repo",
63 selectedThreadWorktreePath: null,
64 }),
65}));
66vi.mock("../../../state/use-selected-thread-git-state", () => ({
67 useSelectedThreadGitState: () => ({
68 selectedThreadBranches: [{ name: "release/stable", worktreePath: null }],
69 selectedThreadBranchesLoading: false,
70 gitOperationLabel: null,
71 }),
72}));
73vi.mock("../../../state/use-selected-thread-git-actions", () => ({
74 useSelectedThreadGitActions: () => ({
75 canChangeThreadBranch: true,
76 onCreateSelectedThreadBranch: () => state.gitResult,
77 onCheckoutSelectedThreadBranch: () => state.gitResult,
78 onCreateSelectedThreadWorktree: (input: { baseBranch: string; newBranch: string }) => {
79 state.worktrees.push(input);
80 return state.gitResult;
81 },
82 }),
83}));
84vi.mock("../../../state/vcs", () => ({ vcsEnvironment: { branch: () => null } }));
85vi.mock("./gitSheetComponents", () => ({
86 SheetActionButton: (props: ActionProps) => {
87 state.actions.set(props.label, props);
88 return null;
89 },
90}));
91
92import { GitBranchesSheet } from "./GitBranchesSheet";
93
94function setup(branch: string | null = null) {
95 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
96 state.refName = null;
97 state.branch = branch;
98 state.inputs.clear();
99 state.actions.clear();
100 state.worktrees = [];
101 state.gitResult = Promise.resolve(null);
102 state.focused = true;
103 state.goBackCalls = 0;
104 const root = createRoot(document.createElement("div"));
105 const render = () =>
106 act(() => {
107 root.render(
108 <GitBranchesSheet
109 route={{ params: { environmentId: "environment", threadId: "thread" } }}
110 />,
111 );
112 });
113 render();
114 return {
115 render,
116 press(label: string) {
117 act(() => state.actions.get(label)!.onPress());
118 },
119 edit(label: string, value: string) {
120 act(() => state.inputs.get(label)!.onChangeText(value));
121 },
122 [Symbol.dispose]() {
123 act(() => root.unmount());
124 },
125 };
126}
127
128it.each([null, "stale-branch"])(
129 "uses the loaded checkout branch when thread metadata is %s",
130 (branch) => {
131 using screen = setup(branch);
132 screen.edit("Worktree branch name", "feature/new-worktree");
133 state.refName = "release/stable";
134 screen.render();
135 screen.press("Create worktree");
136 expect(state.worktrees).toEqual([
137 { baseBranch: "release/stable", newBranch: "feature/new-worktree" },
138 ]);
139 },
140);
141
142it.each(["custom-base", ""])("preserves an edited base %j when status arrives", (base) => {
143 using screen = setup();
144 screen.edit("Worktree base branch", base);
145 screen.edit("Worktree branch name", "feature/new-worktree");
146 state.refName = "release/stable";
147 screen.render();
148 expect(state.inputs.get("Worktree base branch")!.value).toBe(base);
149 expect(state.actions.get("Create worktree")!.disabled).toBe(base === "");
150 screen.press("Create worktree");
151 expect(state.worktrees).toEqual(
152 base === "" ? [] : [{ baseBranch: base, newBranch: "feature/new-worktree" }],
153 );
154});
155
156it.each(["Create worktree", "Create & checkout", "release/stable"])(
157 "does not navigate back when %s finishes after the sheet lost focus",
158 async (action) => {
159 using screen = setup();
160 const gitResult = Promise.withResolvers<object>();
161 state.gitResult = gitResult.promise;
162 screen.edit("New branch name", "feature/new-branch");
163 screen.edit("Worktree branch name", "feature/new-worktree");
164 screen.press(action);
165 state.focused = false;
166
167 await act(async () => {
168 gitResult.resolve({ refName: "feature/ready" });
169 await gitResult.promise;
170 });
171 expect(state.goBackCalls).toBe(0);
172 },
173);