86 lines
2.9 KiB
TypeScript
86 lines
2.9 KiB
TypeScript
// Unit tests for the pure SortableKanbanBoard move reducer. No React / dnd-kit.
|
|
|
|
import { describe, expect, it } from "vitest";
|
|
import { columnDroppableId, computeKanbanMove, locateCard } from "./kanbanMove";
|
|
|
|
interface Card {
|
|
readonly id: string;
|
|
}
|
|
|
|
interface Col {
|
|
readonly id: string;
|
|
readonly title: string; // extra field: must be preserved across moves
|
|
readonly cards: readonly Card[];
|
|
}
|
|
|
|
function board(): Col[] {
|
|
return [
|
|
{ id: "todo", title: "To do", cards: [{ id: "a" }, { id: "b" }, { id: "c" }] },
|
|
{ id: "doing", title: "Doing", cards: [{ id: "d" }] },
|
|
{ id: "done", title: "Done", cards: [] },
|
|
];
|
|
}
|
|
|
|
const ids = (cols: Col[]) => cols.map((c) => `${c.id}:${c.cards.map((x) => x.id).join(",")}`).join("|");
|
|
|
|
describe("locateCard", () => {
|
|
it("finds the column and index of a card", () => {
|
|
expect(locateCard(board(), "b")).toEqual({ columnId: "todo", index: 1 });
|
|
expect(locateCard(board(), "d")).toEqual({ columnId: "doing", index: 0 });
|
|
});
|
|
|
|
it("returns null for an unknown card", () => {
|
|
expect(locateCard(board(), "zzz")).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("computeKanbanMove", () => {
|
|
it("moves a card into another column over a target card and flags crossColumn", () => {
|
|
const result = computeKanbanMove(board(), "a", "d");
|
|
expect(result).not.toBeNull();
|
|
expect(result!.crossColumn).toBe(true);
|
|
expect(result!.cardId).toBe("a");
|
|
expect(result!.to).toEqual({ columnId: "doing", index: 0 });
|
|
expect(ids(result!.columns)).toBe("todo:b,c|doing:a,d|done:");
|
|
});
|
|
|
|
it("appends to an empty column when dropped on the column droppable sentinel", () => {
|
|
const result = computeKanbanMove(board(), "b", columnDroppableId("done"));
|
|
expect(result).not.toBeNull();
|
|
expect(result!.crossColumn).toBe(true);
|
|
expect(result!.to).toEqual({ columnId: "done", index: 0 });
|
|
expect(ids(result!.columns)).toBe("todo:a,c|doing:d|done:b");
|
|
});
|
|
|
|
it("preserves extra column fields (title) when moving", () => {
|
|
const result = computeKanbanMove(board(), "a", "d");
|
|
expect(result!.columns.map((c) => c.title)).toEqual(["To do", "Doing", "Done"]);
|
|
});
|
|
|
|
it("reorders within a column (arrayMove semantics) without crossColumn", () => {
|
|
const result = computeKanbanMove(board(), "a", "c");
|
|
expect(result).not.toBeNull();
|
|
expect(result!.crossColumn).toBe(false);
|
|
expect(ids(result!.columns)).toBe("todo:b,c,a|doing:d|done:");
|
|
});
|
|
|
|
it("returns null when dropped on itself", () => {
|
|
expect(computeKanbanMove(board(), "a", "a")).toBeNull();
|
|
});
|
|
|
|
it("returns null for an unknown active card", () => {
|
|
expect(computeKanbanMove(board(), "zzz", "d")).toBeNull();
|
|
});
|
|
|
|
it("returns null for an unknown drop target", () => {
|
|
expect(computeKanbanMove(board(), "a", "nope")).toBeNull();
|
|
});
|
|
|
|
it("does not mutate the input columns", () => {
|
|
const input = board();
|
|
const before = ids(input);
|
|
computeKanbanMove(input, "a", "d");
|
|
expect(ids(input)).toBe(before);
|
|
});
|
|
});
|