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