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