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

GitBranchesSheet.occupancy.test.tsx

105 lines4.0 KB

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