Files
kandev/apps/web/components/kanban/virtualized-column-task-list.test.tsx
T
2026-09-17 14:39:05 +01:00

274 lines
9.4 KiB
TypeScript

import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { Task } from "../kanban-card";
const droppableState = vi.hoisted(() => ({ isOverId: null as string | null }));
// AC.7's DroppableTaskRow calls dnd-kit's real useDroppable, which needs a
// live DndContext to resolve isOver during a pointer drag. Mocking it lets
// the render test below drive isOver directly, isolating "does the indicator
// actually render" from dnd-kit's own collision-detection machinery (already
// exercised by e2e).
vi.mock("@dnd-kit/core", () => ({
useDroppable: ({ id }: { id: string }) => ({
setNodeRef: () => {},
isOver: id === droppableState.isOverId,
}),
}));
import {
computeInsertionEdge,
computeKeyboardInsertionEdge,
DroppableTaskRow,
findTaskIndex,
getCompactTaskPrefixHeight,
type KeyboardReorderDraft,
} from "./virtualized-column-task-list";
function fakeTask(id: string): Task {
return { id, workflowStepId: "step-1", title: id, position: 0 } as Task;
}
describe("computeInsertionEdge", () => {
const queuedStartIndex = 2; // "a","b" admitted; "c","d" queued
it("returns null when nothing is being dragged", () => {
expect(computeInsertionEdge(queuedStartIndex, null, 1)).toBeNull();
});
it("returns null for the dragged card's own row", () => {
expect(computeInsertionEdge(queuedStartIndex, 1, 1)).toBeNull();
});
it("returns 'bottom' when the hovered row is later than the dragged row in the same band", () => {
expect(computeInsertionEdge(queuedStartIndex, 0, 1)).toBe("bottom");
});
it("returns 'top' when the hovered row is earlier than the dragged row in the same band", () => {
expect(computeInsertionEdge(queuedStartIndex, 1, 0)).toBe("top");
});
it("returns null across a band boundary (AC.11 cross-band reject shows no indicator)", () => {
expect(computeInsertionEdge(queuedStartIndex, 0, 3)).toBeNull();
});
});
describe("findTaskIndex", () => {
const orderedTasks = [fakeTask("a"), fakeTask("b"), fakeTask("c")];
it("returns the task's index", () => {
expect(findTaskIndex(orderedTasks, "b")).toBe(1);
});
it("returns null for a missing or absent id", () => {
expect(findTaskIndex(orderedTasks, "missing")).toBeNull();
expect(findTaskIndex(orderedTasks, null)).toBeNull();
expect(findTaskIndex(orderedTasks, undefined)).toBeNull();
});
});
describe("getCompactTaskPrefixHeight", () => {
it("uses measured rows when available and estimates the rest of the six-row prefix", () => {
const taskIds = ["a", "b", "c", "d", "e", "f", "g"];
const estimateSize = (index: number) => 10 + index;
expect(
getCompactTaskPrefixHeight(
taskIds,
[
{ index: 0, key: "a", size: 111 },
{ index: 3, key: "d", size: 222 },
{ index: 6, key: "g", size: 900 },
],
estimateSize,
),
).toBe(385);
});
it("keeps the queued divider estimate in its logical row and excludes later tasks", () => {
const taskIds = ["a", "b", "c", "queued", "e", "f", "g"];
const estimateSize = (index: number) => (index === 3 ? 136 : 96);
expect(getCompactTaskPrefixHeight(taskIds, [], estimateSize)).toBe(616);
});
it("ignores sparse virtualizer measurement holes", () => {
const measurements = [] as Array<{ index: number; key: string; size: number }>;
measurements[0] = { index: 0, key: "a", size: 111 };
measurements[2] = { index: 2, key: "c", size: 333 };
expect(getCompactTaskPrefixHeight(["a", "b", "c"], measurements, () => 96)).toBe(540);
});
});
describe("computeKeyboardInsertionEdge", () => {
function draft(overrides: Partial<KeyboardReorderDraft> = {}): KeyboardReorderDraft {
return {
taskId: "a",
stepId: "step-1",
band: "admitted",
order: ["a", "b", "c", "d"],
...overrides,
};
}
it("returns null when there is no draft", () => {
expect(computeKeyboardInsertionEdge(null, "step-1", "b")).toBeNull();
});
it("returns null when the draft belongs to a different step", () => {
expect(computeKeyboardInsertionEdge(draft(), "step-2", "b")).toBeNull();
});
it("returns null for a row not present in the draft's band order", () => {
expect(computeKeyboardInsertionEdge(draft(), "step-1", "queued-task")).toBeNull();
});
it("returns null for the dragged card's own row", () => {
expect(computeKeyboardInsertionEdge(draft(), "step-1", "a")).toBeNull();
});
it("returns 'bottom' on the row immediately before the draft's position", () => {
expect(
computeKeyboardInsertionEdge(draft({ order: ["b", "a", "c", "d"] }), "step-1", "b"),
).toBe("bottom");
});
it("returns 'top' on the row immediately after the draft's position", () => {
expect(
computeKeyboardInsertionEdge(draft({ order: ["b", "a", "c", "d"] }), "step-1", "c"),
).toBe("top");
});
it("tracks the draft's neighbors by identity after multiple moves, not by original DOM index", () => {
// "a" starts at index 0 ([a,b,c,d]) and is moved down twice, landing
// between "c" and "d" ([b,c,a,d]). The DOM never re-renders (only the
// draft evolves), so this proves the indicator follows "a"'s actual
// neighbors rather than whatever row sits at a numerically-adjacent DOM
// index (a regression that would flag "b" - "a"'s original DOM
// neighbor, not its current one).
const moved = draft({ order: ["b", "c", "a", "d"] });
expect(computeKeyboardInsertionEdge(moved, "step-1", "b")).toBeNull();
expect(computeKeyboardInsertionEdge(moved, "step-1", "c")).toBe("bottom");
expect(computeKeyboardInsertionEdge(moved, "step-1", "d")).toBe("top");
});
});
// AC.7: "while a card is dragged over its own band, show which edge of the
// hovered card the drop would insert next to". The suites above only cover
// the math (computeInsertionEdge / computeKeyboardInsertionEdge); nothing
// asserted the indicator itself ever reaches the DOM. These tests render
// DroppableTaskRow directly (bypassing the virtualizer and the full
// KanbanCard tree, neither of which this indicator's rendering depends on)
// and check the actual border classes and data-testid it produces.
const INDICATOR_BOTTOM_TESTID = "kanban-insertion-indicator-bottom";
const INDICATOR_TOP_TESTID = "kanban-insertion-indicator-top";
describe("DroppableTaskRow (AC.7 insertion-point indicator)", () => {
afterEach(() => {
cleanup();
droppableState.isOverId = null;
});
it("renders the bottom-edge indicator while the row is a pointer drag's drop target", () => {
droppableState.isOverId = "row-b";
render(
<DroppableTaskRow
taskId="row-b"
index={0}
top={0}
measureElement={() => {}}
insertionEdge="bottom"
forceShowIndicator={false}
>
<div>card</div>
</DroppableTaskRow>,
);
const indicator = screen.getByTestId(INDICATOR_BOTTOM_TESTID);
expect(indicator.className).toContain("border-b-2");
expect(indicator.className).toContain("border-primary");
expect(indicator.className).not.toContain("border-t-2");
});
it("renders the top-edge indicator while the row is a pointer drag's drop target", () => {
droppableState.isOverId = "row-b";
render(
<DroppableTaskRow
taskId="row-b"
index={0}
top={0}
measureElement={() => {}}
insertionEdge="top"
forceShowIndicator={false}
>
<div>card</div>
</DroppableTaskRow>,
);
const indicator = screen.getByTestId(INDICATOR_TOP_TESTID);
expect(indicator.className).toContain("border-t-2");
expect(indicator.className).toContain("border-primary");
expect(indicator.className).not.toContain("border-b-2");
});
it("renders the indicator for a keyboard reorder's adjacent row even though isOver is false", () => {
// forceShowIndicator is how the keyboard path (no pointer, so dnd-kit
// never reports isOver) drives the same visual indicator as a pointer
// drag (AC.12).
droppableState.isOverId = null;
render(
<DroppableTaskRow
taskId="row-b"
index={0}
top={0}
measureElement={() => {}}
insertionEdge="bottom"
forceShowIndicator={true}
>
<div>card</div>
</DroppableTaskRow>,
);
expect(screen.getByTestId(INDICATOR_BOTTOM_TESTID)).toBeTruthy();
});
it("renders no indicator when the row is not a drop target and no keyboard draft targets it", () => {
droppableState.isOverId = null;
render(
<DroppableTaskRow
taskId="row-b"
index={0}
top={0}
measureElement={() => {}}
insertionEdge="bottom"
forceShowIndicator={false}
>
<div>card</div>
</DroppableTaskRow>,
);
expect(screen.queryByTestId(INDICATOR_BOTTOM_TESTID)).toBeNull();
expect(screen.queryByTestId(INDICATOR_TOP_TESTID)).toBeNull();
});
it("renders no indicator when isOver is true but insertionEdge is null (AC.11 cross-band reject)", () => {
droppableState.isOverId = "row-b";
render(
<DroppableTaskRow
taskId="row-b"
index={0}
top={0}
measureElement={() => {}}
insertionEdge={null}
forceShowIndicator={false}
>
<div>card</div>
</DroppableTaskRow>,
);
expect(screen.queryByTestId(INDICATOR_BOTTOM_TESTID)).toBeNull();
expect(screen.queryByTestId(INDICATOR_TOP_TESTID)).toBeNull();
});
});