import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { describe, it, expect, vi, beforeEach } from "vitest"; import { GitControlBarRepoButton } from "#/components/features/chat/git-control-bar-repo-button"; // Mock react-i18next vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (key: string) => key, }), })); // Mock GitProviderIcon vi.mock("#/components/shared/git-provider-icon", () => ({ GitProviderIcon: ({ gitProvider }: { gitProvider: string }) => ( {gitProvider} ), })); // Mock GitExternalLinkIcon vi.mock( "#/components/features/chat/git-external-link-icon", () => ({ GitExternalLinkIcon: () => ( external ), }), ); // Mock RepoForkedIcon vi.mock("#/icons/repo-forked.svg?react", () => ({ default: () => forked, })); vi.mock("#/hooks/use-provider-host", () => ({ useProviderHost: () => null, })); // Mock constructRepositoryUrl vi.mock("#/utils/utils", async (importOriginal) => { const actual = await importOriginal(); return { ...actual, constructRepositoryUrl: (provider: string, repo: string) => `https://${provider}.com/${repo}`, }; }); describe("GitControlBarRepoButton", () => { beforeEach(() => { vi.clearAllMocks(); }); describe("when repository is connected", () => { it("should render as a link with repository name", () => { render( , ); const link = screen.getByRole("link"); expect(link).toHaveAttribute("href", "https://github.com/owner/repo"); expect(link).toHaveAttribute("target", "_blank"); expect(screen.getByText("owner/repo")).toBeInTheDocument(); }); it("should show git provider icon and external link icon", () => { render( , ); expect(screen.getByTestId("git-provider-icon")).toBeInTheDocument(); expect(screen.getByTestId("git-external-link-icon")).toBeInTheDocument(); }); it("should not show repo forked icon", () => { render( , ); expect( screen.queryByTestId("repo-forked-icon"), ).not.toBeInTheDocument(); }); }); describe("when no repository is connected", () => { it("should render as a button with 'No Repo Connected' text", () => { render( , ); const button = screen.getByRole("button"); expect(button).toBeInTheDocument(); expect( screen.getByText("COMMON$NO_REPO_CONNECTED"), ).toBeInTheDocument(); }); it("should show repo forked icon instead of provider icon", () => { render( , ); expect(screen.getByTestId("repo-forked-icon")).toBeInTheDocument(); expect( screen.queryByTestId("git-provider-icon"), ).not.toBeInTheDocument(); }); it("should not show external link icon", () => { render( , ); expect( screen.queryByTestId("git-external-link-icon"), ).not.toBeInTheDocument(); }); it("should call onClick when clicked", async () => { const handleClick = vi.fn(); const user = userEvent.setup(); render( , ); await user.click(screen.getByRole("button")); expect(handleClick).toHaveBeenCalledTimes(1); }); it("should be disabled when disabled prop is true", () => { render( , ); const button = screen.getByRole("button"); expect(button).toBeDisabled(); expect(button).toHaveClass("cursor-not-allowed"); }); it("should be clickable when disabled prop is false", () => { render( , ); const button = screen.getByRole("button"); expect(button).not.toBeDisabled(); expect(button).toHaveClass("cursor-pointer"); }); it("should not call onClick when disabled", async () => { const handleClick = vi.fn(); const user = userEvent.setup(); render( , ); await user.click(screen.getByRole("button")); expect(handleClick).not.toHaveBeenCalled(); }); }); });