// R-P11b interim-UI (AD-28) — DashboardPage unit test. Fixture via the real // buildDashboardPageVM. The page is display-only: it renders the derived layout // and owns no controls. i18n mocked. import { render, screen } from "@testing-library/react"; import { describe, expect, it, vi } from "vitest"; import { buildDashboardPageVM, type BuildDashboardPageInput, type DashboardPageVM, } from "@hivegrid/view-models"; import { DashboardPage } from "./DashboardPage"; vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (s: string) => s, i18n: { language: "de" } }), })); const byUiId = (id: string) => document.querySelector(`[data-ui-id='${id}']`) as HTMLElement; function fixtureInput(overrides: Partial = {}): BuildDashboardPageInput { return { catalog: [ { key: "general.welcome", domain: "general", kind: "hero", titleKey: "app.dashboard.widgets.welcome.title", descriptionKey: "app.dashboard.widgets.welcome.description", requiredAnyPermission: ["dashboard.read"], defaultPeriod: "today", }, { key: "sales.forecast", domain: "sales", kind: "trend", titleKey: "app.dashboard.widgets.salesForecast.title", descriptionKey: "app.dashboard.widgets.salesForecast.description", requiredAnyPermission: ["deals.read_aggregates"], defaultPeriod: "quarter", }, { key: "tasks.my_open", domain: "tasks", kind: "list", titleKey: "app.dashboard.widgets.myTasks.title", descriptionKey: "app.dashboard.widgets.myTasks.description", requiredAnyPermission: ["tasks.read_own"], defaultPeriod: "today", }, ], layouts: [ { role: "gf", nameKey: "app.dashboard.roles.gf.name", descriptionKey: "app.dashboard.roles.gf.description", mobileFirst: false, widgets: [ { key: "general.welcome", position: { x: 0, y: 0 }, size: { w: 12, h: 1 } }, { key: "sales.forecast", position: { x: 0, y: 1 }, size: { w: 4, h: 2 } }, { key: "tasks.my_open", position: { x: 4, y: 1 }, size: { w: 4, h: 2 } }, ], }, { role: "monteur", nameKey: "app.dashboard.roles.monteur.name", descriptionKey: "app.dashboard.roles.monteur.description", mobileFirst: true, widgets: [{ key: "tasks.my_open", position: { x: 0, y: 0 }, size: { w: 12, h: 2 } }], }, ], snapshot: { generatedAt: "2026-07-12T08:00:00.000Z", sales: { openProjects: 7, wonProjects: 3, forecastValue: 125000, winRate: { won: 18, lost: 7, rate: 0.72, outcomes: [ { id: "won", labelKey: "app.dashboard.winRate.won", value: 18, color: "#334155" }, { id: "lost", labelKey: "app.dashboard.winRate.lost", value: 7, color: "#cbd5e1" }, ], }, orderIntake: { currentMonth: 42000, trailingTwelveMonths: 318000, series: { points: [ { id: "2026-05", value: 21000, comparisonValue: 18000 }, { id: "2026-06", value: 35000, comparisonValue: 30000 }, { id: "2026-07", value: 42000, comparisonValue: 26000 }, ], valueFormat: "currency", hasComparison: true, }, }, }, projects: { activeProjects: 5, dueSoon: 2, openDefects: 1, myProjects: [], stageDistribution: [{ id: "stage-1", label: "Vertrieb · Qualifiziert", value: 4, color: "#2563eb" }], }, tasks: { myOpen: 4, dueToday: 1, items: [{ id: "task-1", label: "Angebot nachfassen", href: "/app/acme/tasks", meta: "2026-07-14" }], }, hr: { pendingRequests: 2 }, activity: { items: [] }, comms: { callsToday: 6 }, finance: { openReceivables: 48000, overdueReceivables: 12500, agingBuckets: [ { id: "not_due", labelKey: "app.dashboard.receivables.notDue", value: 35500, color: "#94a3b8" }, { id: "overdue_1_30", labelKey: "app.dashboard.receivables.overdue1To30", value: 8000, color: "#64748b" }, { id: "overdue_31_60", labelKey: "app.dashboard.receivables.overdue31To60", value: 3000, color: "#475569" }, { id: "overdue_61_90", labelKey: "app.dashboard.receivables.overdue61To90", value: 1000, color: "#d97706" }, { id: "overdue_90_plus", labelKey: "app.dashboard.receivables.overdue90Plus", value: 500, color: "#b91c1c" }, ], averageDaysToPayment: 27, }, schedule: { today: [] }, }, permissions: ["dashboard.read", "deals.read_aggregates", "tasks.read_own"], activeRole: "gf", streamUrl: "/app/acme/dashboard/stream", canCustomize: true, ...overrides, }; } function renderPage(vm: DashboardPageVM) { render(); } describe("DashboardPage", () => { it("renders the active role layout with hero, KPI, and list widgets", () => { renderPage(buildDashboardPageVM(fixtureInput())); expect(byUiId("ui.dashboard.roleLayouts")).toBeTruthy(); expect(byUiId("ui.dashboard.widget.general.welcome")).toBeTruthy(); expect(byUiId("ui.dashboard.widget.sales.forecast")).toBeTruthy(); expect(byUiId("ui.dashboard.widget.tasks.my_open")).toBeTruthy(); expect(screen.getByText("Angebot nachfassen")).toBeTruthy(); expect(screen.getByText(/125\.000/)).toBeTruthy(); }); it("wraps single-metric tiles in a link when the widget carries an href", () => { renderPage(buildDashboardPageVM(fixtureInput({ widgetHrefs: { "sales.forecast": "/acme/projects/boards" } }))); const link = byUiId("ui.dashboard.widget.sales.forecast.link"); expect(link).toBeTruthy(); expect(link?.getAttribute("href")).toBe("/acme/projects/boards"); }); it("uses the 12-column layout contract and exposes chart data as an accessible table", () => { const base = fixtureInput(); const catalog: BuildDashboardPageInput["catalog"] = [ ...base.catalog, { key: "projects.stage_distribution", domain: "projects", kind: "chart", titleKey: "app.dashboard.widgets.stageDistribution.title", descriptionKey: "app.dashboard.widgets.stageDistribution.description", requiredAnyPermission: ["dashboard.read"], defaultPeriod: "30d", }, ]; const layouts: BuildDashboardPageInput["layouts"] = [ { ...base.layouts[0]!, widgets: [ ...base.layouts[0]!.widgets, { key: "projects.stage_distribution", position: { x: 0, y: 3 }, size: { w: 8, h: 3 } }, ], }, ]; renderPage(buildDashboardPageVM(fixtureInput({ catalog, layouts }))); expect(byUiId("ui.dashboard.widget.projects.stage_distribution")?.parentElement?.className).toContain("lg:col-span-8"); expect(byUiId("ui.dashboard.widget.projects.stage_distribution")?.parentElement?.className).toContain("lg:col-start-1"); expect(screen.getByRole("table", { name: "app.dashboard.widgets.stageDistribution.title" })).toBeTruthy(); expect(screen.getByRole("rowheader", { name: "Vertrieb · Qualifiziert" })).toBeTruthy(); expect(screen.queryByText("app.dashboard.customize.available")).toBeNull(); }); it("renders a monthly series widget with an accessible month table", async () => { const base = fixtureInput(); const catalog: BuildDashboardPageInput["catalog"] = [ ...base.catalog, { key: "sales.order_intake", domain: "sales", kind: "trend", titleKey: "app.dashboard.widgets.orderIntake.title", descriptionKey: "app.dashboard.widgets.orderIntake.description", requiredAnyPermission: ["deals.read_aggregates"], defaultPeriod: "quarter", }, ]; const layouts: BuildDashboardPageInput["layouts"] = [ { ...base.layouts[0]!, widgets: [{ key: "sales.order_intake", position: { x: 0, y: 0 }, size: { w: 8, h: 3 } }] }, ]; renderPage(buildDashboardPageVM(fixtureInput({ catalog, layouts }))); const table = await screen.findByRole("table", { name: "app.dashboard.widgets.orderIntake.title" }); expect(table).toBeTruthy(); // Every month of the series is readable without the chart, including the // previous-year column the visual encoding carries as a second bar. expect(screen.getByRole("columnheader", { name: "app.dashboard.series.previousYear" })).toBeTruthy(); expect(screen.getAllByRole("rowheader")).toHaveLength(3); // Once as the headline metric, once in the month table. expect(screen.getAllByText(/42\.000/).length).toBeGreaterThanOrEqual(2); }); it("marks permission-hidden widgets with clear text instead of data", () => { renderPage(buildDashboardPageVM(fixtureInput({ permissions: ["dashboard.read"] }))); expect(screen.getAllByText("app.dashboard.widgets.permissionHidden").length).toBeGreaterThan(0); expect(screen.queryByText(/125\.000/)).toBeNull(); }); it("renders the derived layout without offering a role switcher", () => { renderPage(buildDashboardPageVM(fixtureInput())); expect(document.querySelector("[data-ui-id^='ui.dashboard.roleSwitcher']")).toBeNull(); }); it("renders the Monteur layout when the server derived it", () => { renderPage(buildDashboardPageVM(fixtureInput({ activeRole: "monteur" }))); expect(byUiId("ui.dashboard.widget.tasks.my_open")).toBeTruthy(); expect(byUiId("ui.dashboard.widget.sales.forecast")).toBeNull(); }); it("offers no live-update controls", () => { renderPage(buildDashboardPageVM(fixtureInput())); expect(byUiId("ui.dashboard.liveBar")).toBeNull(); expect(byUiId("ui.dashboard.live.toggle")).toBeNull(); expect(byUiId("ui.dashboard.live.lastUpdated")).toBeNull(); }); it("renders a plain-language loading state", () => { renderPage({ status: "loading" }); expect(byUiId("ui.dashboard.page.loading")).toBeTruthy(); expect(screen.getByText("app.dashboard.loading")).toBeTruthy(); }); it("renders the catalog error message in the error state", () => { renderPage({ status: "error", error: { messageKey: "app.dashboard.errors.tenantNotFound" } }); expect(byUiId("ui.dashboard.page.error")).toBeTruthy(); expect(screen.getByText("app.dashboard.errors.tenantNotFound")).toBeTruthy(); }); });