kanban-dev-webapp/src/lib/data/board.test.ts

60 lines
2.4 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import { loadProjectSummaries } from "@/lib/data/board";
import { getServerPb } from "@/lib/pb/server";
vi.mock("@/lib/pb/server", () => ({ getServerPb: vi.fn() }));
describe("home project summaries", () => {
beforeEach(() => vi.clearAllMocks());
it("counts open and inbox cards across projects with one read per collection", async () => {
const projects = [
{ id: "p1", client: "c1", name: "Alpha" },
{ id: "p2", client: "c1", name: "Beta" },
];
const clients = [{ id: "c1", name: "Client" }];
const columns = [
{ id: "i1", project: "p1", is_inbox: true, is_done: false },
{ id: "d1", project: "p1", is_inbox: false, is_done: true },
{ id: "i2", project: "p2", is_inbox: true, is_done: false },
];
const cards = [
{ project: "p1", column: "i1" },
{ project: "p1", column: "d1" },
{ project: "p2", column: "i2" },
{ project: "p2", column: "i2" },
];
const getFullList = vi.fn(async (name: string) => ({ projects, clients, columns, cards })[name as "projects" | "clients" | "columns" | "cards"]);
const getFirstListItem = vi.fn(async (filter: string) => {
const projectId = filter.match(/project = "([^"]+)"/)?.[1];
const column = columns.find((item) => item.project === projectId && (filter.includes("is_inbox") ? item.is_inbox : item.is_done));
if (!column) throw new Error("No matching column");
return column;
});
const getList = vi.fn(async (_page: number, _perPage: number, options: { filter: string }) => ({
totalItems: options.filter.includes('column = "i1"') ? 1
: options.filter.includes('column = "i2"') ? 2
: options.filter.includes('project = "p1"') ? 1 : 2,
}));
const pb = {
authStore: { isValid: true },
collection: vi.fn((name: string) => ({
getFullList: () => getFullList(name),
getFirstListItem,
getList,
})),
};
vi.mocked(getServerPb).mockResolvedValue(pb as never);
const summaries = await loadProjectSummaries();
expect(summaries.map((summary) => ({ id: summary.project.id, open: summary.openCards, inbox: summary.inboxCards }))).toEqual([
{ id: "p1", open: 1, inbox: 1 },
{ id: "p2", open: 2, inbox: 2 },
]);
expect(getFullList).toHaveBeenCalledTimes(4);
expect(getFirstListItem).not.toHaveBeenCalled();
expect(getList).not.toHaveBeenCalled();
});
});