71 lines
2.1 KiB
TypeScript
71 lines
2.1 KiB
TypeScript
import { cleanup, render, screen } from "@testing-library/react";
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { ToastProvider } from "@/components/toast-provider";
|
|
import { GitHubCLIForm } from "./github-cli-form";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
fetchAccounts: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("@/lib/api/domains/github-api", () => ({
|
|
fetchGitHubCLIAccounts: mocks.fetchAccounts,
|
|
}));
|
|
|
|
beforeEach(() => {
|
|
mocks.fetchAccounts.mockReset();
|
|
});
|
|
|
|
afterEach(() => cleanup());
|
|
|
|
describe("GitHubCLIForm", () => {
|
|
it("shows account discovery failures instead of an empty-account message", async () => {
|
|
mocks.fetchAccounts.mockRejectedValue(new Error("Request failed (500): github_internal_error"));
|
|
|
|
render(
|
|
<ToastProvider>
|
|
<GitHubCLIForm workspaceId="workspace-1" onAccountChange={vi.fn()} />
|
|
</ToastProvider>,
|
|
);
|
|
|
|
expect((await screen.findByRole("alert")).textContent).toBe(
|
|
"Could not load GitHub CLI accounts. Request failed (500): github_internal_error",
|
|
);
|
|
expect(screen.queryByText(/Sign in with/)).toBeNull();
|
|
});
|
|
|
|
it("keeps sign-in guidance for a successful empty account list", async () => {
|
|
mocks.fetchAccounts.mockResolvedValue([]);
|
|
|
|
render(
|
|
<ToastProvider>
|
|
<GitHubCLIForm workspaceId="workspace-1" onAccountChange={vi.fn()} />
|
|
</ToastProvider>,
|
|
);
|
|
|
|
expect(await screen.findByText(/Sign in with/)).toBeTruthy();
|
|
expect(screen.queryByRole("alert")).toBeNull();
|
|
});
|
|
|
|
it("reports the preferred account without requiring a second action", async () => {
|
|
const account = {
|
|
host: "github.com",
|
|
login: "octocat",
|
|
active: true,
|
|
selected: true,
|
|
state: "active",
|
|
};
|
|
const onAccountChange = vi.fn();
|
|
mocks.fetchAccounts.mockResolvedValue([account]);
|
|
|
|
render(
|
|
<ToastProvider>
|
|
<GitHubCLIForm workspaceId="workspace-1" onAccountChange={onAccountChange} />
|
|
</ToastProvider>,
|
|
);
|
|
|
|
expect(await screen.findByText(/octocat/)).toBeTruthy();
|
|
expect(onAccountChange).toHaveBeenLastCalledWith(account);
|
|
expect(screen.queryByRole("button", { name: "Use account" })).toBeNull();
|
|
});
|
|
});
|