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

GitBranchesSheet.test.tsx

133 lines4.4 KB

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