test(01-02): add failing tests for item, category, and totals services
- Item CRUD tests: create, getAll, getById, update, delete - Category CRUD tests: create, getAll, update, delete with reassignment - Totals tests: per-category and global aggregates Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
127
tests/services/item.service.test.ts
Normal file
127
tests/services/item.service.test.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
import { describe, it, expect, beforeEach } from "bun:test";
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
import {
|
||||
getAllItems,
|
||||
getItemById,
|
||||
createItem,
|
||||
updateItem,
|
||||
deleteItem,
|
||||
} from "../../src/server/services/item.service.ts";
|
||||
|
||||
describe("Item Service", () => {
|
||||
let db: ReturnType<typeof createTestDb>;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createTestDb();
|
||||
});
|
||||
|
||||
describe("createItem", () => {
|
||||
it("creates item with all fields, returns item with id and timestamps", () => {
|
||||
const item = createItem(
|
||||
db,
|
||||
{
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
priceCents: 35000,
|
||||
categoryId: 1,
|
||||
notes: "Ultralight 2-person",
|
||||
productUrl: "https://example.com/tent",
|
||||
},
|
||||
);
|
||||
|
||||
expect(item).toBeDefined();
|
||||
expect(item!.id).toBeGreaterThan(0);
|
||||
expect(item!.name).toBe("Tent");
|
||||
expect(item!.weightGrams).toBe(1200);
|
||||
expect(item!.priceCents).toBe(35000);
|
||||
expect(item!.categoryId).toBe(1);
|
||||
expect(item!.notes).toBe("Ultralight 2-person");
|
||||
expect(item!.productUrl).toBe("https://example.com/tent");
|
||||
expect(item!.createdAt).toBeDefined();
|
||||
expect(item!.updatedAt).toBeDefined();
|
||||
});
|
||||
|
||||
it("only name and categoryId are required, other fields optional", () => {
|
||||
const item = createItem(db, { name: "Spork", categoryId: 1 });
|
||||
|
||||
expect(item).toBeDefined();
|
||||
expect(item!.name).toBe("Spork");
|
||||
expect(item!.weightGrams).toBeNull();
|
||||
expect(item!.priceCents).toBeNull();
|
||||
expect(item!.notes).toBeNull();
|
||||
expect(item!.productUrl).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAllItems", () => {
|
||||
it("returns all items with category info joined", () => {
|
||||
createItem(db, { name: "Tent", categoryId: 1 });
|
||||
createItem(db, { name: "Sleeping Bag", categoryId: 1 });
|
||||
|
||||
const all = getAllItems(db);
|
||||
expect(all).toHaveLength(2);
|
||||
expect(all[0].categoryName).toBe("Uncategorized");
|
||||
expect(all[0].categoryEmoji).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("getItemById", () => {
|
||||
it("returns single item or null", () => {
|
||||
const created = createItem(db, { name: "Tent", categoryId: 1 });
|
||||
const found = getItemById(db, created!.id);
|
||||
expect(found).toBeDefined();
|
||||
expect(found!.name).toBe("Tent");
|
||||
|
||||
const notFound = getItemById(db, 9999);
|
||||
expect(notFound).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateItem", () => {
|
||||
it("updates specified fields, sets updatedAt", () => {
|
||||
const created = createItem(db, {
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const updated = updateItem(db, created!.id, {
|
||||
name: "Big Agnes Tent",
|
||||
weightGrams: 1100,
|
||||
});
|
||||
|
||||
expect(updated).toBeDefined();
|
||||
expect(updated!.name).toBe("Big Agnes Tent");
|
||||
expect(updated!.weightGrams).toBe(1100);
|
||||
});
|
||||
|
||||
it("returns null for non-existent id", () => {
|
||||
const result = updateItem(db, 9999, { name: "Ghost" });
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteItem", () => {
|
||||
it("removes item from DB, returns deleted item", () => {
|
||||
const created = createItem(db, {
|
||||
name: "Tent",
|
||||
categoryId: 1,
|
||||
imageFilename: "tent.jpg",
|
||||
});
|
||||
|
||||
const deleted = deleteItem(db, created!.id);
|
||||
expect(deleted).toBeDefined();
|
||||
expect(deleted!.name).toBe("Tent");
|
||||
expect(deleted!.imageFilename).toBe("tent.jpg");
|
||||
|
||||
// Verify it's gone
|
||||
const found = getItemById(db, created!.id);
|
||||
expect(found).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null for non-existent id", () => {
|
||||
const result = deleteItem(db, 9999);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user