Files
kandev/apps/web/components/integrations/change-request-detail.test.tsx
T

263 lines
8.5 KiB
TypeScript

import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
import { TooltipProvider } from "@kandev/ui/tooltip";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
ChangeRequestDetail,
type ChangeRequestDetailModel,
type ChangeRequestDetailProps,
} from "./change-request-detail";
const detail: ChangeRequestDetailModel = {
providerId: "bitbucket",
reviewKey: "workspace/repo#42",
number: 42,
title: "Fix authentication",
url: "https://bitbucket.org/workspace/repo/pull-requests/42",
state: "open",
author: {
name: "Ada Lovelace",
url: "https://bitbucket.org/ada",
},
createdAt: "2026-08-06T12:00:00Z",
sourceBranch: "feature/auth",
targetBranch: "main",
additions: 12,
deletions: 3,
description: "Use the new token exchange.",
reviewState: "approved",
pendingReviewCount: 1,
reviews: [
{
id: "review-1",
author: { name: "Grace", url: "https://bitbucket.org/grace" },
state: "APPROVED",
body: "Looks good.",
createdAt: "2026-08-06T12:30:00Z",
},
],
requestedReviewers: [{ name: "Linus" }],
checks: [
{
id: "check-1",
name: "CI",
state: "completed",
conclusion: "success",
url: "https://ci.example.test/1",
},
],
comments: [
{
id: "comment-1",
author: { name: "Grace", url: "https://bitbucket.org/grace" },
body: "Please keep this branch.",
createdAt: "2026-08-06T12:45:00Z",
path: "auth.go",
line: 42,
},
{
id: "comment-2",
parentId: "comment-1",
author: { name: "Ada" },
body: "Done.",
createdAt: "2026-08-06T13:00:00Z",
},
],
lastSyncedAt: "2026-08-06T13:01:00Z",
};
function renderDetail(props: Partial<ChangeRequestDetailProps> = {}) {
return render(
<TooltipProvider>
<ChangeRequestDetail detail={detail} {...props} />
</TooltipProvider>,
);
}
afterEach(() => {
cleanup();
vi.useRealTimers();
});
describe("ChangeRequestDetail", () => {
it("renders the GitHub detail anatomy from provider-neutral data", () => {
renderDetail();
expect(screen.getByRole("link", { name: "Fix authentication" }).getAttribute("href")).toBe(
detail.url,
);
expect(screen.getByRole("link", { name: "Ada Lovelace" }).getAttribute("href")).toBe(
detail.author.url,
);
expect(screen.getByText("#42")).toBeTruthy();
expect(screen.getByText("feature/auth")).toBeTruthy();
expect(screen.getByText("main")).toBeTruthy();
expect(screen.getByText(/Reviews/)).toBeTruthy();
expect(screen.getByText(/Checks/)).toBeTruthy();
expect(screen.getByText(/Comments/)).toBeTruthy();
fireEvent.click(screen.getByRole("button", { name: /Description/ }));
expect(screen.getByText("Use the new token exchange.")).toBeTruthy();
expect(screen.getByRole("link", { name: "CI" }).getAttribute("href")).toBe(
detail.checks[0].url,
);
expect(screen.getByText("auth.go:42")).toBeTruthy();
expect(screen.getByText("Done.")).toBeTruthy();
expect(screen.queryByRole("tab")).toBeNull();
});
});
describe("ChangeRequestDetail interactions", () => {
it("preserves GitHub section summaries and bulk/thread context actions", () => {
const onAddContext = vi.fn();
renderDetail({
detail: {
...detail,
requestedReviewers: [{ name: "platform", kind: "team" }],
checks: [
{
...detail.checks[0],
conclusion: "failure",
output: "tests failed",
},
],
},
onAddContext,
});
expect(screen.getByText("Reviews: 1 approved, 1 pending")).toBeTruthy();
expect(screen.getByText("CI Checks: 1 failed")).toBeTruthy();
expect(screen.getByText("platform (team)")).toBeTruthy();
const addAll = screen.getAllByRole("button", { name: "Add all" });
expect(addAll).toHaveLength(3);
addAll.forEach((button) => fireEvent.click(button));
expect(onAddContext).toHaveBeenCalledWith("review", expect.stringContaining("All PR Reviews"));
expect(onAddContext).toHaveBeenCalledWith(
"review",
expect.stringContaining("**Grace**: Approved"),
);
expect(onAddContext).toHaveBeenCalledWith(
"check",
expect.stringContaining("1 CI Check Failed"),
);
expect(onAddContext).toHaveBeenCalledWith("check", expect.stringContaining("**CI**: failure"));
expect(onAddContext).toHaveBeenCalledWith(
"comment",
expect.stringContaining("All PR Comments"),
);
const threadContext = screen.getByTestId("change-request-thread-context-comment-1");
fireEvent.click(threadContext.querySelector("button")!);
expect(onAddContext).toHaveBeenCalledWith("comment", expect.stringContaining("Comment Thread"));
});
it("shows provider aggregate pending review count when identities are unavailable", () => {
renderDetail({
detail: {
...detail,
reviews: [],
requestedReviewers: [],
pendingReviewCount: 2,
},
});
expect(screen.getByText("Reviews: 2 pending")).toBeTruthy();
});
});
describe("ChangeRequestDetail actions and states", () => {
it("dispatches only advertised provider actions with mobile-safe targets", () => {
const onAction = vi.fn();
renderDetail({
presentation: "mobile",
actions: [
{ id: "approve", label: "Approve", placement: "header", tone: "success" },
{ id: "decline", label: "Decline", placement: "header", tone: "danger" },
{ id: "comment", label: "Comment", placement: "comment", input: "text" },
{ id: "reply", label: "Reply", placement: "thread", input: "text" },
],
onAction,
});
const approve = screen.getByRole("button", { name: "Approve" });
expect(approve.classList.contains("min-h-11")).toBe(true);
fireEvent.click(approve);
expect(onAction).toHaveBeenCalledWith({ actionId: "approve" });
fireEvent.change(screen.getByLabelText("Add review comment"), {
target: { value: "Ship it." },
});
fireEvent.click(screen.getByRole("button", { name: "Comment" }));
expect(onAction).toHaveBeenCalledWith({ actionId: "comment", body: "Ship it." });
fireEvent.change(screen.getByRole("textbox", { name: "Reply to thread comment-1" }), {
target: { value: "Resolved." },
});
fireEvent.click(screen.getByRole("button", { name: "Reply to thread comment-1" }));
expect(onAction).toHaveBeenCalledWith({
actionId: "reply",
body: "Resolved.",
threadId: "comment-1",
});
});
it("renders one context action for a failed check", () => {
renderDetail({
detail: {
...detail,
reviews: [],
comments: [],
checks: [{ ...detail.checks[0], conclusion: "failure" }],
},
onAddContext: vi.fn(),
});
const checkRow = screen.getByRole("link", { name: "CI" }).parentElement;
expect(checkRow?.querySelectorAll("button")).toHaveLength(1);
});
it("updates a running check duration", () => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-08-06T14:00:00Z"));
renderDetail({
detail: {
...detail,
checks: [
{
...detail.checks[0],
state: "in_progress",
conclusion: "",
startedAt: "2026-08-06T13:58:00Z",
},
],
},
});
const duration = screen.getByTestId("check-duration-CI");
const initial = duration.textContent;
act(() => vi.advanceTimersByTime(1_000));
expect(duration.textContent).not.toBe(initial);
expect(duration.textContent).toContain("running");
});
it("owns one vertical scroller and keeps loading and error states inside it", () => {
const { rerender } = renderDetail({ detail: null, loading: true });
expect(screen.getByTestId("change-request-detail").classList.contains("h-full")).toBe(true);
expect(screen.getByTestId("change-request-detail").classList.contains("min-h-0")).toBe(true);
expect(screen.getAllByTestId("change-request-detail-scroll")).toHaveLength(1);
expect(
screen.getByTestId("change-request-detail-content").classList.contains("overflow-x-hidden"),
).toBe(true);
expect(screen.getByLabelText("Loading change request")).toBeTruthy();
rerender(
<TooltipProvider>
<ChangeRequestDetail detail={null} error="Provider unavailable" onRetry={vi.fn()} />
</TooltipProvider>,
);
expect(screen.getByRole("alert").textContent).toContain("Provider unavailable");
expect(screen.getByRole("button", { name: "Retry" }).classList.contains("min-h-11")).toBe(true);
});
});