334 lines
14 KiB
TypeScript
334 lines
14 KiB
TypeScript
// R-P7 interim-UI (AD-28) — LeadInboxPage unit test. Fixture VM via the real
|
|
// buildLeadListVM; interactions asserted on injected action callbacks. i18n mocked.
|
|
|
|
import React from "react";
|
|
import { fireEvent, render, screen } from "@testing-library/react";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import { buildLeadListVM, type LeadListRowSource } from "@hivegrid/view-models";
|
|
import type { LeadListQuery } from "~/utils/integrations/leads/leadListQuery";
|
|
import { LeadInboxPage, type LeadInboxActions } from "./LeadInboxPage";
|
|
|
|
vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (s: string) => s, i18n: { language: "de" } }) }));
|
|
|
|
// Radix dropdowns neither open nor provide their menu context in jsdom, so both
|
|
// the filter-bar facet options and the per-row status flyover would be
|
|
// unreachable. Root/Trigger/Portal/Content become passthroughs; Item keeps its
|
|
// `onSelect` wired to a click so the status flyover assertions stay real.
|
|
vi.mock("@subframe/core", async () => {
|
|
const actual = await vi.importActual<Record<string, unknown>>("@subframe/core");
|
|
const Pass = ({ children }: { children?: React.ReactNode }) => <>{children}</>;
|
|
const Item = ({ children, onSelect, asChild, ...rest }: any) =>
|
|
asChild && React.isValidElement(children)
|
|
? React.cloneElement(children as React.ReactElement<any>, { onClick: onSelect, ...rest })
|
|
: (
|
|
<button type="button" onClick={onSelect} {...rest}>
|
|
{children}
|
|
</button>
|
|
);
|
|
return { ...actual, DropdownMenu: { Root: Pass, Trigger: Pass, Portal: Pass, Content: Pass, Item } };
|
|
});
|
|
|
|
vi.mock("~/ui/components/DropdownMenuMultiSelect", () => {
|
|
const DropdownMenuMultiSelect = ({ children }: { children?: React.ReactNode }) => <div>{children}</div>;
|
|
DropdownMenuMultiSelect.DropdownItem = ({ children, onClick, selected, ...rest }: any) => (
|
|
<button type="button" onClick={onClick} aria-pressed={Boolean(selected)} data-ui-id={rest["data-ui-id"]}>
|
|
{children}
|
|
</button>
|
|
);
|
|
DropdownMenuMultiSelect.DropdownCustomItem = ({ children }: any) => <div>{children}</div>;
|
|
DropdownMenuMultiSelect.DropdownUserItem = ({ title }: any) => <div>{title}</div>;
|
|
DropdownMenuMultiSelect.DropdownDivider = () => <hr />;
|
|
return { DropdownMenuMultiSelect };
|
|
});
|
|
|
|
const QUERY: LeadListQuery = {
|
|
filter: { status: null, quelle: null, zustaendigerUserId: null, search: null, includeSpam: false, includeConverted: false },
|
|
sort: { field: "createdAt", dir: "desc" },
|
|
cursor: null,
|
|
};
|
|
|
|
function row(id: string, over: Partial<LeadListRowSource> = {}): LeadListRowSource {
|
|
return {
|
|
id,
|
|
quelle: "form",
|
|
status: "new",
|
|
weiterverarbeitungFreigabe: "unklar",
|
|
vorname: "Anna",
|
|
nachname: "Muster",
|
|
email: "anna@example.com",
|
|
telefon: null,
|
|
message: "Rückruf bitte",
|
|
zustaendigerUserId: null,
|
|
spamScore: 0,
|
|
spamReason: null,
|
|
isQuarantined: false,
|
|
konvertiertProjektId: null,
|
|
verknuepfterKontaktId: null,
|
|
createdAt: new Date("2026-06-01T10:00:00.000Z"),
|
|
...over,
|
|
};
|
|
}
|
|
|
|
function vm(rows: LeadListRowSource[]) {
|
|
return buildLeadListVM({
|
|
rows,
|
|
userLabels: new Map(),
|
|
total: rows.length,
|
|
nextCursor: null,
|
|
perms: { canWrite: true, canDelete: true, canReadAll: true },
|
|
tenantSlug: "acme",
|
|
createHref: "/acme/leads/new",
|
|
});
|
|
}
|
|
|
|
function actions(over: Partial<LeadInboxActions> = {}): LeadInboxActions {
|
|
return { onApplyFilter: vi.fn(), onSetStatus: vi.fn(), onBulkStatus: vi.fn(), onNextPage: vi.fn(), onOpenBoard: vi.fn(), ...over };
|
|
}
|
|
|
|
function renderPage(props: Partial<Parameters<typeof LeadInboxPage>[0]> = {}) {
|
|
const acts = props.actions ?? actions();
|
|
render(
|
|
<LeadInboxPage
|
|
vm={props.vm ?? vm([row("l1")])}
|
|
moduleActive
|
|
query={QUERY}
|
|
hasNextPage={false}
|
|
sseHref="/acme/leads/events"
|
|
submitting={false}
|
|
errorKey={null}
|
|
actions={acts}
|
|
{...props}
|
|
/>
|
|
);
|
|
return acts;
|
|
}
|
|
|
|
/** Render with a handle to swap the query, the way a navigation would. */
|
|
function renderPageWithRerender() {
|
|
const acts = actions();
|
|
const props = (query: LeadListQuery) => (
|
|
<LeadInboxPage
|
|
vm={vm([row("l1")])}
|
|
moduleActive
|
|
query={query}
|
|
hasNextPage={false}
|
|
sseHref={null}
|
|
submitting={false}
|
|
errorKey={null}
|
|
actions={acts}
|
|
/>
|
|
);
|
|
const view = render(props(QUERY));
|
|
return { rerender: (query: LeadListQuery) => view.rerender(props(query)) };
|
|
}
|
|
|
|
const byUiId = (id: string) => document.querySelector(`[data-ui-id='${id}']`) as HTMLElement;
|
|
/** A selection shows up as a chip in the bar; that chip is the readable state. */
|
|
const chip = (facet: string, value: string) => byUiId(`ui.leads.list.filter.chip.${facet}.${value}`);
|
|
|
|
describe("LeadInboxPage", () => {
|
|
it("renders the triage table + carries the SSE href", () => {
|
|
renderPage();
|
|
const root = document.querySelector("[data-ui-id='ui.leads.list']") as HTMLElement;
|
|
expect(root.getAttribute("data-sse-href")).toBe("/acme/leads/events");
|
|
expect(document.querySelector("[data-ui-id='ui.leads.list.table']")).not.toBeNull();
|
|
});
|
|
|
|
it("carries the three facets of the shared filter bar", () => {
|
|
renderPage();
|
|
expect(byUiId("ui.leads.list.filter.quelle")).toBeTruthy();
|
|
expect(byUiId("ui.leads.list.filter.status")).toBeTruthy();
|
|
expect(byUiId("ui.leads.list.filter.visibility")).toBeTruthy();
|
|
});
|
|
|
|
it("applies a filter with 'show spam' off mapped to includeSpam=false", () => {
|
|
const onApplyFilter = vi.fn();
|
|
renderPage({ actions: actions({ onApplyFilter }) });
|
|
fireEvent.click(byUiId("ui.leads.list.filter.quelle.email"));
|
|
expect(onApplyFilter).toHaveBeenCalledTimes(1);
|
|
expect(onApplyFilter.mock.calls[0][0].filter).toMatchObject({ quelle: "email", includeSpam: false });
|
|
});
|
|
|
|
it("reads an incoming status filter back into the bar, not just into the table", () => {
|
|
// Arriving from a statistics tile (or a shared link) with the filter already
|
|
// in the query — the bar has to show it, otherwise the table looks filtered
|
|
// for no visible reason.
|
|
const { rerender } = renderPageWithRerender();
|
|
expect(chip("status", "new")).toBeNull();
|
|
|
|
rerender({ ...QUERY, filter: { ...QUERY.filter, status: ["new"] } });
|
|
expect(chip("status", "new")).toBeTruthy();
|
|
|
|
rerender({ ...QUERY, filter: { ...QUERY.filter, status: ["spam"], includeSpam: true } });
|
|
expect(chip("status", "new")).toBeNull();
|
|
expect(chip("status", "spam")).toBeTruthy();
|
|
});
|
|
|
|
it("reads the two visibility options back out of the query as well", () => {
|
|
// Same read/write contract as the other facets: the spam and converted tiles
|
|
// link here with those flags set, and the bar has to show it. Both read as
|
|
// "additionally show these", so both mirror their flag directly.
|
|
const { rerender } = renderPageWithRerender();
|
|
expect(chip("visibility", "spam")).toBeNull();
|
|
expect(chip("visibility", "converted")).toBeNull();
|
|
|
|
rerender({ ...QUERY, filter: { ...QUERY.filter, status: ["spam"], includeSpam: true } });
|
|
expect(chip("visibility", "spam")).toBeTruthy();
|
|
|
|
rerender({ ...QUERY, filter: { ...QUERY.filter, status: ["converted"], includeConverted: true } });
|
|
expect(chip("visibility", "spam")).toBeNull();
|
|
expect(chip("visibility", "converted")).toBeTruthy();
|
|
});
|
|
|
|
it("turns a status pick into the query, opening spam/converted visibility with it", () => {
|
|
const onApplyFilter = vi.fn();
|
|
renderPage({ actions: actions({ onApplyFilter }) });
|
|
|
|
fireEvent.click(byUiId("ui.leads.list.filter.status.spam"));
|
|
|
|
// Filtering for spam while "hide spam" is on would always yield nothing.
|
|
expect(onApplyFilter.mock.calls.at(-1)![0].filter).toMatchObject({ status: ["spam"], includeSpam: true });
|
|
});
|
|
|
|
it("takes a status filter back off via its chip", () => {
|
|
const onApplyFilter = vi.fn();
|
|
renderPage({ actions: actions({ onApplyFilter }) });
|
|
|
|
fireEvent.click(byUiId("ui.leads.list.filter.status.qualified"));
|
|
expect(onApplyFilter.mock.calls.at(-1)![0].filter.status).toEqual(["qualified"]);
|
|
|
|
fireEvent.click(byUiId("ui.leads.list.filter.chip.status.qualified.remove"));
|
|
expect(onApplyFilter.mock.calls.at(-1)![0].filter.status).toBeNull();
|
|
});
|
|
|
|
it("clears search and every facet through the reset pill", () => {
|
|
const onApplyFilter = vi.fn();
|
|
renderPage({ actions: actions({ onApplyFilter }) });
|
|
|
|
fireEvent.click(byUiId("ui.leads.list.filter.quelle.email"));
|
|
fireEvent.click(byUiId("ui.leads.list.filter.visibility.spam"));
|
|
fireEvent.click(screen.getByText("leads.filter.resetAll"));
|
|
|
|
expect(onApplyFilter.mock.calls.at(-1)![0].filter).toMatchObject({
|
|
search: null,
|
|
status: null,
|
|
quelle: null,
|
|
includeSpam: false,
|
|
includeConverted: false,
|
|
});
|
|
});
|
|
|
|
it("changes a single lead's status from the badge flyover and saves right away", () => {
|
|
const onSetStatus = vi.fn();
|
|
renderPage({ actions: actions({ onSetStatus }) });
|
|
|
|
// Addressed by row ui-id, not by label: the status facet in the filter bar
|
|
// now carries the same five captions.
|
|
fireEvent.click(byUiId("ui.leads.list.row.status.l1.qualified"));
|
|
|
|
// No confirm step, no separate save button — picking is the write.
|
|
expect(onSetStatus).toHaveBeenCalledWith("l1", "qualified");
|
|
});
|
|
|
|
it("shows every status in the flyover but locks the ones the server would reject", () => {
|
|
const onSetStatus = vi.fn();
|
|
// "converted" is terminal — no transition out of it is allowed.
|
|
renderPage({ vm: vm([row("l1", { status: "converted", konvertiertProjektId: "p1" })]), actions: actions({ onSetStatus }) });
|
|
|
|
// All five are listed, so the user can see where a lead can go at all.
|
|
for (const status of ["new", "qualified", "converted", "lost", "spam"]) {
|
|
expect(byUiId(`ui.leads.list.row.status.l1.${status}`)).not.toBeNull();
|
|
}
|
|
fireEvent.click(byUiId("ui.leads.list.row.status.l1.qualified"));
|
|
expect(onSetStatus).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("drops the per-row quick-action column", () => {
|
|
renderPage();
|
|
expect(document.querySelector("[data-ui-id='ui.leads.list.row.qualify.l1']")).toBeNull();
|
|
expect(document.querySelector("[data-ui-id='ui.leads.list.row.spam.l1']")).toBeNull();
|
|
});
|
|
|
|
it("preselects the row's own status for a single selection and stays empty for several", () => {
|
|
renderPage({ vm: vm([row("l1", { status: "lost" }), row("l2", { vorname: "Bea" })]) });
|
|
const trigger = () => document.querySelector("[data-ui-id='ui.leads.list.bulk'] button") as HTMLElement;
|
|
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.leads.list.row.select.l1']")!);
|
|
expect(trigger().textContent).toContain("leads.status.lost");
|
|
|
|
// Two rows with different statuses: showing one of them would misstate the other.
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.leads.list.row.select.l2']")!);
|
|
expect(trigger().textContent).not.toContain("leads.status.lost");
|
|
expect(trigger().textContent).toContain("leads.actions.chooseStatus");
|
|
});
|
|
|
|
it("bulk-updates only the selected leads", () => {
|
|
const onBulkStatus = vi.fn();
|
|
renderPage({ vm: vm([row("l1"), row("l2", { vorname: "Bea", status: "lost" })]), actions: actions({ onBulkStatus }) });
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.leads.list.row.select.l2']")!);
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.leads.list.bulk.apply']")!);
|
|
// Single selection seeds the dropdown with that row's own status.
|
|
expect(onBulkStatus).toHaveBeenCalledWith(["l2"], "lost");
|
|
});
|
|
|
|
it("rides the bulk-status control inside the filter bar, behind the facets", () => {
|
|
renderPage();
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.leads.list.row.select.l1']")!);
|
|
|
|
const bar = byUiId("ui.leads.list.filter");
|
|
const bulk = byUiId("ui.leads.list.bulk");
|
|
expect(bar.contains(bulk)).toBe(true);
|
|
|
|
// Both narrow or act on the same list, so they share one strip rather than
|
|
// reading as two unrelated toolbars — the bulk control trails the facets.
|
|
const facet = byUiId("ui.leads.list.filter.visibility");
|
|
expect(facet.compareDocumentPosition(bulk) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
|
|
});
|
|
|
|
it("shows the bulk-status control only once something is selected", () => {
|
|
renderPage();
|
|
expect(document.querySelector("[data-ui-id='ui.leads.list.bulk']")).toBeNull();
|
|
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.leads.list.row.select.l1']")!);
|
|
expect(document.querySelector("[data-ui-id='ui.leads.list.bulk']")).not.toBeNull();
|
|
|
|
// Deselecting the last row takes the control away again.
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.leads.list.row.select.l1']")!);
|
|
expect(document.querySelector("[data-ui-id='ui.leads.list.bulk']")).toBeNull();
|
|
});
|
|
|
|
it("navigates to the board view via the list/board toggle", () => {
|
|
const onOpenBoard = vi.fn();
|
|
renderPage({ actions: actions({ onOpenBoard }) });
|
|
fireEvent.click(screen.getByLabelText("leads.viewMode.board"));
|
|
expect(onOpenBoard).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("opens the create dialog instead of navigating to the create page", () => {
|
|
renderPage();
|
|
expect(document.querySelector("[data-ui-id='ui.leads.new.dialog']")).toBeNull();
|
|
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.leads.list.new']")!);
|
|
|
|
const form = document.querySelector("[data-ui-id='ui.leads.new.dialog']") as HTMLFormElement;
|
|
expect(form.getAttribute("action")).toBe("/acme/leads/new");
|
|
expect(form.getAttribute("method")).toBe("post");
|
|
// A lead needs at least one identifying detail before it can be submitted.
|
|
const submit = document.querySelector("[data-ui-id='ui.leads.new.dialog.submit']") as HTMLButtonElement;
|
|
expect(submit.disabled).toBe(true);
|
|
fireEvent.input(document.querySelector("[data-ui-id='ui.leads.new.dialog.email']")!, { target: { value: "a@example.com" } });
|
|
expect((document.querySelector("[data-ui-id='ui.leads.new.dialog.submit']") as HTMLButtonElement).disabled).toBe(false);
|
|
});
|
|
|
|
it("renders no inline admin nav (sources/anti-spam moved to the leads sub-sidenav)", () => {
|
|
renderPage();
|
|
expect(document.querySelector("[data-ui-id='ui.leads.list.nav']")).toBeNull();
|
|
});
|
|
|
|
it("shows the locked state when the module is off", () => {
|
|
renderPage({ moduleActive: false });
|
|
expect(document.querySelector("[data-ui-id='ui.leads.list.table']")).toBeNull();
|
|
expect(screen.getByText("leads.list.locked")).toBeTruthy();
|
|
});
|
|
});
|