Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/screens/leads/LeadBoardPage.test.tsx
T

187 lines
7.8 KiB
TypeScript

// R-P7 interim-UI (AD-28) — LeadBoardPage unit test. Fixture built via the real
// buildLeadBoardVM; interactions asserted on injected callbacks. i18n mocked.
// SortableKanbanBoard is stubbed so a "drag-end" is simulated by invoking its
// onCardMove prop directly — this keeps the test deterministic (no dnd-kit layout
// math in jsdom) while still exercising the screen's requestMove routing.
import { fireEvent, render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { buildLeadBoardVM, type LeadBoardData, type LeadBoardCardSource } from "@hivegrid/view-models";
import { LeadBoardPage, type LeadBoardActions } from "./LeadBoardPage";
vi.mock("react-i18next", () => ({
useTranslation: () => ({ t: (s: string) => s, i18n: { language: "de" } }),
}));
// Radix dropdowns neither open nor provide their menu context in jsdom, so the
// filter bar's facet options would be unreachable. Both the SubframeCore wrapper
// and the ~/ui menu are swapped for passthroughs; the entries stay real buttons.
vi.mock("@subframe/core", async () => {
const actual = await vi.importActual<Record<string, unknown>>("@subframe/core");
const Pass = ({ children }: { children?: React.ReactNode }) => <>{children}</>;
return { ...actual, DropdownMenu: { Root: Pass, Trigger: Pass, Portal: Pass, Content: Pass, Item: Pass } };
});
vi.mock("~/ui/components/DropdownMenuMultiSelect", () => {
const DropdownMenuMultiSelect = ({ children }: { children?: React.ReactNode }) => <div>{children}</div>;
DropdownMenuMultiSelect.DropdownItem = ({ children, onClick, selected, ...rest }: any) => (
<button type="button" onClick={onClick} aria-pressed={Boolean(selected)} data-ui-id={rest["data-ui-id"]}>
{children}
</button>
);
DropdownMenuMultiSelect.DropdownCustomItem = ({ children }: any) => <div>{children}</div>;
DropdownMenuMultiSelect.DropdownUserItem = ({ title }: any) => <div>{title}</div>;
DropdownMenuMultiSelect.DropdownDivider = () => <hr />;
return { DropdownMenuMultiSelect };
});
// Stub the shared board: render each card via the real renderCard slot and expose
// one "move" button per (card, target column) that fires onCardMove — the same
// signal a real drag-end emits.
vi.mock("~/components/ui/kanban/SortableKanbanBoard", () => ({
SortableKanbanBoard: (p: {
columns: { id: string; name: React.ReactNode; sumMode?: "visible" | "locked" | "hidden"; cards: { id: string }[] }[];
renderCard: (card: { id: string }) => React.ReactNode;
onCardMove: (cardId: string, toColumnId: string, toIndex: number) => void;
}) => (
<div data-ui-id="ui.leads.board.columns">
{p.columns.map((col) => (
<div key={col.id} data-ui-id={`stub.col.${col.id}`} data-sum-mode={col.sumMode}>
<span>{col.name}</span>
{col.cards.map((card) => (
<div key={card.id}>
{p.renderCard(card)}
{p.columns
.filter((target) => target.id !== col.id)
.map((target) => (
<button key={target.id} type="button" data-ui-id={`stub.move.${card.id}.${target.id}`} onClick={() => p.onCardMove(card.id, target.id, 0)} />
))}
</div>
))}
</div>
))}
</div>
),
}));
const byUiId = (id: string) => document.querySelector(`[data-ui-id='${id}']`) as HTMLElement;
function row(id: string, over: Partial<LeadBoardCardSource> = {}): LeadBoardCardSource {
return {
id,
vorname: "Anna",
nachname: "Muster",
email: "anna@example.test",
telefon: null,
quelle: "form",
status: "new",
zustaendigerUserId: null,
createdAt: new Date("2026-07-01T08:00:00.000Z"),
...over,
};
}
function boardData(includeSpam = false): LeadBoardData {
const vm = buildLeadBoardVM({
rows: [row("l1", { status: "new" }), row("l2", { status: "qualified" }), row("l3", { status: "converted" }), row("l5", { status: "spam" })],
includeSpam,
userLabels: new Map(),
perms: { canWrite: true },
listHref: "/acme/leads",
buildDetailHref: (id) => `/acme/leads/${id}`,
generatedAtIso: "2026-07-09T00:00:00.000Z",
});
if (vm.status !== "ready") throw new Error("fixture not ready");
return vm.data;
}
function renderPage(over: Partial<LeadBoardActions> = {}, includeSpam = false) {
const actions: LeadBoardActions = {
onSetStatus: vi.fn(),
onOpenList: vi.fn(),
onNew: vi.fn(),
onSearch: vi.fn(),
onToggleSpam: vi.fn(),
onResetFilters: vi.fn(),
...over,
};
render(<LeadBoardPage data={boardData(includeSpam)} search="" submitting={false} actions={actions} />);
return actions;
}
describe("LeadBoardPage", () => {
it("hides the spam column until the checkbox asks for it", () => {
renderPage();
expect(byUiId("ui.leads.board.card.l5")).toBeNull();
expect(document.querySelector("[data-ui-id='stub.col.spam']")).toBeNull();
});
it("shows the spam column as the rightmost one when enabled", () => {
renderPage({}, true);
expect(byUiId("ui.leads.board.card.l5")).toBeTruthy();
const columns = [...document.querySelectorAll("[data-sum-mode]")].map((node) => node.getAttribute("data-ui-id"));
expect(columns.at(-1)).toBe("stub.col.spam");
});
it("toggles the spam column through the injected callback", () => {
const { onToggleSpam } = renderPage();
fireEvent.click(byUiId("ui.leads.board.filter.visibility.spam"));
expect(onToggleSpam).toHaveBeenCalledWith(true);
});
it("takes the spam visibility back off once it is on", () => {
const { onToggleSpam } = renderPage({}, true);
fireEvent.click(byUiId("ui.leads.board.filter.visibility.spam"));
expect(onToggleSpam).toHaveBeenCalledWith(false);
});
it("wears the same shared filter bar as the list view", () => {
const { onResetFilters } = renderPage({}, true);
expect(byUiId("ui.leads.board.filter")).toBeTruthy();
expect(byUiId("ui.leads.board.filter.search")).toBeTruthy();
// Search and spam visibility clear in ONE navigation, not two racing ones.
fireEvent.click(screen.getByText("leads.filter.resetAll"));
expect(onResetFilters).toHaveBeenCalledTimes(1);
});
it("renders the four columns and a lead card", () => {
renderPage();
expect(screen.getAllByText("leads.status.new").length).toBeGreaterThan(0);
expect(screen.getAllByText("leads.status.converted").length).toBeGreaterThan(0);
expect(byUiId("ui.leads.board")).toBeTruthy();
expect(byUiId("ui.leads.board.card.l1")).toBeTruthy();
expect(byUiId("stub.col.new").getAttribute("data-sum-mode")).toBe("hidden");
});
it("submits a valid transition (new -> qualified) via onSetStatus", () => {
const actions = renderPage();
fireEvent.click(byUiId("stub.move.l1.qualified"));
expect(actions.onSetStatus).toHaveBeenCalledWith("l1", "qualified");
expect(byUiId("ui.leads.board.error")).toBeFalsy();
});
it("rejects an invalid transition from a terminal card (no submit, shows error)", () => {
const actions = renderPage();
fireEvent.click(byUiId("stub.move.l3.qualified"));
expect(actions.onSetStatus).not.toHaveBeenCalled();
expect(byUiId("ui.leads.board.error")).toBeTruthy();
});
it("routes a drag into 'Gewonnen' (converted) to the detail conversion, never a silent flip", () => {
const actions = renderPage();
fireEvent.click(byUiId("stub.move.l1.converted"));
expect(actions.onSetStatus).not.toHaveBeenCalled();
const hint = byUiId("ui.leads.board.convertHint");
expect(hint).toBeTruthy();
const link = byUiId("ui.leads.board.convertHint.link") as HTMLAnchorElement;
expect(link.getAttribute("href")).toBe("/acme/leads/l1");
});
it("navigates back to the list via the list/board toggle", () => {
const actions = renderPage();
fireEvent.click(screen.getByLabelText("leads.viewMode.list"));
expect(actions.onOpenList).toHaveBeenCalledTimes(1);
});
});