// WF-P8 S06 — LostReasonAdminPage unit test. Fixture via the real // buildLostReasonAdminVM; interactions asserted on the injected onSubmit. // The row menu is a Radix dropdown, so the entries are driven through the // component's own state rather than through the portal. import { fireEvent, render, screen } from "@testing-library/react"; import { describe, expect, it, vi } from "vitest"; import { buildLostReasonAdminVM, type LostReasonAdminData } from "@hivegrid/view-models"; import { filterLostReasons, LostReasonAdminPage } from "./LostReasonAdminPage"; 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 // row actions would be unreachable. Both the SubframeCore wrapper and the ~/ui // menu are swapped for plain passthroughs; the entries stay real buttons, which // is what these tests are about. vi.mock("@subframe/core", async () => { const actual = await vi.importActual>("@subframe/core"); const Pass = ({ children }: { children?: React.ReactNode }) => <>{children}; return { ...actual, DropdownMenu: { Root: Pass, Trigger: Pass, Portal: Pass, Content: Pass } }; }); vi.mock("~/ui/components/DropdownMenu", () => { const DropdownMenu = ({ children }: { children?: React.ReactNode }) =>
{children}
; DropdownMenu.DropdownItem = ({ children, onSelect, ...rest }: any) => ( ); DropdownMenu.DropdownDivider = () =>
; return { DropdownMenu }; }); const byUiId = (id: string) => document.querySelector(`[data-ui-id='${id}']`) as HTMLElement; type FixtureReason = Parameters[0]["reasons"][number]; const defaultReasons: FixtureReason[] = [ { id: "lr1", name: "Preis zu hoch", beschreibung: "Angebot lag über dem Budgetrahmen.", kategorie: "Preis", position: 10, isDemoData: false, istVorlage: true, projectCount: 34, lastUsedIso: "2026-07-12T06:42:00.000Z", }, { id: "lr2", name: "Dublette", position: 20, isDemoData: false, istVorlage: false, archivedAt: "2026-07-01T00:00:00.000Z", projectCount: 0, lastUsedIso: null, }, ]; function reasonData( reasons: FixtureReason[] = defaultReasons, insights?: { topReasonName: string | null; recentLossCount: number; previousLossCount: number } ): LostReasonAdminData { const vm = buildLostReasonAdminVM({ canAdmin: true, reasons, insights }); if (vm.status !== "ready") throw new Error("fixture not ready"); return vm.data; } function renderPage(data = reasonData()) { const onSubmit = vi.fn(); render(); return onSubmit; } describe("LostReasonAdminPage", () => { it("renders the S06 table with origin, usage and status", () => { renderPage(); expect(byUiId("ui.projects.lostReasons.list")).toBeTruthy(); expect(screen.getByText("Preis zu hoch")).toBeTruthy(); expect(screen.getByText("Angebot lag über dem Budgetrahmen.")).toBeTruthy(); expect(screen.getAllByText("projects.lostReasonsAdmin.origin.template")).toHaveLength(1); expect(screen.getAllByText("projects.lostReasonsAdmin.origin.own")).toHaveLength(1); expect(byUiId("ui.projects.lostReasons.row.lr1.projects").textContent).toContain("projects.lostReasonsAdmin.projectCount"); expect(byUiId("ui.projects.lostReasons.footer").textContent).toContain("projects.lostReasonsAdmin.footerLine"); }); it("labels the export's five columns and leaves the row-menu column unheaded", () => { renderPage(); expect([...document.querySelectorAll("th")].map((th) => th.textContent?.trim())).toEqual([ "projects.lostReasonsAdmin.columns.reason", "projects.lostReasonsAdmin.columns.description", "projects.lostReasonsAdmin.columns.projects", "projects.lostReasonsAdmin.columns.status", "projects.lostReasonsAdmin.columns.lastUsed", "", ]); }); it("sorts archived reasons to the end, whatever their position says", () => { // The archived "Dublette" carries the higher position but used to arrive // first, pushing the reasons still in use down the list. const data = reasonData(); expect(data.reasons.map((reason) => reason.name)).toEqual(["Preis zu hoch", "Dublette"]); renderPage(data); expect([...document.querySelectorAll("[data-row-id]")].map((row) => row.getAttribute("data-row-id"))).toEqual(["lr1", "lr2"]); }); it("keeps both insight compartments, even with nothing to compare against", () => { // The card used to drop the comparison entirely, which read as a broken header. renderPage(reasonData(defaultReasons, { topReasonName: null, recentLossCount: 0, previousLossCount: 0 })); const insights = byUiId("ui.projects.lostReasons.insights"); expect(insights.textContent).toContain("projects.lostReasonsAdmin.insights.topReason"); expect(insights.textContent).toContain("projects.lostReasonsAdmin.insights.monthComparison"); expect(byUiId("ui.projects.lostReasons.insights.change").textContent).toContain("projects.lostReasonsAdmin.insights.noComparison"); }); it("puts search, status and the add action into one filter bar and no button into the header", () => { renderPage(); const bar = byUiId("ui.projects.lostReasons.filter"); expect(bar.querySelector("[data-ui-id='ui.projects.lostReasons.filter.search']")).toBeTruthy(); expect(bar.querySelector("[data-ui-id='ui.projects.lostReasons.filter.status']")).toBeTruthy(); expect(bar.querySelector("[data-ui-id='ui.projects.lostReasons.new.toggle']")).toBeTruthy(); // The header carries the loss picture only — no back link, no add button. expect(byUiId("ui.projects.lostReasons.insights").querySelector("button")).toBeNull(); }); it("shows an empty state when there are no reasons", () => { renderPage(reasonData([])); expect(byUiId("ui.projects.lostReasons.empty")).toBeTruthy(); }); it("adds a reason with description and category", () => { const onSubmit = renderPage(); fireEvent.click(byUiId("ui.projects.lostReasons.new.toggle")); fireEvent.change(byUiId("ui.projects.lostReasons.new.name"), { target: { value: "Timing" } }); fireEvent.change(byUiId("ui.projects.lostReasons.new.kategorie"), { target: { value: "Zeitplan" } }); fireEvent.change(byUiId("ui.projects.lostReasons.new.beschreibung"), { target: { value: "Kunde verschiebt." } }); fireEvent.click(byUiId("ui.projects.lostReasons.new.create")); expect(onSubmit).toHaveBeenCalledWith("upsertLostReason", { name: "Timing", kategorie: "Zeitplan", beschreibung: "Kunde verschiebt.", }); }); it("archives an active reason and reactivates an archived one", () => { const onSubmit = renderPage(); fireEvent.click(byUiId("ui.projects.lostReasons.row.lr1.archive")); expect(onSubmit).toHaveBeenCalledWith("archiveLostReason", { reasonId: "lr1" }); // The archived row offers the way back instead of a second archive. expect(byUiId("ui.projects.lostReasons.row.lr2.archive")).toBeNull(); fireEvent.click(byUiId("ui.projects.lostReasons.row.lr2.reactivate")); expect(onSubmit).toHaveBeenCalledWith("reactivateLostReason", { reasonId: "lr2" }); }); it("edits a reason in place", () => { const onSubmit = renderPage(); fireEvent.click(byUiId("ui.projects.lostReasons.row.lr1.edit")); fireEvent.change(byUiId("ui.projects.lostReasons.row.lr1.kategorieInput"), { target: { value: "Budget" } }); fireEvent.click(byUiId("ui.projects.lostReasons.row.lr1.save")); expect(onSubmit).toHaveBeenCalledWith("upsertLostReason", { reasonId: "lr1", name: "Preis zu hoch", beschreibung: "Angebot lag über dem Budgetrahmen.", kategorie: "Budget", position: "10", }); }); it("filters by search text and status", () => { const rows = reasonData().reasons; const names = (result: readonly { name: string }[]) => result.map((row) => row.name); expect(names(filterLostReasons(rows, { search: "", status: "active" }))).toEqual(["Preis zu hoch"]); expect(names(filterLostReasons(rows, { search: "", status: "archived" }))).toEqual(["Dublette"]); // Search covers description and category, not just the name. expect(names(filterLostReasons(rows, { search: "budgetrahmen", status: "all" }))).toEqual(["Preis zu hoch"]); expect(names(filterLostReasons(rows, { search: "preis", status: "all" }))).toEqual(["Preis zu hoch"]); expect(filterLostReasons(rows, { search: "gibtsnicht", status: "all" })).toEqual([]); }); it("compares the loss windows and does not invent a percentage out of zero", () => { const withPrevious = reasonData(defaultReasons, { topReasonName: "Preis zu hoch", recentLossCount: 28, previousLossCount: 25 }); expect(withPrevious.insights.changePercent).toBe(12); expect(withPrevious.insights.changeIsWorse).toBe(true); // Nothing to compare against: a jump from 0 is not "+infinity percent". const withoutPrevious = reasonData(defaultReasons, { topReasonName: null, recentLossCount: 5, previousLossCount: 0 }); expect(withoutPrevious.insights.changePercent).toBeNull(); expect(withoutPrevious.insights.changeIsWorse).toBe(false); }); });