Files
GearBox/tests/services/item.service.test.ts
Jean-Luc Makiola b496462df5
All checks were successful
CI / ci (push) Successful in 15s
chore: auto-fix Biome formatting and configure lint rules
Run biome check --write --unsafe to fix tabs, import ordering, and
non-null assertions across entire codebase. Disable a11y rules not
applicable to this single-user app. Exclude auto-generated routeTree.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-15 19:51:34 +01:00

125 lines
3.3 KiB
TypeScript

import { beforeEach, describe, expect, it } from "bun:test";
import {
createItem,
deleteItem,
getAllItems,
getItemById,
updateItem,
} from "../../src/server/services/item.service.ts";
import { createTestDb } from "../helpers/db.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].categoryIcon).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();
});
});
});