183 lines
8.1 KiB
TypeScript
183 lines
8.1 KiB
TypeScript
// WF-P8 S04 — BoardSettingsPage unit test. Fixture via the real
|
|
// buildBoardSettingsVM on top of buildPipelineAdminVM; interactions asserted on
|
|
// the injected onSubmit. The column tests moved here from the old combined
|
|
// admin page, which no longer configures anything itself.
|
|
|
|
import { fireEvent, render, screen } from "@testing-library/react";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import { MemoryRouter } from "react-router";
|
|
import { buildBoardSettingsVM, buildPipelineAdminVM, type BoardSettingsData } from "@hivegrid/view-models";
|
|
import { BoardSettingsPage } from "./BoardSettingsPage";
|
|
|
|
vi.mock("react-i18next", () => ({
|
|
useTranslation: () => ({ t: (s: string) => s, i18n: { language: "de" } }),
|
|
}));
|
|
|
|
// Radix selects are not interactable in jsdom; swap ~/ui Select for a native one.
|
|
vi.mock("~/ui/components/Select", () => {
|
|
const Select = ({ value, onValueChange, label, triggerAriaLabel, children, ...rest }: any) => (
|
|
<div data-ui-id={rest["data-ui-id"]}>
|
|
<select
|
|
aria-label={typeof label === "string" ? label : (triggerAriaLabel ?? "select")}
|
|
value={value ?? ""}
|
|
onChange={(e: any) => onValueChange?.(e.target.value)}
|
|
data-ui-id={rest["data-ui-id"] ? `${rest["data-ui-id"]}.native` : undefined}
|
|
>
|
|
<option value="" />
|
|
{children}
|
|
</select>
|
|
</div>
|
|
);
|
|
Select.Item = ({ value, children }: any) => <option value={value}>{children}</option>;
|
|
return { Select };
|
|
});
|
|
|
|
const byUiId = (id: string) => document.querySelector(`[data-ui-id='${id}']`) as HTMLElement;
|
|
|
|
type FixturePipeline = Parameters<typeof buildPipelineAdminVM>[0]["pipelines"][number];
|
|
|
|
const board: FixturePipeline = {
|
|
id: "pl1",
|
|
name: "Sales",
|
|
typHint: "sales",
|
|
isDefault: true,
|
|
projectCount: 11,
|
|
updatedAtIso: "2026-07-12T06:12:00.000Z",
|
|
stages: [
|
|
{ id: "s1", name: "Neu", stageKind: "offen", winProbability: 10, color: "#000000", position: 10, projectCount: 4 },
|
|
{ id: "s2", name: "Gewonnen", stageKind: "gewonnen", winProbability: 100, color: "#00aa00", position: 20, projectCount: 5 },
|
|
{ id: "s3", name: "Verloren", stageKind: "verloren", winProbability: 0, color: "#aa0000", position: 30, projectCount: 2 },
|
|
],
|
|
accessRules: [],
|
|
fieldConfigs: [],
|
|
};
|
|
|
|
const otherBoard: FixturePipeline = {
|
|
id: "pl2",
|
|
name: "Wartung",
|
|
typHint: "auftrag",
|
|
isDefault: false,
|
|
projectCount: 3,
|
|
stages: [],
|
|
accessRules: [],
|
|
fieldConfigs: [],
|
|
};
|
|
|
|
function settingsData(boardId = "pl1"): BoardSettingsData {
|
|
const admin = buildPipelineAdminVM({ canAdmin: true, pipelines: [board, otherBoard] });
|
|
if (admin.status !== "ready") throw new Error("fixture not ready");
|
|
const vm = buildBoardSettingsVM({ boards: admin.data.pipelines, boardId, canAdmin: true });
|
|
if (vm.status !== "ready") throw new Error("board not found");
|
|
return vm.data;
|
|
}
|
|
|
|
function renderPage(data = settingsData()) {
|
|
const onSubmit = vi.fn();
|
|
render(
|
|
<MemoryRouter>
|
|
<BoardSettingsPage
|
|
data={data}
|
|
submitting={false}
|
|
fieldTemplateHref="/acme/admin/pipelines/pl1/fields"
|
|
actions={{ onSubmit }}
|
|
/>
|
|
</MemoryRouter>
|
|
);
|
|
return onSubmit;
|
|
}
|
|
|
|
describe("buildBoardSettingsVM", () => {
|
|
it("returns empty for an unknown board so the route can 404", () => {
|
|
const admin = buildPipelineAdminVM({ canAdmin: true, pipelines: [board] });
|
|
if (admin.status !== "ready") throw new Error("fixture not ready");
|
|
expect(buildBoardSettingsVM({ boards: admin.data.pipelines, boardId: "nope", canAdmin: true }).status).toBe("empty");
|
|
});
|
|
|
|
it("never offers the board itself as its own successor", () => {
|
|
const data = settingsData();
|
|
expect(data.successorOptions.map((option) => option.id)).toEqual(["pl2"]);
|
|
});
|
|
});
|
|
|
|
describe("BoardSettingsPage", () => {
|
|
it("renders the column table with per-column project counts", () => {
|
|
renderPage();
|
|
expect(byUiId("ui.projects.boardSettings.stageList.pl1")).toBeTruthy();
|
|
expect(byUiId("ui.projects.boardSettings.stage.s1.projects").textContent).toContain("projects.boardSettings.columns.projectCount");
|
|
expect(byUiId("ui.projects.boardSettings.stage.s1.drag")).toBeTruthy();
|
|
});
|
|
|
|
it("offers a save action only once a column actually changed", () => {
|
|
const onSubmit = renderPage();
|
|
expect(byUiId("ui.projects.boardSettings.stage.s1.save")).toBeNull();
|
|
fireEvent.change(byUiId("ui.projects.boardSettings.stage.s1.name"), { target: { value: "Eingang" } });
|
|
fireEvent.click(byUiId("ui.projects.boardSettings.stage.s1.save"));
|
|
expect(onSubmit).toHaveBeenCalledWith("upsertStage", expect.objectContaining({ pipelineId: "pl1", stageId: "s1", name: "Eingang" }));
|
|
});
|
|
|
|
it("archives a column", () => {
|
|
const onSubmit = renderPage();
|
|
fireEvent.click(byUiId("ui.projects.boardSettings.stage.s1.archive"));
|
|
expect(onSubmit).toHaveBeenCalledWith("archiveStage", { stageId: "s1" });
|
|
});
|
|
|
|
it("adds a column", () => {
|
|
const onSubmit = renderPage();
|
|
fireEvent.click(byUiId("ui.projects.boardSettings.addStage.pl1.open"));
|
|
fireEvent.change(byUiId("ui.projects.boardSettings.addStage.pl1.name"), { target: { value: "Angebot" } });
|
|
fireEvent.click(byUiId("ui.projects.boardSettings.addStage.pl1.submit"));
|
|
expect(onSubmit).toHaveBeenCalledWith("upsertStage", expect.objectContaining({ pipelineId: "pl1", name: "Angebot" }));
|
|
});
|
|
|
|
it("renames the board from the header", () => {
|
|
const onSubmit = renderPage();
|
|
fireEvent.click(byUiId("ui.projects.boardSettings.rename.open"));
|
|
fireEvent.change(byUiId("ui.projects.boardSettings.rename.input"), { target: { value: "Vertrieb" } });
|
|
fireEvent.click(byUiId("ui.projects.boardSettings.rename.save"));
|
|
expect(onSubmit).toHaveBeenCalledWith("updatePipeline", { pipelineId: "pl1", name: "Vertrieb" });
|
|
// Saving leaves the edit mode right away. Waiting for a changed loader
|
|
// value would keep the header in the input whenever the name stayed equal.
|
|
expect(byUiId("ui.projects.boardSettings.rename.input")).toBeNull();
|
|
});
|
|
|
|
it("closes the rename editor even when the name did not change", () => {
|
|
const onSubmit = renderPage();
|
|
fireEvent.click(byUiId("ui.projects.boardSettings.rename.open"));
|
|
fireEvent.click(byUiId("ui.projects.boardSettings.rename.save"));
|
|
expect(onSubmit).toHaveBeenCalledWith("updatePipeline", { pipelineId: "pl1", name: "Sales" });
|
|
expect(byUiId("ui.projects.boardSettings.rename.input")).toBeNull();
|
|
});
|
|
|
|
it("links to the S05 detail-field template", () => {
|
|
renderPage();
|
|
expect(byUiId("ui.projects.boardSettings.fieldTemplate").getAttribute("href")).toBe("/acme/admin/pipelines/pl1/fields");
|
|
});
|
|
|
|
it("shows the export's three columns and hides the win rate behind the pencil", () => {
|
|
const onSubmit = renderPage();
|
|
// Name / Status / Projekte, plus the leading drag+colour cell.
|
|
const headers = [...document.querySelectorAll("th")].map((th) => th.textContent?.trim());
|
|
expect(headers).toEqual(["", "projects.fields.name", "projects.boardSettings.columns.status", "projects.boardSettings.columns.projects"]);
|
|
|
|
expect(byUiId("ui.projects.boardSettings.stage.s1.winProbability")).toBeNull();
|
|
fireEvent.click(byUiId("ui.projects.boardSettings.stage.s1.edit"));
|
|
fireEvent.change(byUiId("ui.projects.boardSettings.stage.s1.winProbability"), { target: { value: "40" } });
|
|
fireEvent.click(byUiId("ui.projects.boardSettings.stage.s1.save"));
|
|
expect(onSubmit).toHaveBeenCalledWith("upsertStage", expect.objectContaining({ stageId: "s1", winProbability: "40" }));
|
|
});
|
|
|
|
it("has no back link and keeps the access rules behind one expander", () => {
|
|
renderPage();
|
|
expect(byUiId("ui.projects.boardSettings.back")).toBeNull();
|
|
// Raw principal-id assignment is gone; the rules are read-only here.
|
|
expect(document.querySelector("[data-ui-id^='ui.projects.boardSettings.addAccess']")).toBeNull();
|
|
expect(byUiId("ui.projects.boardSettings.access")).toBeTruthy();
|
|
});
|
|
|
|
it("warns when the board has no won and no lost column", () => {
|
|
renderPage(settingsData("pl2"));
|
|
expect(byUiId("ui.projects.boardSettings.invalid")).toBeTruthy();
|
|
expect(screen.getByText("projects.admin.invalidHint")).toBeTruthy();
|
|
});
|
|
});
|