feat(plan): add plan category service with tests
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
86
src/__tests__/planCategory.test.ts
Normal file
86
src/__tests__/planCategory.test.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { describe, it, expect, afterAll } from "vitest";
|
||||
import prisma from "../config/database";
|
||||
import {
|
||||
listPlanCategories,
|
||||
createPlanCategory,
|
||||
updatePlanCategory,
|
||||
deactivatePlanCategory,
|
||||
isCategoryKeyActive,
|
||||
slugifyCategory,
|
||||
} from "../services/planCategory.service";
|
||||
|
||||
const created: number[] = [];
|
||||
|
||||
afterAll(async () => {
|
||||
if (created.length) {
|
||||
await prisma.plan_categories.deleteMany({ where: { id: { in: created } } });
|
||||
}
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
|
||||
describe("slugifyCategory", () => {
|
||||
it("strips Czech diacritics and non-alphanumerics", () => {
|
||||
expect(slugifyCategory("Cesta / Montáž")).toBe("cesta_montaz");
|
||||
expect(slugifyCategory("Příprava")).toBe("priprava");
|
||||
});
|
||||
});
|
||||
|
||||
describe("plan category service", () => {
|
||||
it("seeds are present and ordered", async () => {
|
||||
const cats = await listPlanCategories(true);
|
||||
const keys = cats.map((c) => c.key);
|
||||
expect(keys).toContain("work");
|
||||
expect(keys).toContain("other");
|
||||
});
|
||||
|
||||
it("creates a category with a unique slug and next sort_order", async () => {
|
||||
const res = await createPlanCategory({
|
||||
label: "Test Kategorie ZZ",
|
||||
color: "#123456",
|
||||
});
|
||||
if ("error" in res) throw new Error(res.error);
|
||||
created.push(res.data.id);
|
||||
expect(res.data.key).toBe("test_kategorie_zz");
|
||||
expect(res.data.color).toBe("#123456");
|
||||
expect(res.data.is_active).toBe(true);
|
||||
expect(await isCategoryKeyActive("test_kategorie_zz")).toBe(true);
|
||||
});
|
||||
|
||||
it("dedupes a colliding slug", async () => {
|
||||
const res = await createPlanCategory({
|
||||
label: "Test Kategorie ZZ",
|
||||
color: "#222222",
|
||||
});
|
||||
if ("error" in res) throw new Error(res.error);
|
||||
created.push(res.data.id);
|
||||
expect(res.data.key).toBe("test_kategorie_zz_2");
|
||||
});
|
||||
|
||||
it("updates label and color (key immutable)", async () => {
|
||||
const res = await createPlanCategory({
|
||||
label: "Test Upd ZZ",
|
||||
color: "#aaaaaa",
|
||||
});
|
||||
if ("error" in res) throw new Error(res.error);
|
||||
created.push(res.data.id);
|
||||
const upd = await updatePlanCategory(res.data.id, {
|
||||
label: "Test Upd2 ZZ",
|
||||
color: "#bbbbbb",
|
||||
});
|
||||
if ("error" in upd) throw new Error(upd.error);
|
||||
expect(upd.data.label).toBe("Test Upd2 ZZ");
|
||||
expect(upd.data.color).toBe("#bbbbbb");
|
||||
expect(upd.data.key).toBe("test_upd_zz");
|
||||
});
|
||||
|
||||
it("deactivate hides the key from active check", async () => {
|
||||
const res = await createPlanCategory({
|
||||
label: "Test Deact ZZ",
|
||||
color: "#cccccc",
|
||||
});
|
||||
if ("error" in res) throw new Error(res.error);
|
||||
created.push(res.data.id);
|
||||
await deactivatePlanCategory(res.data.id);
|
||||
expect(await isCategoryKeyActive("test_deact_zz")).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user