Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/screens/projects/PipelineFieldTemplatePage.test.tsx
T

229 lines
10 KiB
TypeScript

// WF-P8 S05 — the field-type picker and the options editor of the inspector.
// Fixture built through the real buildPipelineFieldTemplateVM; interactions are
// asserted on the injected onSubmit. i18n mocked so `t` echoes the key.
import { fireEvent, render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { buildPipelineFieldTemplateVM, type BuildPipelineFieldTemplateVMInput } from "@hivegrid/view-models";
import { PipelineFieldTemplatePage } from "./PipelineFieldTemplatePage";
const byUiId = (id: string) => document.querySelector(`[data-ui-id='${id}']`) as HTMLElement;
vi.mock("react-i18next", () => ({
useTranslation: () => ({ t: (s: string) => s, i18n: { language: "de" } }),
}));
function vmInput(over: Partial<BuildPipelineFieldTemplateVMInput> = {}): BuildPipelineFieldTemplateVMInput {
return {
pipelineId: "pipe_1",
pipelineName: "Vertrieb",
groups: [{ id: "grp_base", name: "Grunddaten", reihenfolge: 0, istSystem: true }],
fields: [
{
id: "cfg_sys",
fieldKey: "projektname",
groupId: "grp_base",
sichtbar: true,
pflicht: true,
pflichtAbStageId: null,
hinweis: null,
istSystem: true,
gesperrt: false,
reihenfolge: 0,
},
{
id: "cfg_custom",
fieldKey: "status_aufmass",
groupId: "grp_base",
sichtbar: true,
pflicht: false,
pflichtAbStageId: null,
hinweis: null,
istSystem: false,
gesperrt: false,
reihenfolge: 1,
},
],
poolFields: [],
stages: [{ id: "stage_1", name: "Qualifiziert" }],
definitions: {
projektname: { label: "Projektname", datentyp: "text" },
status_aufmass: {
id: "def_1",
label: "Aufmaß-Status",
datentyp: "select",
optionen: { options: [{ value: "offen", label: "Offen", color: "#ff0000" }, { value: "erledigt", label: "Erledigt", color: null }] },
},
},
alwaysVisibleKeys: ["projektname"],
canEdit: true,
...over,
};
}
function renderPage(over: Partial<BuildPipelineFieldTemplateVMInput> = {}) {
const onSubmit = vi.fn();
render(<PipelineFieldTemplatePage data={buildPipelineFieldTemplateVM(vmInput(over))} backHref="/acme/admin/pipelines" submitting={false} actions={{ onSubmit }} />);
return onSubmit;
}
function selectField(id: string) {
fireEvent.click(byUiId(`ui.projects.fieldTemplate.field.${id}.select`));
}
describe("PipelineFieldTemplatePage — type and options", () => {
it("offers the type picker for a custom field", () => {
renderPage();
selectField("cfg_custom");
expect(byUiId("ui.projects.fieldTemplate.inspector.typeEditor")).toBeTruthy();
expect(byUiId("ui.projects.fieldTemplate.inspector.type")).toBeTruthy();
});
it("keeps a system field's type read-only, because it has no definition to change", () => {
renderPage();
selectField("cfg_sys");
expect(byUiId("ui.projects.fieldTemplate.inspector.typeEditor")).toBeNull();
expect(screen.getAllByText("projects.fieldTemplate.type.text").length).toBeGreaterThan(0);
});
it("shows the stored options in their stored order", () => {
renderPage();
selectField("cfg_custom");
expect((byUiId("ui.projects.fieldTemplate.inspector.option.0.label") as HTMLInputElement).value).toBe("Offen");
expect((byUiId("ui.projects.fieldTemplate.inspector.option.1.label") as HTMLInputElement).value).toBe("Erledigt");
expect((byUiId("ui.projects.fieldTemplate.inspector.option.0.color") as HTMLInputElement).value).toBe("#ff0000");
});
it("saves reordered options with their labels and colours", () => {
const onSubmit = renderPage();
selectField("cfg_custom");
fireEvent.click(byUiId("ui.projects.fieldTemplate.inspector.option.1.up"));
fireEvent.click(byUiId("ui.projects.fieldTemplate.inspector.saveType"));
const [[intent, fields]] = onSubmit.mock.calls.slice(-1);
expect(intent).toBe("updateCustomField");
expect(fields.definitionId).toBe("def_1");
expect(JSON.parse(fields.optionen).map((option: { value: string }) => option.value)).toEqual(["erledigt", "offen"]);
});
it("adds an option whose value the server derives from the typed label", () => {
const onSubmit = renderPage();
selectField("cfg_custom");
fireEvent.click(byUiId("ui.projects.fieldTemplate.inspector.addOption"));
fireEvent.change(byUiId("ui.projects.fieldTemplate.inspector.option.2.label"), { target: { value: "Geplant" } });
fireEvent.click(byUiId("ui.projects.fieldTemplate.inspector.saveType"));
const [[, fields]] = onSubmit.mock.calls.slice(-1);
// The new entry travels with an empty value; the shared parser fills it from
// the label, so an existing option never has its value rewritten.
expect(JSON.parse(fields.optionen)[2]).toEqual({ value: "", label: "Geplant", color: null });
});
it("removes an option", () => {
const onSubmit = renderPage();
selectField("cfg_custom");
fireEvent.click(byUiId("ui.projects.fieldTemplate.inspector.option.0.remove"));
fireEvent.click(byUiId("ui.projects.fieldTemplate.inspector.saveType"));
const [[, fields]] = onSubmit.mock.calls.slice(-1);
expect(JSON.parse(fields.optionen).map((option: { value: string }) => option.value)).toEqual(["erledigt"]);
});
it("expresses Mehrfachauswahl as the multiselect type instead of a second flag", () => {
const onSubmit = renderPage();
selectField("cfg_custom");
fireEvent.click(byUiId("ui.projects.fieldTemplate.inspector.multiple"));
fireEvent.click(byUiId("ui.projects.fieldTemplate.inspector.saveType"));
const [[, fields]] = onSubmit.mock.calls.slice(-1);
expect(fields.datentyp).toBe("multiselect");
});
it("hides the options editor for a type that carries none", () => {
renderPage({
definitions: {
projektname: { label: "Projektname", datentyp: "text" },
status_aufmass: { id: "def_1", label: "Aufmaß-Status", datentyp: "zahl", optionen: null },
},
});
selectField("cfg_custom");
expect(byUiId("ui.projects.fieldTemplate.inspector.type")).toBeTruthy();
expect(byUiId("ui.projects.fieldTemplate.inspector.options")).toBeNull();
});
it("previews the template with its groups, order and required marker", () => {
renderPage();
const preview = byUiId("ui.projects.fieldTemplate.preview");
expect(preview.textContent).toContain("Grunddaten");
// Projektname is a required field, so the line carries the marker.
expect(byUiId("ui.projects.fieldTemplate.preview.field.cfg_sys").textContent).toContain("Projektname *");
expect(byUiId("ui.projects.fieldTemplate.preview.field.cfg_custom").textContent).toContain("Aufmaß-Status");
});
it("saves a renamed custom field together with its type", () => {
const onSubmit = renderPage();
selectField("cfg_custom");
fireEvent.change(byUiId("ui.projects.fieldTemplate.inspector.label"), { target: { value: "Aufmaß" } });
fireEvent.click(byUiId("ui.projects.fieldTemplate.inspector.saveType"));
const [[intent, fields]] = onSubmit.mock.calls.slice(-1);
expect(intent).toBe("updateCustomField");
expect(fields.label).toBe("Aufmaß");
});
it("keeps a system field's name read-only — there is no definition to rename", () => {
renderPage();
selectField("cfg_sys");
expect((byUiId("ui.projects.fieldTemplate.inspector.label") as HTMLInputElement).readOnly).toBe(true);
expect(byUiId("ui.projects.fieldTemplate.inspector.delete")).toBeNull();
});
it("names a system field that has no definition from its i18n key, not from its column key", () => {
// Without a definition the label used to fall through to "prioritaet".
renderPage({
fields: [
{ id: "cfg_prio", fieldKey: "prioritaet", groupId: "grp_base", sichtbar: true, pflicht: false, pflichtAbStageId: null, hinweis: null, istSystem: true, gesperrt: false, reihenfolge: 0 },
],
definitions: {},
});
expect(byUiId("ui.projects.fieldTemplate.field.cfg_prio").textContent).toContain("projects.fieldTemplate.systemField.prioritaet");
});
it("files a pool field into the group whose add row was used", () => {
const onSubmit = renderPage({
poolFields: [
{ id: "cfg_pool", fieldKey: "foerdernummer", groupId: null, sichtbar: true, pflicht: false, pflichtAbStageId: null, hinweis: null, istSystem: false, gesperrt: false, reihenfolge: 0 },
],
});
fireEvent.click(byUiId("ui.projects.fieldTemplate.group.grp_base.addExisting"));
// M05 picks first and confirms after — the dialog names the target group,
// so the pairing is meant to be checked before it happens.
fireEvent.click(byUiId("ui.projects.fieldTemplate.poolDialog.option.cfg_pool"));
fireEvent.click(byUiId("ui.projects.fieldTemplate.poolDialog.confirm"));
expect(onSubmit).toHaveBeenCalledWith("moveTemplateField", { fieldConfigId: "cfg_pool", groupId: "grp_base", reihenfolge: "0" });
});
it("lets a pick be taken back by clicking the same row again", () => {
renderPage({
poolFields: [
{ id: "cfg_pool", fieldKey: "foerdernummer", groupId: null, sichtbar: true, pflicht: false, pflichtAbStageId: null, hinweis: null, istSystem: false, gesperrt: false, reihenfolge: 0 },
],
});
fireEvent.click(byUiId("ui.projects.fieldTemplate.group.grp_base.addExisting"));
const row = byUiId("ui.projects.fieldTemplate.poolDialog.option.cfg_pool");
fireEvent.click(row);
expect(row.getAttribute("aria-pressed")).toBe("true");
fireEvent.click(row);
expect(row.getAttribute("aria-pressed")).toBe("false");
// Nothing picked means nothing to add.
expect((byUiId("ui.projects.fieldTemplate.poolDialog.confirm") as HTMLButtonElement).disabled).toBe(true);
});
it("leaves a hidden field out of the preview, like the detail page will", () => {
renderPage({
fields: [
{ id: "cfg_sys", fieldKey: "projektname", groupId: "grp_base", sichtbar: true, pflicht: true, pflichtAbStageId: null, hinweis: null, istSystem: true, gesperrt: false, reihenfolge: 0 },
{ id: "cfg_hidden", fieldKey: "status_aufmass", groupId: "grp_base", sichtbar: false, pflicht: false, pflichtAbStageId: null, hinweis: null, istSystem: false, gesperrt: false, reihenfolge: 1 },
],
});
expect(byUiId("ui.projects.fieldTemplate.preview.field.cfg_sys")).toBeTruthy();
expect(byUiId("ui.projects.fieldTemplate.preview.field.cfg_hidden")).toBeNull();
});
});