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