145 lines
6.2 KiB
TypeScript
145 lines
6.2 KiB
TypeScript
import { vi, describe, it, expect } from "vitest";
|
|
import "@testing-library/jest-dom";
|
|
import { render } from "@testing-library/react";
|
|
import { BrowserRouter as Router } from "react-router";
|
|
import { buildTaskDetailVM, type BuildTaskDetailVMInput, type TaskDetailData, type TaskDetailSource } from "@hivegrid/view-models";
|
|
|
|
import { TaskDetailPage } from "./TaskDetailPage";
|
|
|
|
vi.mock("react-i18next", () => ({
|
|
useTranslation: () => ({ t: (str: string) => str, i18n: { language: "de" } }),
|
|
}));
|
|
|
|
function source(overrides: Partial<TaskDetailSource> = {}): TaskDetailSource {
|
|
return {
|
|
id: "t1",
|
|
title: "Angebot nachfassen",
|
|
description: "Kunde bis Freitag anrufen",
|
|
status: "open",
|
|
priority: "high",
|
|
deadlineIso: "2026-02-01T10:00:00.000Z",
|
|
isOverdue: false,
|
|
assignee: { kind: "user", label: "Alex Meier" },
|
|
linkedEntity: { entityType: "contact", entityId: "c9", labelKey: "tasks.entity.contact" },
|
|
source: "manual",
|
|
sourceHref: null,
|
|
watchers: [{ userId: "u2", label: "Sam Fischer" }],
|
|
createdAtIso: "2026-01-02T10:00:00.000Z",
|
|
completedAtIso: null,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function data(input: Partial<BuildTaskDetailVMInput> = {}): TaskDetailData {
|
|
const vm = buildTaskDetailVM({
|
|
task: source(),
|
|
assignableUsers: [{ id: "u1", label: "Alex Meier" }],
|
|
assignableGroups: [{ id: "g1", label: "Vertrieb" }],
|
|
canWrite: true,
|
|
canDelete: true,
|
|
listHref: "/acme/tasks",
|
|
...input,
|
|
});
|
|
if (vm.status !== "ready" || !vm.data) throw new Error("expected ready VM");
|
|
return vm.data;
|
|
}
|
|
|
|
describe("TaskDetailPage (interim-UI)", () => {
|
|
it("renders the detail surface + title + linked entity + edit form", () => {
|
|
const { container, getAllByText } = render(
|
|
<Router>
|
|
<TaskDetailPage data={data()} />
|
|
</Router>,
|
|
);
|
|
expect(container.querySelector('[data-ui-id="ui.tasks.detail"]')).toBeInTheDocument();
|
|
expect(container.querySelector('[data-ui-id="ui.tasks.detail.linkedEntity"]')).toBeInTheDocument();
|
|
expect(container.querySelector('[data-ui-id="ui.tasks.detail.edit"]')).toBeInTheDocument();
|
|
// Title appears in both the breadcrumb (active) and the header.
|
|
expect(getAllByText("Angebot nachfassen").length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it("renders the watcher list with a per-watcher marker", () => {
|
|
const { container, getByText } = render(
|
|
<Router>
|
|
<TaskDetailPage data={data()} />
|
|
</Router>,
|
|
);
|
|
expect(container.querySelector('[data-watcher-id="u2"]')).toBeInTheDocument();
|
|
expect(getByText("Sam Fischer")).toBeInTheDocument();
|
|
});
|
|
|
|
it("exposes complete + delete actions for a writable, non-done task", () => {
|
|
const { container } = render(
|
|
<Router>
|
|
<TaskDetailPage data={data()} />
|
|
</Router>,
|
|
);
|
|
expect(container.querySelector('[data-ui-id="ui.tasks.detail.complete"]')).toBeInTheDocument();
|
|
expect(container.querySelector('[data-ui-id="ui.tasks.detail.delete"]')).toBeInTheDocument();
|
|
});
|
|
|
|
it("saves the whole form from the single header button, in the agreed order", () => {
|
|
const { container } = render(
|
|
<Router>
|
|
<TaskDetailPage data={data()} />
|
|
</Router>,
|
|
);
|
|
const editForm = container.querySelector('[data-ui-id="ui.tasks.detail.edit"]') as HTMLFormElement;
|
|
const save = container.querySelector('[data-ui-id="ui.tasks.detail.save"]') as HTMLButtonElement;
|
|
// The save button sits in the header, outside the form it submits, and is
|
|
// wired to it by id — so assignment and watcher pick save together with the
|
|
// master data.
|
|
expect(save.getAttribute("type")).toBe("submit");
|
|
expect(save.getAttribute("form")).toBe(editForm.getAttribute("id"));
|
|
expect(editForm.querySelector('[name="assigneeId"]')).toBeInTheDocument();
|
|
expect(editForm.querySelector('[name="watcherUserId"]')).toBeInTheDocument();
|
|
// No second save/assign submit left at the bottom of the form. The only
|
|
// submits inside it are the watcher-remove buttons, which are explicitly
|
|
// re-associated to their own form.
|
|
const ownSubmits = [...editForm.querySelectorAll('button[type="submit"]')].filter((button) => !button.getAttribute("form"));
|
|
expect(ownSubmits).toHaveLength(0);
|
|
|
|
const order = [...container.querySelectorAll("[data-ui-id^='ui.tasks.detail.']")]
|
|
.map((element) => element.getAttribute("data-ui-id"))
|
|
.filter((id) => ["ui.tasks.detail.complete", "ui.tasks.detail.archive", "ui.tasks.detail.delete", "ui.tasks.detail.save"].includes(id ?? ""));
|
|
expect(order).toEqual(["ui.tasks.detail.complete", "ui.tasks.detail.archive", "ui.tasks.detail.delete", "ui.tasks.detail.save"]);
|
|
});
|
|
|
|
it("keeps the remove-watcher submit out of the edit form (no nested forms)", () => {
|
|
const { container } = render(
|
|
<Router>
|
|
<TaskDetailPage data={data()} />
|
|
</Router>,
|
|
);
|
|
const editForm = container.querySelector('[data-ui-id="ui.tasks.detail.edit"]') as HTMLFormElement;
|
|
expect(editForm.querySelector("form")).toBeNull();
|
|
const removeButton = container.querySelector('[data-watcher-id="u2"] button[type="submit"]') as HTMLButtonElement;
|
|
const removeFormId = removeButton.getAttribute("form");
|
|
expect(removeFormId).toBeTruthy();
|
|
const removeForm = container.querySelector(`#${removeFormId}`) as HTMLFormElement;
|
|
expect(removeForm).toBeInTheDocument();
|
|
expect(editForm.contains(removeForm)).toBe(false);
|
|
expect(removeForm.querySelector('[name="userId"]')).toHaveValue("u2");
|
|
});
|
|
|
|
it("has no back link — the shell already provides navigation", () => {
|
|
const { container } = render(
|
|
<Router>
|
|
<TaskDetailPage data={data()} />
|
|
</Router>,
|
|
);
|
|
expect(container.querySelector('[data-ui-id="ui.tasks.detail.back"]')).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("hides the edit form + action buttons for a read-only viewer", () => {
|
|
const { container } = render(
|
|
<Router>
|
|
<TaskDetailPage data={data({ canWrite: false, canDelete: false })} />
|
|
</Router>,
|
|
);
|
|
expect(container.querySelector('[data-ui-id="ui.tasks.detail.edit"]')).not.toBeInTheDocument();
|
|
expect(container.querySelector('[data-ui-id="ui.tasks.detail.complete"]')).not.toBeInTheDocument();
|
|
expect(container.querySelector('[data-ui-id="ui.tasks.detail.delete"]')).not.toBeInTheDocument();
|
|
});
|
|
});
|