Every package now has tests — no more empty test suites: - map-core: 33 tests — maxspeedColor, routeGradeColor, elevationColor, POI categories, getCategoriesForProfile, tile layer configs - db: 5 tests — withDb error handling (503 on DB error, re-throws Response/DataWithResponseInit, logs errors) - ui: 14 tests — Button variants/sizes/disabled, Input label/id/error, Card className forwarding - map: 2 tests — baseLayers/overlayLayers re-export verification Remove passWithNoTests from vitest.shared.ts so missing tests are caught immediately when adding new packages. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
import { describe, it, expect, vi } from "vitest";
|
|
import { withDb } from "./index.ts";
|
|
|
|
describe("withDb", () => {
|
|
it("returns the handler result on success", async () => {
|
|
const result = await withDb(async () => "ok");
|
|
expect(result).toBe("ok");
|
|
});
|
|
|
|
it("throws 503 Response on database errors", async () => {
|
|
try {
|
|
await withDb(async () => {
|
|
throw new Error("connection refused");
|
|
});
|
|
expect.fail("should have thrown");
|
|
} catch (err) {
|
|
expect(err).toBeInstanceOf(Response);
|
|
expect((err as Response).status).toBe(503);
|
|
}
|
|
});
|
|
|
|
it("re-throws Response instances (redirects)", async () => {
|
|
const redirect = new Response(null, { status: 302 });
|
|
try {
|
|
await withDb(async () => {
|
|
throw redirect;
|
|
});
|
|
expect.fail("should have thrown");
|
|
} catch (err) {
|
|
expect(err).toBe(redirect);
|
|
expect((err as Response).status).toBe(302);
|
|
}
|
|
});
|
|
|
|
it("re-throws DataWithResponseInit errors", async () => {
|
|
const dataError = { type: "DataWithResponseInit", data: "test", init: {} };
|
|
try {
|
|
await withDb(async () => {
|
|
throw dataError;
|
|
});
|
|
expect.fail("should have thrown");
|
|
} catch (err) {
|
|
expect(err).toBe(dataError);
|
|
}
|
|
});
|
|
|
|
it("logs database errors", async () => {
|
|
const spy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
try {
|
|
await withDb(async () => {
|
|
throw new Error("timeout");
|
|
});
|
|
} catch {
|
|
// expected
|
|
}
|
|
expect(spy).toHaveBeenCalledWith("[withDb] Database error:", "timeout");
|
|
spy.mockRestore();
|
|
});
|
|
});
|