// R-P7 interim-UI (AD-28) — LeadStatisticsPage unit test. Fixture VM via the real // buildLeadStatisticsVM. i18n mocked. import { fireEvent, render, screen } from "@testing-library/react"; import { MemoryRouter } from "react-router"; import { describe, expect, it, vi } from "vitest"; import { buildLeadStatisticsVM, type LeadStatisticsVM } from "@hivegrid/view-models"; import { LeadStatisticsPage } from "./LeadStatisticsPage"; vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (s: string) => s, i18n: { language: "de" } }) })); const vm = buildLeadStatisticsVM({ stats: { total: 12, new: 5, converted: 4, spam: 1, byQuelle: [{ quelle: "form", count: 8 }], byStatus: [{ status: "new", count: 5 }] }, canReadAll: true, listHref: "/acme/leads", }); function renderPage(state: LeadStatisticsVM, onOpenLeads = vi.fn()) { render( ); return { onOpenLeads }; } describe("LeadStatisticsPage", () => { it("renders the page header, the four KPI tiles and both breakdowns", () => { renderPage(vm); expect(screen.getByRole("heading", { level: 1 }).textContent).toBe("leads.statistics.title"); expect(document.querySelector("[data-ui-id='ui.leads.statistics.kpi.total']")).not.toBeNull(); expect(document.querySelector("[data-ui-id='ui.leads.statistics.kpi.spam']")).not.toBeNull(); expect(document.querySelector("[data-ui-id='ui.leads.statistics.bySource']")).not.toBeNull(); expect(document.querySelector("[data-ui-id='ui.leads.statistics.byStatus']")).not.toBeNull(); expect(document.querySelector("[data-ui-id='ui.leads.statistics.bySource.row.form']")).not.toBeNull(); expect(screen.getByText("12")).toBeTruthy(); }); it("hides the own-scope hint when the user may read all leads", () => { renderPage(vm); expect(document.querySelector("[data-ui-id='ui.leads.statistics.scopeHint']")).toBeNull(); renderPage(buildLeadStatisticsVM({ stats: { ...emptyStats, total: 3, new: 3 }, canReadAll: false, listHref: "/acme/leads" })); expect(document.querySelector("[data-ui-id='ui.leads.statistics.scopeHint']")).not.toBeNull(); }); it("shows the plaintext empty state when the tenant has no leads yet", () => { renderPage(buildLeadStatisticsVM({ stats: emptyStats, canReadAll: true, listHref: "/acme/leads" })); expect(document.querySelector("[data-ui-id='ui.leads.statistics.empty']")).not.toBeNull(); expect(screen.getByText("leads.statistics.empty.body")).toBeTruthy(); }); it("shows a plaintext error instead of a bare state", () => { renderPage({ status: "error", error: { messageKey: "leads.errors.loadFailed" } }); expect(screen.getByText("leads.errors.loadFailed")).toBeTruthy(); }); it("turns each KPI tile into a shortcut to the matching inbox filter", () => { const { onOpenLeads } = renderPage(vm); for (const kpi of ["total", "new", "converted", "spam"]) { fireEvent.click(document.querySelector(`[data-ui-id='ui.leads.statistics.kpi.${kpi}.link']`)!); } expect(onOpenLeads.mock.calls.map((call) => call[0])).toEqual(["total", "new", "converted", "spam"]); }); }); const emptyStats = { total: 0, new: 0, converted: 0, spam: 0, byQuelle: [], byStatus: [] } as const;