251 lines
12 KiB
TypeScript
251 lines
12 KiB
TypeScript
// R-P7 interim-UI (AD-28) — LeadDetailPage unit test. Fixture VM via the real
|
|
// buildLeadDetailVM; interactions asserted on the injected onSubmit. i18n mocked.
|
|
|
|
import { fireEvent, render, screen } from "@testing-library/react";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import { buildLeadDetailVM, type LeadDetailSource } from "@hivegrid/view-models";
|
|
import { LeadDetailPage, type LeadDetailPageActions } from "./LeadDetailPage";
|
|
|
|
vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (s: string) => s, i18n: { language: "de" } }) }));
|
|
|
|
function source(over: Partial<LeadDetailSource> = {}): LeadDetailSource {
|
|
return {
|
|
id: "l1",
|
|
quelle: "form",
|
|
sourceOrigin: null,
|
|
status: "new",
|
|
weiterverarbeitungFreigabe: "unklar",
|
|
vorname: "Anna",
|
|
nachname: "Muster",
|
|
email: "anna@example.com",
|
|
telefon: null,
|
|
message: "Rückruf",
|
|
spamScore: 12,
|
|
spamReason: null,
|
|
isQuarantined: false,
|
|
konvertiertProjektId: null,
|
|
verknuepfterKontaktId: null,
|
|
autoDeleteAt: null,
|
|
zustaendigerUserId: null,
|
|
createdAt: new Date("2026-06-01T10:00:00.000Z"),
|
|
updatedAt: new Date("2026-06-02T10:00:00.000Z"),
|
|
...over,
|
|
};
|
|
}
|
|
|
|
const CONVERT_OPTIONS = {
|
|
defaultProjektname: "Projekt Anna Muster",
|
|
pipelines: [
|
|
{ id: "p1", label: "Vertrieb", stages: [{ id: "s1", label: "Erstkontakt" }, { id: "s2", label: "Angebot" }] },
|
|
{ id: "p2", label: "Service", stages: [{ id: "s9", label: "Eingang" }] },
|
|
],
|
|
contacts: [{ id: "k1", label: "Anna Muster · anna@example.com" }, { id: "k2", label: "Bert Bauer · bert@example.com" }],
|
|
suggestedKontaktId: "k1",
|
|
contactsTruncated: false,
|
|
};
|
|
|
|
function vm(over: Partial<LeadDetailSource> = {}, canWrite = true, convert: typeof CONVERT_OPTIONS | null = CONVERT_OPTIONS) {
|
|
return buildLeadDetailVM({ lead: source(over), userLabels: new Map(), perms: { canWrite }, tenantSlug: "acme", convert });
|
|
}
|
|
|
|
function actions(over: Partial<LeadDetailPageActions> = {}): LeadDetailPageActions {
|
|
return { onSubmit: vi.fn(), ...over };
|
|
}
|
|
|
|
function renderPage(props: Partial<Parameters<typeof LeadDetailPage>[0]> = {}) {
|
|
const acts = props.actions ?? actions();
|
|
render(<LeadDetailPage vm={props.vm ?? vm()} submitting={false} errorKey={null} actions={acts} {...props} />);
|
|
return acts;
|
|
}
|
|
|
|
describe("LeadDetailPage", () => {
|
|
it("saves master data and status through one submit", () => {
|
|
// Master data, release flag and status used to be three separate buttons.
|
|
const onSubmit = vi.fn();
|
|
renderPage({ actions: actions({ onSubmit }) });
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.leads.detail.save']")!);
|
|
expect(onSubmit).toHaveBeenCalledTimes(1);
|
|
expect(onSubmit).toHaveBeenCalledWith("save", expect.objectContaining({ vorname: "Anna", weiterverarbeitungFreigabe: "unklar", status: "new" }));
|
|
});
|
|
|
|
it("carries an edited status along with the master data", () => {
|
|
const onSubmit = vi.fn();
|
|
renderPage({ actions: actions({ onSubmit }) });
|
|
const trigger = document.querySelector("[data-ui-id='ui.leads.detail.status']")!.querySelector("button")!;
|
|
fireEvent.keyDown(trigger, { key: "ArrowDown" });
|
|
fireEvent.click(screen.getByText("leads.status.qualified"));
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.leads.detail.save']")!);
|
|
expect(onSubmit).toHaveBeenCalledWith("save", expect.objectContaining({ status: "qualified" }));
|
|
});
|
|
|
|
it("keeps the save button in the header, not inside a card", () => {
|
|
renderPage();
|
|
const save = document.querySelector("[data-ui-id='ui.leads.detail.save']")!;
|
|
const basics = document.querySelector("[data-ui-id='ui.leads.detail.basics']")!;
|
|
expect(save.compareDocumentPosition(basics) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
|
|
});
|
|
|
|
it("follows the view-model after the server normalised the input", () => {
|
|
// updateLead lower-cases the e-mail. Here it normalises back to the value
|
|
// that was already stored, so the view-model returns byte-identical — only
|
|
// the finished submit reveals that the raw input is stale.
|
|
const acts = actions();
|
|
const { rerender } = render(<LeadDetailPage vm={vm()} submitting={false} errorKey={null} actions={acts} />);
|
|
const email = () => document.querySelector("[data-ui-id='ui.leads.detail.email']") as HTMLInputElement;
|
|
fireEvent.change(email(), { target: { value: "Anna@Example.COM" } });
|
|
expect(email().value).toBe("Anna@Example.COM");
|
|
|
|
rerender(<LeadDetailPage vm={vm()} submitting={true} errorKey={null} actions={acts} />);
|
|
rerender(<LeadDetailPage vm={vm()} submitting={false} errorKey={null} actions={acts} />);
|
|
expect(email().value).toBe("anna@example.com");
|
|
});
|
|
|
|
it("keeps the entered value when the save failed", () => {
|
|
const acts = actions();
|
|
const { rerender } = render(<LeadDetailPage vm={vm()} submitting={false} errorKey={null} actions={acts} />);
|
|
const email = () => document.querySelector("[data-ui-id='ui.leads.detail.email']") as HTMLInputElement;
|
|
fireEvent.change(email(), { target: { value: "kaputt" } });
|
|
rerender(<LeadDetailPage vm={vm()} submitting={true} errorKey={null} actions={acts} />);
|
|
rerender(<LeadDetailPage vm={vm()} submitting={false} errorKey="leads.error.invalidStatusTransition" actions={acts} />);
|
|
expect(email().value).toBe("kaputt");
|
|
});
|
|
|
|
it("does not offer status transitions the server would reject", () => {
|
|
// converted is terminal — offering the other statuses only produced an error.
|
|
renderPage({ vm: vm({ status: "converted", konvertiertProjektId: "p1" }) });
|
|
const trigger = document.querySelector("[data-ui-id='ui.leads.detail.status']")!.querySelector("button")!;
|
|
fireEvent.keyDown(trigger, { key: "ArrowDown" });
|
|
expect(screen.getByText("leads.status.qualified").closest("[role='option']")!.getAttribute("aria-disabled")).toBe("true");
|
|
});
|
|
|
|
it("does not offer converting a lead that is already converted", () => {
|
|
renderPage({ vm: vm({ status: "converted", konvertiertProjektId: "p1" }) });
|
|
expect(document.querySelector("[data-ui-id='ui.leads.detail.convert']")).toBeNull();
|
|
expect(screen.getByText("leads.detail.convert.done")).toBeTruthy();
|
|
});
|
|
|
|
it("treats a converted status as converted even without a linked project", () => {
|
|
// Setting the status directly leaves konvertiertProjektId null; reporting
|
|
// that as "cannot be converted" told the user the wrong reason.
|
|
renderPage({ vm: vm({ status: "converted", konvertiertProjektId: null }) });
|
|
expect(screen.getByText("leads.detail.convert.done")).toBeTruthy();
|
|
expect(screen.queryByText("leads.detail.convert.blocked")).toBeNull();
|
|
});
|
|
|
|
it("does not repeat the status as a marker badge", () => {
|
|
// The view-model marks a converted lead, which next to the status badge
|
|
// rendered the same word twice.
|
|
renderPage({ vm: vm({ status: "converted", konvertiertProjektId: "p1" }) });
|
|
const heading = document.querySelector("h1")!.textContent!;
|
|
expect(heading).not.toContain("leads.marker.converted");
|
|
expect(heading).toContain("leads.status.converted");
|
|
});
|
|
|
|
it("does not offer converting a lost lead", () => {
|
|
// Converting runs through "qualified", which lost can no longer reach.
|
|
renderPage({ vm: vm({ status: "lost" }) });
|
|
expect(document.querySelector("[data-ui-id='ui.leads.detail.convert']")).toBeNull();
|
|
expect(screen.getByText("leads.detail.convert.blocked")).toBeTruthy();
|
|
});
|
|
|
|
it("shows where the lead came from and when it changed", () => {
|
|
renderPage({ vm: vm({ zustaendigerUserId: null }) });
|
|
const meta = document.querySelector("[data-ui-id='ui.leads.detail.meta']")!;
|
|
expect(meta.textContent).toContain("leads.field.quelle");
|
|
expect(meta.textContent).toContain("leads.detail.meta.updated");
|
|
});
|
|
|
|
it("asks for board and stage before creating the project", () => {
|
|
// Projekt.pipelineId/stageId are non-null in the schema; the conversion used
|
|
// to fill them silently with the default sales board.
|
|
const onSubmit = vi.fn();
|
|
renderPage({ actions: actions({ onSubmit }) });
|
|
expect(document.querySelector("[data-ui-id='ui.leads.detail.convert.dialog']")).toBeNull();
|
|
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.leads.detail.convert']")!);
|
|
expect(document.querySelector("[data-ui-id='ui.leads.detail.convert.dialog']")).not.toBeNull();
|
|
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.leads.detail.convert.dialog.submit']")!);
|
|
expect(onSubmit).toHaveBeenCalledWith("convert", {
|
|
projektname: "Projekt Anna Muster",
|
|
pipelineId: "p1",
|
|
stageId: "s1",
|
|
kontaktId: "k1",
|
|
});
|
|
});
|
|
|
|
it("resets the stage when another board is picked", () => {
|
|
// A stage belongs to one board — carrying it over posts a stage the target
|
|
// board does not own, which the server rejects.
|
|
const onSubmit = vi.fn();
|
|
renderPage({ actions: actions({ onSubmit }) });
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.leads.detail.convert']")!);
|
|
|
|
const pipeline = document.querySelector("[data-ui-id='ui.leads.detail.convert.dialog.pipeline']")!.querySelector("button")!;
|
|
fireEvent.keyDown(pipeline, { key: "ArrowDown" });
|
|
fireEvent.click(screen.getByText("Service"));
|
|
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.leads.detail.convert.dialog.submit']")!);
|
|
expect(onSubmit).toHaveBeenCalledWith("convert", expect.objectContaining({ pipelineId: "p2", stageId: "s9" }));
|
|
});
|
|
|
|
it("preselects the contact matched by e-mail and allows dropping it", () => {
|
|
const onSubmit = vi.fn();
|
|
renderPage({ actions: actions({ onSubmit }) });
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.leads.detail.convert']")!);
|
|
|
|
const kontakt = document.querySelector("[data-ui-id='ui.leads.detail.convert.dialog.kontakt']")!.querySelector("button")!;
|
|
fireEvent.keyDown(kontakt, { key: "ArrowDown" });
|
|
fireEvent.click(screen.getByText("leads.detail.convert.kontaktNone"));
|
|
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.leads.detail.convert.dialog.submit']")!);
|
|
expect(onSubmit).toHaveBeenCalledWith("convert", expect.objectContaining({ kontaktId: "" }));
|
|
});
|
|
|
|
it("filters the contact list by the search field", () => {
|
|
renderPage();
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.leads.detail.convert']")!);
|
|
fireEvent.change(document.querySelector("[data-ui-id='ui.leads.detail.convert.dialog.kontaktSearch']")!, { target: { value: "bert" } });
|
|
|
|
const kontakt = document.querySelector("[data-ui-id='ui.leads.detail.convert.dialog.kontakt']")!.querySelector("button")!;
|
|
fireEvent.keyDown(kontakt, { key: "ArrowDown" });
|
|
expect(screen.getByText("Bert Bauer · bert@example.com")).toBeTruthy();
|
|
// The preselected contact stays listed, otherwise the Select would go blank.
|
|
expect(screen.getAllByText("Anna Muster · anna@example.com").length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it("says so when there is no board to put the project on", () => {
|
|
const empty = { ...CONVERT_OPTIONS, pipelines: [] };
|
|
renderPage({ vm: vm({}, true, empty) });
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.leads.detail.convert']")!);
|
|
expect(document.querySelector("[data-ui-id='ui.leads.detail.convert.dialog.noPipelines']")).not.toBeNull();
|
|
expect((document.querySelector("[data-ui-id='ui.leads.detail.convert.dialog.submit']") as HTMLButtonElement).disabled).toBe(true);
|
|
});
|
|
|
|
it("shows the GDPR deadline card when autoDeleteAt is set", () => {
|
|
renderPage({ vm: vm({ status: "spam", autoDeleteAt: new Date("2026-07-01T00:00:00.000Z") }) });
|
|
expect(document.querySelector("[data-ui-id='ui.leads.detail.dsgvo']")).not.toBeNull();
|
|
});
|
|
|
|
it("hides the triage/edit affordances without canWrite", () => {
|
|
renderPage({ vm: vm({}, false) });
|
|
expect(document.querySelector("[data-ui-id='ui.leads.detail.save']")).toBeNull();
|
|
expect(document.querySelector("[data-ui-id='ui.leads.detail.convert.section']")).toBeNull();
|
|
});
|
|
|
|
it("exposes click-to-call only through the optional VM and callback prop", () => {
|
|
const onCall = vi.fn();
|
|
const number = { id: "phone-1", label: "Lead", e164: "+49301234567", primary: true } as const;
|
|
renderPage({
|
|
clickToCall: {
|
|
vm: { mode: "tel-fallback", numbers: [number], requiresSelection: false, reasonKey: "telephony.call.fallback.noConnection" },
|
|
onCall,
|
|
},
|
|
});
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: "telephony.clickToCall.open" }));
|
|
fireEvent.click(screen.getByRole("button", { name: "telephony.clickToCall.openPhoneApp" }));
|
|
expect(onCall).toHaveBeenCalledWith(number, "tel-fallback");
|
|
});
|
|
});
|