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
This commit is contained in:
2026-04-06 07:56:32 +02:00
parent e59e724d84
commit 6f07e874f9
2 changed files with 87 additions and 0 deletions

53
tests/routes/tags.test.ts Normal file
View File

@@ -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<ReturnType<typeof createTestDb>>["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");
});
});
});