|
1 | 1 | import { Theme } from "@radix-ui/themes"; |
2 | | -import { render, screen } from "@testing-library/react"; |
| 2 | +import { act, render, screen } from "@testing-library/react"; |
3 | 3 | import userEvent from "@testing-library/user-event"; |
| 4 | +import { useState } from "react"; |
4 | 5 | import { describe, expect, it, vi } from "vitest"; |
5 | 6 |
|
6 | 7 | vi.mock("../state/gitInteractionStore", () => ({ |
@@ -42,9 +43,36 @@ vi.mock("../../../primitives/toast", () => ({ |
42 | 43 | })); |
43 | 44 |
|
44 | 45 | const mutateMock = vi.fn(); |
| 46 | +let checkoutMutationOptions: { |
| 47 | + onSuccess?: (result: { |
| 48 | + previousBranch: string; |
| 49 | + currentBranch: string; |
| 50 | + }) => void; |
| 51 | +}; |
| 52 | +let completeCheckout: (result: { |
| 53 | + previousBranch: string; |
| 54 | + currentBranch: string; |
| 55 | +}) => void; |
45 | 56 | vi.mock("@tanstack/react-query", () => ({ |
46 | 57 | useQuery: () => ({ data: [], isLoading: false }), |
47 | | - useMutation: () => ({ mutate: mutateMock }), |
| 58 | + useMutation: (options: typeof checkoutMutationOptions) => { |
| 59 | + checkoutMutationOptions = options; |
| 60 | + const [result, setResult] = useState<{ |
| 61 | + data?: { previousBranch: string; currentBranch: string }; |
| 62 | + variables?: { directoryPath: string; branchName: string }; |
| 63 | + }>({}); |
| 64 | + completeCheckout = (data) => { |
| 65 | + setResult({ |
| 66 | + data, |
| 67 | + variables: { |
| 68 | + directoryPath: "/repos/code", |
| 69 | + branchName: data.currentBranch, |
| 70 | + }, |
| 71 | + }); |
| 72 | + options.onSuccess?.(data); |
| 73 | + }; |
| 74 | + return { mutate: mutateMock, ...result }; |
| 75 | + }, |
48 | 76 | useQueryClient: () => ({ |
49 | 77 | getQueriesData: () => [], |
50 | 78 | getQueryData: () => undefined, |
@@ -297,6 +325,45 @@ describe("BranchSelector cloud mode", () => { |
297 | 325 | }); |
298 | 326 |
|
299 | 327 | describe("BranchSelector checkout context", () => { |
| 328 | + it("shows the checked-out branch after an in-place checkout succeeds", () => { |
| 329 | + const { rerender } = renderInTheme( |
| 330 | + <BranchSelector |
| 331 | + repoPath="/repos/code" |
| 332 | + currentBranch="main" |
| 333 | + workspaceMode="local" |
| 334 | + />, |
| 335 | + ); |
| 336 | + |
| 337 | + expect(screen.getByRole("combobox", { name: "Branch" })).toHaveTextContent( |
| 338 | + "main", |
| 339 | + ); |
| 340 | + |
| 341 | + act(() => { |
| 342 | + completeCheckout({ |
| 343 | + previousBranch: "main", |
| 344 | + currentBranch: "feature/in-place", |
| 345 | + }); |
| 346 | + }); |
| 347 | + |
| 348 | + expect(screen.getByRole("combobox", { name: "Branch" })).toHaveTextContent( |
| 349 | + "feature/in-place", |
| 350 | + ); |
| 351 | + |
| 352 | + rerender( |
| 353 | + <Theme> |
| 354 | + <BranchSelector |
| 355 | + repoPath="/repos/code" |
| 356 | + currentBranch="feature/external" |
| 357 | + workspaceMode="local" |
| 358 | + /> |
| 359 | + </Theme>, |
| 360 | + ); |
| 361 | + |
| 362 | + expect(screen.getByRole("combobox", { name: "Branch" })).toHaveTextContent( |
| 363 | + "feature/external", |
| 364 | + ); |
| 365 | + }); |
| 366 | + |
300 | 367 | it.each([ |
301 | 368 | { |
302 | 369 | name: "local mode shows which checkout the branch switch applies to", |
|
0 commit comments