Files

58 lines
2.7 KiB
TypeScript

// R-P6 interim-UI (AD-28) — CustomFieldAdminPage unit test. Fixture VM via the
// real buildCustomFieldAdminVM; asserts the create/archive callbacks. i18n mocked.
import { fireEvent, render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { buildCustomFieldAdminVM } from "@hivegrid/view-models";
import { CustomFieldAdminPage, type CustomFieldAdminPageActions } from "./CustomFieldAdminPage";
vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (s: string) => s, i18n: { language: "de" } }) }));
const vm = buildCustomFieldAdminVM({
rows: [{ id: "cf1", entityType: "kontakt", key: "budget", label: "Budget", datentyp: "waehrung", optionen: null, archiviert: false, isDemoData: false }],
entityType: "kontakt",
canWrite: true,
});
function actions(over: Partial<CustomFieldAdminPageActions> = {}): CustomFieldAdminPageActions {
return { onCreate: vi.fn(), onArchive: vi.fn(), ...over };
}
function renderPage(acts = actions()) {
render(<CustomFieldAdminPage vm={vm} moduleActive submitting={false} errorKey={null} actions={acts} />);
return acts;
}
describe("CustomFieldAdminPage", () => {
it("renders the definition row", () => {
renderPage();
expect(document.querySelector("[data-ui-id='ui.crm.admin.customFields.table']")).not.toBeNull();
expect(screen.getByText("Budget")).toBeTruthy();
});
it("soft-deletes a definition via the delete button", () => {
const onArchive = vi.fn();
renderPage(actions({ onArchive }));
fireEvent.click(screen.getByText("crm.actions.delete"));
expect(onArchive).toHaveBeenCalledWith("cf1");
});
it("creates a definition with key + label", () => {
const onCreate = vi.fn();
renderPage(actions({ onCreate }));
fireEvent.change(document.querySelector("[data-ui-id='ui.crm.admin.customFields.create.key']")!, { target: { value: "leadsource" } });
fireEvent.change(document.querySelector("[data-ui-id='ui.crm.admin.customFields.create.label']")!, { target: { value: "Lead source" } });
fireEvent.click(document.querySelector("[data-ui-id='ui.crm.admin.customFields.create.submit']")!);
expect(onCreate).toHaveBeenCalledWith(expect.objectContaining({ key: "leadsource", label: "Lead source", datentyp: "text" }));
});
it("puts the create form above the table", () => {
// The page exists to add fields and the list only grows, so the form must
// not drift below it.
renderPage(actions());
const form = document.querySelector("[data-ui-id='ui.crm.admin.customFields.create']")!;
const table = document.querySelector("[data-ui-id='ui.crm.admin.customFields.table']")!;
expect(form.compareDocumentPosition(table) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
});
});