// R-P6 interim-UI (AD-28) — KontaktDetailPage unit test. Fixture data built via // the real buildKontaktDetailVM / buildActivityStreamVM; interactions asserted on // the injected onSubmit callback. i18n mocked so `t` echoes the key. import { fireEvent, render, screen } from "@testing-library/react"; import { describe, expect, it, vi } from "vitest"; import { buildActivityStreamVM, buildKontaktDetailVM, type BuildKontaktDetailVMInput, type KontaktDetailData } from "@hivegrid/view-models"; import { KontaktDetailPage, type KontaktDetailPageActions } from "./KontaktDetailPage"; vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (s: string) => s, i18n: { language: "de" } }), })); const emptyActivity = buildActivityStreamVM({ rows: [], userLabels: new Map(), hasMore: false }); function detailData(over: Partial = {}, perms = { canWrite: true, canDelete: true, canInviteToPortal: true }): KontaktDetailData { const vm = buildKontaktDetailVM({ detail: { kontakt: { id: "k1", kontaktTyp: "privatperson", kontaktStatus: "kontakt", vorname: "Anna", nachname: "Muster", orgName: null, orgMerkmal: null, primaerEmail: "anna@example.com", zustaendigerUserId: null, archivedAt: null, createdAt: new Date("2026-06-01T10:00:00.000Z"), updatedAt: new Date("2026-06-02T10:00:00.000Z"), }, emails: [{ id: "e1", email: "anna@example.com", primaer: true }], telefone: [], relationen: [], adresse: null, }, customFields: [], tags: [], userLabels: new Map(), perms, tenantSlug: "acme", ...over, }); if (vm.status !== "ready" || !vm.data) throw new Error("fixture not ready"); return vm.data; } function actions(over: Partial = {}): KontaktDetailPageActions { return { onSubmit: vi.fn(), ...over }; } function renderPage(props: Partial[0]> = {}) { const acts = props.actions ?? actions(); render( ); return acts; } describe("KontaktDetailPage", () => { it("renders the name and the main sections", () => { renderPage(); expect(screen.getAllByText("Anna Muster").length).toBeGreaterThan(0); expect(document.querySelector("[data-ui-id='ui.crm.detail.basics']")).not.toBeNull(); expect(document.querySelector("[data-ui-id='ui.crm.detail.activity']")).not.toBeNull(); }); it("saves the inline-edited basics via updateBasics", () => { const onSubmit = vi.fn(); renderPage({ actions: actions({ onSubmit }) }); fireEvent.click(document.querySelector("[data-ui-id='ui.crm.detail.basics.save']")!); expect(onSubmit).toHaveBeenCalledWith("updateBasics", expect.objectContaining({ vorname: "Anna", nachname: "Muster", kontaktStatus: "kontakt" })); }); it("adds an email via addEmail", () => { const onSubmit = vi.fn(); renderPage({ actions: actions({ onSubmit }) }); fireEvent.change(document.querySelector("[data-ui-id='ui.crm.detail.emails.new']")!, { target: { value: "neu@example.com" } }); const addButtons = screen.getAllByText("crm.actions.add"); fireEvent.click(addButtons[0]); expect(onSubmit).toHaveBeenCalledWith("addEmail", expect.objectContaining({ email: "neu@example.com" })); }); it("archives and deletes via the quick actions", () => { const onSubmit = vi.fn(); renderPage({ actions: actions({ onSubmit }) }); fireEvent.click(document.querySelector("[data-ui-id='ui.crm.detail.archive']")!); fireEvent.click(document.querySelector("[data-ui-id='ui.crm.detail.delete']")!); expect(onSubmit).toHaveBeenCalledWith("archive"); expect(onSubmit).toHaveBeenCalledWith("delete"); }); it("offers the gated portal invitation through the injected route action", () => { const onSubmit = vi.fn(); renderPage({ actions: actions({ onSubmit }) }); fireEvent.click(document.querySelector("[data-ui-id='ui.crm.detail.portalInvite']")!); expect(onSubmit).toHaveBeenCalledWith("invitePortalContact"); }); it("shows clear feedback after the setup invitation was queued", () => { renderPage({ messageKey: "customerPortal.invitation.sent" }); expect(document.querySelector("[data-ui-id='ui.crm.detail.portalInviteSuccess']")).not.toBeNull(); expect(screen.getByText("customerPortal.invitation.sent")).toBeTruthy(); }); it("hides write affordances without canWrite", () => { renderPage({ data: detailData({}, { canWrite: false, canDelete: false, canInviteToPortal: false }) }); expect(document.querySelector("[data-ui-id='ui.crm.detail.basics.save']")).toBeNull(); expect(document.querySelector("[data-ui-id='ui.crm.detail.actions']")).toBeNull(); expect(document.querySelector("[data-ui-id='ui.crm.detail.portalInvite']")).toBeNull(); }); it("exposes click-to-call only through the optional VM and callback prop", () => { const onCall = vi.fn(); const number = { id: "phone-1", label: "Mobil", e164: "+491701234567", primary: true } as const; renderPage({ clickToCall: { vm: { mode: "provider", numbers: [number], requiresSelection: false, reasonKey: null }, onCall, }, }); fireEvent.click(screen.getByRole("button", { name: "telephony.clickToCall.open" })); fireEvent.click(screen.getByRole("button", { name: "telephony.clickToCall.startCall" })); expect(onCall).toHaveBeenCalledWith(number, "provider"); }); });