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}));
18
19vi.mock("@react-navigation/native", () => ({ useNavigation: () => ({ goBack() {} }) }));
20vi.mock("react-native", () => ({
21 Platform: { OS: "android" },
22 useWindowDimensions: () => ({ height: 800 }),
23 View: ({ children }: { children: ReactNode }) => children,
24 ScrollView: ({ children }: { children: ReactNode }) => children,
25 Pressable: () => null,
26}));
27vi.mock("react-native-safe-area-context", () => ({
28 useSafeAreaInsets: () => ({ bottom: 0 }),
29}));
30vi.mock("../../../components/ui/AndroidScreenHeader", () => ({ AndroidSheetHeader: () => null }));
31vi.mock("../../../components/ui/SheetHeader", () => ({ SheetHeader: () => null }));
32vi.mock("../../../components/ui/ScreenContent", () => ({
33 ScreenContent: ({ children }: { children: ReactNode }) => children,
34}));
35vi.mock("../../../native/StackHeader", () => ({ NativeStackScreenOptions: () => null }));
36vi.mock("../../../components/ui/AppText", () => ({
37 AppText: ({ children }: { children: ReactNode }) => children,
38 AppTextInput: (props: InputProps) => {
39 state.inputs.set(props.accessibilityLabel, props);
40 return null;
41 },
42}));
43vi.mock("../../../state/query", () => ({
44 useEnvironmentQuery: () => ({
45 data: state.refName === null ? undefined : { refName: state.refName },
46 }),
47}));
48vi.mock("../../../state/use-thread-selection", () => ({
49 useThreadSelection: () => ({
50 selectedThread: { environmentId: "environment", branch: state.branch },
51 }),
52}));
53vi.mock("../../../state/use-selected-thread-worktree", () => ({
54 useSelectedThreadWorktree: () => ({
55 selectedThreadCwd: "/repo",
56 selectedThreadWorktreePath: null,
57 }),
58}));
59vi.mock("../../../state/use-selected-thread-git-state", () => ({
60 useSelectedThreadGitState: () => ({
61 selectedThreadBranches: [],
62 selectedThreadBranchesLoading: false,
63 gitOperationLabel: null,
64 }),
65}));
66vi.mock("../../../state/use-selected-thread-git-actions", () => ({
67 useSelectedThreadGitActions: () => ({
68 canChangeThreadBranch: true,
69 onCreateSelectedThreadWorktree: async (input: { baseBranch: string; newBranch: string }) => {
70 state.worktrees.push(input);
71 return null;
72 },
73 }),
74}));
75vi.mock("../../../state/vcs", () => ({ vcsEnvironment: { status: () => null } }));
76vi.mock("./gitSheetComponents", () => ({
77 SheetActionButton: (props: ActionProps) => {
78 state.actions.set(props.label, props);
79 return null;
80 },
81}));
82
83import { GitBranchesSheet } from "./GitBranchesSheet";
84
85function setup(branch: string | null = null) {
86 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
87 state.refName = null;
88 state.branch = branch;
89 state.inputs.clear();
90 state.actions.clear();
91 state.worktrees = [];
92 const root = createRoot(document.createElement("div"));
93 const render = () =>
94 act(() => {
95 root.render(
96 <GitBranchesSheet
97 route={{ params: { environmentId: "environment", threadId: "thread" } }}
98 />,
99 );
100 });
101 render();
102 return {
103 render,
104 edit(label: string, value: string) {
105 act(() => state.inputs.get(label)!.onChangeText(value));
106 },
107 create() {
108 act(() => state.actions.get("Create worktree")!.onPress());
109 },
110 [Symbol.dispose]() {
111 act(() => root.unmount());
112 },
113 };
114}
115
116it.each([null, "stale-branch"])(
117 "uses the loaded checkout branch when thread metadata is %s",
118 (branch) => {
119 using screen = setup(branch);
120 screen.edit("Worktree branch name", "feature/new-worktree");
121 state.refName = "release/stable";
122 screen.render();
123 screen.create();
124 expect(state.worktrees).toEqual([
125 { baseBranch: "release/stable", newBranch: "feature/new-worktree" },
126 ]);
127 },
128);
129
130it.each(["custom-base", ""])("preserves an edited base %j when status arrives", (base) => {
131 using screen = setup();
132 screen.edit("Worktree base branch", base);
133 screen.edit("Worktree branch name", "feature/new-worktree");
134 state.refName = "release/stable";
135 screen.render();
136 expect(state.inputs.get("Worktree base branch")!.value).toBe(base);
137 expect(state.actions.get("Create worktree")!.disabled).toBe(base === "");
138 screen.create();
139 expect(state.worktrees).toEqual(
140 base === "" ? [] : [{ baseBranch: base, newBranch: "feature/new-worktree" }],
141 );
142});