128 lines
4.4 KiB
TypeScript
128 lines
4.4 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
buildTaskDetailVM,
|
|
buildTaskListVM,
|
|
taskEntityLabelKey,
|
|
taskPriorityKey,
|
|
taskSourceKey,
|
|
taskStatusKey,
|
|
toTaskListModel,
|
|
type BuildTaskDetailVMInput,
|
|
type TaskListFilterState,
|
|
type TaskListRow,
|
|
} from "../index.js";
|
|
|
|
const I18N_KEY = /^[a-z][a-z0-9]*(?:[.-][a-z0-9]+)*$/;
|
|
|
|
const filter: TaskListFilterState = {
|
|
preset: "all",
|
|
status: null,
|
|
priority: null,
|
|
assignedToUserId: null,
|
|
includeArchived: false,
|
|
};
|
|
|
|
const row: TaskListRow = {
|
|
id: "t1",
|
|
title: "Angebot nachfassen",
|
|
status: "open",
|
|
statusKey: taskStatusKey("open"),
|
|
priority: "high",
|
|
priorityKey: taskPriorityKey("high"),
|
|
deadlineIso: "2026-06-25T08:00:00.000Z",
|
|
isOverdue: false,
|
|
assignee: { kind: "user", label: "Akeshan" },
|
|
linkedEntity: { entityType: "deal", entityId: "d9", labelKey: taskEntityLabelKey("deal") },
|
|
source: "manual",
|
|
sourceKey: taskSourceKey("manual"),
|
|
sourceHref: null,
|
|
detailHref: "/acme/tasks/t1",
|
|
};
|
|
|
|
describe("buildTaskListVM", () => {
|
|
it("returns a ready empty-state with i18n keys and no cursor when there are no rows", () => {
|
|
const vm = buildTaskListVM({ rows: [], filter, canWrite: true, canDelete: false, createHref: "/acme/tasks/new", nextCursor: "x" });
|
|
expect(vm.status).toBe("ready");
|
|
if (vm.status !== "ready") throw new Error("expected ready");
|
|
expect(vm.data.rows).toHaveLength(0);
|
|
expect(vm.data.nextCursor).toBeNull();
|
|
expect(vm.data.emptyHeadlineKey).toMatch(I18N_KEY);
|
|
expect(vm.data.emptyBodyKey).toMatch(I18N_KEY);
|
|
expect(vm.data.createHref).toBe("/acme/tasks/new");
|
|
});
|
|
|
|
it("passes rows + embed context + cursor through; hrefs stay internal", () => {
|
|
const vm = buildTaskListVM({
|
|
rows: [row],
|
|
filter: { ...filter, preset: "mine" },
|
|
canWrite: true,
|
|
canDelete: true,
|
|
createHref: "/acme/tasks/new?deal=d9",
|
|
embed: { linkedEntityType: "deal", linkedEntityId: "d9" },
|
|
nextCursor: "cur2",
|
|
});
|
|
if (vm.status !== "ready") throw new Error("expected ready");
|
|
expect(vm.data.rows[0]?.detailHref.startsWith("http")).toBe(false);
|
|
expect(vm.data.embed?.linkedEntityId).toBe("d9");
|
|
expect(vm.data.nextCursor).toBe("cur2");
|
|
expect(vm.data.filter.preset).toBe("mine");
|
|
});
|
|
});
|
|
|
|
describe("toTaskListModel", () => {
|
|
it("flattens rows to the hot-swap model (assignee label, status key)", () => {
|
|
const vm = buildTaskListVM({ rows: [row], filter, canWrite: false, canDelete: false, createHref: "/acme/tasks/new" });
|
|
if (vm.status !== "ready") throw new Error("expected ready");
|
|
const model = toTaskListModel(vm.data);
|
|
expect(model.rowCount).toBe(1);
|
|
expect(model.titleKey).toMatch(I18N_KEY);
|
|
expect(model.rows[0]?.assigneeLabel).toBe("Akeshan");
|
|
expect(model.rows[0]?.statusKey).toBe("tasks.status.open");
|
|
});
|
|
});
|
|
|
|
describe("buildTaskDetailVM", () => {
|
|
const base: BuildTaskDetailVMInput = {
|
|
task: {
|
|
id: "t1",
|
|
title: "Angebot nachfassen",
|
|
description: "**Markdown** Beschreibung",
|
|
status: "open",
|
|
priority: "normal",
|
|
deadlineIso: null,
|
|
isOverdue: false,
|
|
assignee: { kind: "group", label: "Vertrieb" },
|
|
linkedEntity: { entityType: "contact", entityId: "c1", labelKey: "" },
|
|
source: "chat",
|
|
sourceHref: "/acme/inbox/msg-7",
|
|
watchers: [{ userId: "u2", label: "Raffael" }],
|
|
createdAtIso: "2026-06-20T08:00:00.000Z",
|
|
completedAtIso: null,
|
|
},
|
|
assignableUsers: [{ id: "u1", label: "Akeshan" }],
|
|
assignableGroups: [{ id: "g1", label: "Vertrieb" }],
|
|
canWrite: true,
|
|
canDelete: false,
|
|
listHref: "/acme/tasks",
|
|
};
|
|
|
|
it("derives i18n keys for status/priority/source/entity and keeps the source link", () => {
|
|
const vm = buildTaskDetailVM(base);
|
|
if (vm.status !== "ready") throw new Error("expected ready");
|
|
expect(vm.data.statusKey).toBe("tasks.status.open");
|
|
expect(vm.data.priorityKey).toBe("tasks.priority.normal");
|
|
expect(vm.data.sourceKey).toBe("tasks.source.chat");
|
|
expect(vm.data.linkedEntity?.labelKey).toBe("tasks.entity.contact");
|
|
expect(vm.data.sourceHref).toBe("/acme/inbox/msg-7");
|
|
expect(vm.data.watchers[0]?.label).toBe("Raffael");
|
|
});
|
|
|
|
it("hides picker option lists from non-writers", () => {
|
|
const vm = buildTaskDetailVM({ ...base, canWrite: false });
|
|
if (vm.status !== "ready") throw new Error("expected ready");
|
|
expect(vm.data.assignableUsers).toHaveLength(0);
|
|
expect(vm.data.assignableGroups).toHaveLength(0);
|
|
});
|
|
});
|