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
5const checkout = vi.hoisted(() => ({
6 projectRoot: "",
7 worktreePath: null as string | null,
8 branches: [] as Array<{ name: string; worktreePath: string | null }>,
9}));
10
11vi.mock("@react-navigation/native", () => ({ useNavigation: () => ({}) }));
12vi.mock("../../../components/ui/SheetHeader", () => ({ SheetHeader: () => null }));
13vi.mock("../../../components/ui/FitSheet", () => ({
14 FitSheet: ({ children }: { children: ReactNode }) => <div>{children}</div>,
15}));
16vi.mock("../../../native/StackHeader", () => ({ NativeStackScreenOptions: () => null }));
17vi.mock("../../../components/ui/FormSection", () => ({
18 FormSection: () => null,
19 FormField: () => null,
20}));
21vi.mock("../../../components/ui/ListSection", () => ({
22 ListSection: ({ children }: { children: ReactNode }) => <div>{children}</div>,
23}));
24vi.mock("../../../components/ui/ListChoiceRow", () => ({
25 ListChoiceRow: (props: { label: string; description: string; disabled: boolean }) => (
26 <button disabled={props.disabled}>
27 <span>{props.label}</span>
28 <span>{props.description}</span>
29 </button>
30 ),
31}));
32vi.mock("./gitSheetComponents", () => ({ SheetActionButton: () => null }));
33vi.mock("../../../state/entities", () => ({}));
34vi.mock("../../../state/threads", () => ({}));
35vi.mock("../../../state/use-thread-selection", () => ({
36 useThreadSelection: () => ({
37 selectedThread: { environmentId: "environment-1", worktreePath: checkout.worktreePath },
38 selectedThreadProject: { workspaceRoot: checkout.projectRoot },
39 }),
40}));
41vi.mock("../../../state/use-thread-detail", () => ({
42 useSelectedThreadWorktreePath: () => checkout.worktreePath,
43}));
44vi.mock("../../../state/use-selected-thread-git-state", () => ({
45 useSelectedThreadGitState: () => ({
46 selectedThreadBranches: checkout.branches,
47 selectedThreadBranchesLoading: false,
48 gitOperationLabel: null,
49 }),
50}));
51vi.mock("../../../state/use-selected-thread-git-actions", () => ({
52 useSelectedThreadGitActions: () => ({ canChangeThreadBranch: true }),
53}));
54vi.mock("../../../state/query", () => ({ useEnvironmentQuery: () => ({ data: null }) }));
55vi.mock("../../../state/vcs", () => ({ vcsEnvironment: { branch: () => null } }));
56
57import { GitBranchesSheet } from "./GitBranchesSheet";
58
59it("recognizes both the main checkout and a linked worktree as this thread's checkout", () => {
60 const directory = "/repo";
61 const linked = "/repo-linked";
62 checkout.projectRoot = directory;
63 checkout.branches = [
64 { name: "feature/available", worktreePath: null },
65 { name: "feature/linked", worktreePath: linked },
66 { name: "main", worktreePath: directory },
67 ];
68 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
69 const container = document.createElement("div");
70 const root = createRoot(container);
71 const rows = () => {
72 act(() => {
73 root.render(
74 <GitBranchesSheet
75 route={{ params: { environmentId: "environment-1", threadId: "thread-1" } }}
76 />,
77 );
78 });
79 return [...container.querySelectorAll("button")].map((button) => ({
80 name: button.querySelectorAll("span")[0]?.textContent,
81 subtitle: button.querySelectorAll("span")[1]?.textContent,
82 disabled: button.disabled,
83 }));
84 };
85 try {
86 checkout.worktreePath = null;
87 expect(rows()).toEqual([
88 { name: "feature/available", subtitle: "Local branch", disabled: false },
89 { name: "feature/linked", subtitle: "Checked out in another worktree", disabled: true },
90 { name: "main", subtitle: "Checked out in this thread", disabled: false },
91 ]);
92
93 checkout.worktreePath = linked;
94 expect(rows()).toEqual([
95 { name: "feature/available", subtitle: "Local branch", disabled: false },
96 { name: "feature/linked", subtitle: "Checked out in this thread", disabled: false },
97 { name: "main", subtitle: "Checked out in another worktree", disabled: true },
98 ]);
99 } finally {
100 act(() => root.unmount());
101 }
102});