Adds POST /api/items/:id/duplicate endpoint, useDuplicateItem hook, and a Duplicate button on ItemCard (collection view only) that opens the new item for editing immediately after creation. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
161 lines
4.3 KiB
TypeScript
161 lines
4.3 KiB
TypeScript
import { beforeEach, describe, expect, it } from "bun:test";
|
|
import {
|
|
createItem,
|
|
deleteItem,
|
|
duplicateItem,
|
|
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("duplicateItem", () => {
|
|
it("creates a copy with '(copy)' suffix in name", () => {
|
|
const original = createItem(db, {
|
|
name: "Tent",
|
|
weightGrams: 1200,
|
|
priceCents: 35000,
|
|
categoryId: 1,
|
|
notes: "Ultralight",
|
|
productUrl: "https://example.com/tent",
|
|
});
|
|
|
|
const copy = duplicateItem(db, original?.id);
|
|
|
|
expect(copy).toBeDefined();
|
|
expect(copy?.name).toBe("Tent (copy)");
|
|
expect(copy?.weightGrams).toBe(1200);
|
|
expect(copy?.priceCents).toBe(35000);
|
|
expect(copy?.categoryId).toBe(1);
|
|
expect(copy?.notes).toBe("Ultralight");
|
|
expect(copy?.productUrl).toBe("https://example.com/tent");
|
|
});
|
|
|
|
it("copy has a different ID from the original", () => {
|
|
const original = createItem(db, { name: "Helmet", categoryId: 1 });
|
|
const copy = duplicateItem(db, original?.id);
|
|
|
|
expect(copy?.id).not.toBe(original?.id);
|
|
});
|
|
|
|
it("returns null for non-existent item", () => {
|
|
const result = duplicateItem(db, 9999);
|
|
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();
|
|
});
|
|
});
|
|
});
|