From 6f07e874f9953b19caf6c91e782fec2b42518617 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Mon, 6 Apr 2026 07:56:32 +0200 Subject: [PATCH] test(20-01): add failing tests for tag service and route - Tag service tests: empty array, alphabetical ordering, id+name projection - Tag route tests: GET /api/tags returns 200, correct tag objects --- tests/routes/tags.test.ts | 53 ++++++++++++++++++++++++++++++ tests/services/tag.service.test.ts | 34 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 tests/routes/tags.test.ts create mode 100644 tests/services/tag.service.test.ts diff --git a/tests/routes/tags.test.ts b/tests/routes/tags.test.ts new file mode 100644 index 0000000..feab38d --- /dev/null +++ b/tests/routes/tags.test.ts @@ -0,0 +1,53 @@ +import { beforeEach, describe, expect, it } from "bun:test"; +import { Hono } from "hono"; +import { tags } from "../../src/db/schema.ts"; +import { tagRoutes } from "../../src/server/routes/tags.ts"; +import { createTestDb } from "../helpers/db.ts"; + +function createTestApp(db: any) { + const app = new Hono(); + + app.use("*", async (c, next) => { + c.set("db", db); + await next(); + }); + + app.route("/api/tags", tagRoutes); + return app; +} + +describe("Tag Routes", () => { + let app: Hono; + let db: Awaited>["db"]; + + beforeEach(async () => { + const testDb = await createTestDb(); + db = testDb.db; + app = createTestApp(db); + }); + + describe("GET /api/tags", () => { + it("returns 200 with empty array when no tags", async () => { + const res = await app.request("/api/tags"); + expect(res.status).toBe(200); + + const body = await res.json(); + expect(body).toEqual([]); + }); + + it("returns 200 with tag objects after seeding", async () => { + await db.insert(tags).values([ + { name: "bikepacking" }, + { name: "ultralight" }, + ]); + + const res = await app.request("/api/tags"); + expect(res.status).toBe(200); + + const body = await res.json(); + expect(body).toHaveLength(2); + expect(body[0]).toHaveProperty("id"); + expect(body[0]).toHaveProperty("name"); + }); + }); +}); diff --git a/tests/services/tag.service.test.ts b/tests/services/tag.service.test.ts new file mode 100644 index 0000000..0682c14 --- /dev/null +++ b/tests/services/tag.service.test.ts @@ -0,0 +1,34 @@ +import { beforeEach, describe, expect, it } from "bun:test"; +import { tags } from "../../src/db/schema.ts"; +import { getAllTags } from "../../src/server/services/tag.service.ts"; +import { createTestDb } from "../helpers/db.ts"; + +describe("Tag Service", () => { + let db: Awaited>["db"]; + + beforeEach(async () => { + const testDb = await createTestDb(); + db = testDb.db; + }); + + it("returns empty array when no tags exist", async () => { + const result = await getAllTags(db); + expect(result).toEqual([]); + }); + + it("returns all tags as { id, name } ordered alphabetically", async () => { + await db.insert(tags).values([ + { name: "bikepacking" }, + { name: "ultralight" }, + { name: "accessories" }, + ]); + + const result = await getAllTags(db); + expect(result).toHaveLength(3); + expect(result[0].name).toBe("accessories"); + expect(result[1].name).toBe("bikepacking"); + expect(result[2].name).toBe("ultralight"); + // Should NOT include createdAt + expect(result[0]).toEqual({ id: expect.any(Number), name: "accessories" }); + }); +});