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

187 lines
10 KiB
TypeScript

// WF-P8 Subframe delta — BoardsStartPage unit test. Fixture via the real
// buildBoardsStartVM; assertions cover rendering, admin gating, the
// client-side search/type filter and the locked-board rows (greyed out,
// request-access action / pending badge, no links). i18n mocked.
import { fireEvent, render, screen, within } from "@testing-library/react";
import { describe, expect, it } from "vitest";
import { vi } from "vitest";
import { buildBoardsStartVM, type BoardsStartData } from "@hivegrid/view-models";
import { BoardsStartPage, filterBoardRows } from "./BoardsStartPage";
vi.mock("react-i18next", () => ({
useTranslation: () => ({ t: (s: string) => s, i18n: { language: "de" } }),
}));
// Radix dropdowns neither open nor provide their menu context in jsdom, so the
// facet options would be unreachable. Both the SubframeCore wrapper and the
// ~/ui menu are swapped for passthroughs; the entries stay real buttons.
vi.mock("@subframe/core", async () => {
const actual = await vi.importActual<Record<string, unknown>>("@subframe/core");
const Pass = ({ children }: { children?: React.ReactNode }) => <>{children}</>;
return { ...actual, DropdownMenu: { Root: Pass, Trigger: Pass, Portal: Pass, Content: Pass, Item: Pass } };
});
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 byUiId = (id: string) => document.querySelector(`[data-ui-id='${id}']`) as HTMLElement;
const noopActions = { onRequestAccess: () => undefined };
function boardsData(
overrides: { canAdmin?: boolean; empty?: boolean; locked?: boolean; requestPending?: boolean } = {}
): BoardsStartData {
const vm = buildBoardsStartVM({
pipelines: overrides.empty
? []
: [
{ id: "pl1", name: "Vertrieb", typHint: "sales", isDefault: true, stageCount: 5, projectCount: 12 },
{ id: "pl2", name: "Montage", typHint: "auftrag", isDefault: false, stageCount: 4, projectCount: 3 },
],
lockedPipelines: overrides.locked
? [
{
id: "pl3",
name: "Geschaeftsleitung",
typHint: "custom",
isDefault: false,
stageCount: 3,
projectCount: 9,
requestPending: overrides.requestPending ?? false,
},
]
: [],
perms: { canAdmin: overrides.canAdmin ?? true },
buildOpenHref: (id) => `/acme/projects?pipelineId=${id}`,
buildSettingsHref: (id) => `/acme/admin/pipelines/${id}`,
adminHref: "/acme/admin/pipelines",
lostReasonsHref: "/acme/admin/pipelines/lost-reasons",
});
if (vm.status !== "ready") throw new Error("fixture not ready");
return vm.data;
}
describe("BoardsStartPage", () => {
it("renders the boards with counts, open links and status", () => {
render(<BoardsStartPage data={boardsData()} actions={noopActions} />);
expect(screen.getByText("Vertrieb")).toBeTruthy();
expect(screen.getByText("Montage")).toBeTruthy();
expect(byUiId("ui.projects.boards.open.pl1").getAttribute("href")).toBe("/acme/projects?pipelineId=pl1");
expect(byUiId("ui.projects.boards.table")).toBeTruthy();
expect(byUiId("ui.projects.boards.count").textContent).toContain("projects.boards.countLine");
// Scoped to the table: the facet menus carry the same option captions.
const table = within(byUiId("ui.projects.boards.table"));
expect(table.getAllByText("projects.boards.status.usable")).toHaveLength(2);
// New S00 columns: type tag, "N Spalten", access state and the footer line.
expect(table.getAllByText("projects.typHint.sales")).toHaveLength(1);
expect(table.getAllByText("projects.boards.stagesValue")).toHaveLength(2);
expect(table.getAllByText("projects.boards.access.granted")).toHaveLength(2);
expect(byUiId("ui.projects.boards.footer").textContent).toContain("projects.boards.footerLine");
});
it("filters by access and by status", () => {
const rows = boardsData({ locked: true }).rows;
const base = { search: "", typ: new Set<string>(), access: new Set<string>(), status: new Set<string>() };
const names = (result: readonly { name: string }[]) => result.map((row) => row.name);
expect(names(filterBoardRows(rows, { ...base, access: new Set(["requestable"]) }))).toEqual(["Geschaeftsleitung"]);
expect(names(filterBoardRows(rows, { ...base, access: new Set(["granted"]) }))).toEqual(["Vertrieb", "Montage"]);
expect(names(filterBoardRows(rows, { ...base, status: new Set(["active"]) }))).toEqual(["Vertrieb", "Montage"]);
expect(names(filterBoardRows(rows, { ...base, status: new Set(["locked"]) }))).toEqual(["Geschaeftsleitung"]);
expect(names(filterBoardRows(rows, { ...base, typ: new Set(["auftrag"]) }))).toEqual(["Montage"]);
// Several values inside one facet are OR-ed, not AND-ed.
expect(names(filterBoardRows(rows, { ...base, typ: new Set(["sales", "auftrag"]) }))).toEqual(["Vertrieb", "Montage"]);
// Facets combine rather than override each other.
expect(filterBoardRows(rows, { ...base, typ: new Set(["custom"]), status: new Set(["active"]) })).toEqual([]);
// An empty facet is not a filter.
expect(names(filterBoardRows(rows, base))).toHaveLength(rows.length);
});
it("renders all three facet controls", () => {
render(<BoardsStartPage data={boardsData()} actions={noopActions} />);
expect(byUiId("ui.projects.boards.filter.typ")).toBeTruthy();
expect(byUiId("ui.projects.boards.filter.access")).toBeTruthy();
expect(byUiId("ui.projects.boards.filter.status")).toBeTruthy();
});
it("filters on a facet click and takes the filter back off via its chip", () => {
render(<BoardsStartPage data={boardsData()} actions={noopActions} />);
fireEvent.click(byUiId("ui.projects.boards.filter.typ.auftrag"));
expect(within(byUiId("ui.projects.boards.table")).queryByText("Vertrieb")).toBeNull();
// Every active facet leaves a chip, so there is always a way back.
fireEvent.click(byUiId("ui.projects.boards.filter.chip.typ.auftrag.remove"));
expect(within(byUiId("ui.projects.boards.table")).getByText("Vertrieb")).toBeTruthy();
});
it("shows admin actions only for projects.admin viewers", () => {
const { unmount } = render(<BoardsStartPage data={boardsData({ canAdmin: true })} actions={noopActions} />);
// Davids S00 (Subframe 2026-07-28) has all three header actions back, plus
// the per-row gear that deep-links into that board's settings card (S04).
expect(byUiId("ui.projects.boards.newBoard").getAttribute("href")).toBe("/acme/admin/pipelines");
expect(byUiId("ui.projects.boards.adminSettings").getAttribute("href")).toBe("/acme/admin/pipelines");
expect(byUiId("ui.projects.boards.lostReasons").getAttribute("href")).toBe("/acme/admin/pipelines/lost-reasons");
expect(byUiId("ui.projects.boards.settings.pl1").getAttribute("href")).toBe("/acme/admin/pipelines/pl1");
unmount();
render(<BoardsStartPage data={boardsData({ canAdmin: false })} actions={noopActions} />);
expect(byUiId("ui.projects.boards.newBoard")).toBeNull();
expect(byUiId("ui.projects.boards.adminSettings")).toBeNull();
expect(byUiId("ui.projects.boards.lostReasons")).toBeNull();
expect(byUiId("ui.projects.boards.settings.pl1")).toBeNull();
});
it("renders locked boards greyed out without links and requests access on click", () => {
const onRequestAccess = vi.fn();
render(<BoardsStartPage data={boardsData({ locked: true })} actions={{ onRequestAccess }} />);
// Name is plain text (no open link), lock status + Klartext hint shown.
expect(byUiId("ui.projects.boards.open.pl3")).toBeNull();
expect(byUiId("ui.projects.boards.lockedName.pl3").textContent).toBe("Geschaeftsleitung");
// The gear survives on a locked board: configuring it is an admin
// capability, separate from being allowed to work on it.
expect(byUiId("ui.projects.boards.settings.pl3").getAttribute("href")).toBe("/acme/admin/pipelines/pl3");
const table = within(byUiId("ui.projects.boards.table"));
expect(table.getByText("projects.boards.status.locked")).toBeTruthy();
expect(table.getByText("projects.boards.locked.noPermission")).toBeTruthy();
// Counts line switches to the variant including the locked part.
expect(byUiId("ui.projects.boards.count").textContent).toContain("projects.boards.countLineWithLocked");
// Request-access button posts via the injected callback.
fireEvent.click(byUiId("ui.projects.boards.requestAccess.pl3"));
expect(onRequestAccess).toHaveBeenCalledWith("pl3");
});
it("shows a pending badge instead of a dead button once a request is open", () => {
render(<BoardsStartPage data={boardsData({ locked: true, requestPending: true })} actions={noopActions} />);
expect(byUiId("ui.projects.boards.requestAccess.pl3")).toBeNull();
expect(byUiId("ui.projects.boards.requestPending.pl3").textContent).toContain("projects.boards.locked.requestPending");
});
it("filters boards client-side by search text", () => {
render(<BoardsStartPage data={boardsData()} actions={noopActions} />);
const input = byUiId("ui.projects.boards.filter.search") as HTMLInputElement;
fireEvent.change(input, { target: { value: "mont" } });
expect(screen.queryByText("Vertrieb")).toBeNull();
expect(screen.getByText("Montage")).toBeTruthy();
fireEvent.change(input, { target: { value: "gibtsnicht" } });
expect(byUiId("ui.projects.boards.noMatches")).toBeTruthy();
});
it("renders a klartext empty state when the tenant has no boards", () => {
render(<BoardsStartPage data={boardsData({ empty: true })} actions={noopActions} />);
expect(byUiId("ui.projects.boards.empty")).toBeTruthy();
expect(screen.getByText("projects.boards.emptyTitle")).toBeTruthy();
});
});