39
apps/web/src/lib/validation/schemas.test.ts
Executable file
39
apps/web/src/lib/validation/schemas.test.ts
Executable file
@@ -0,0 +1,39 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { ValidationError, mapErrorToResponse } from "@/lib/errors";
|
||||
import { teacherCreateSchema, normalizeTeacherCreateBody } from "@/lib/validation/schemas";
|
||||
|
||||
describe("teacherCreateSchema", () => {
|
||||
it("requires topic", () => {
|
||||
const result = teacherCreateSchema.safeParse({});
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
|
||||
it("accepts valid topic", () => {
|
||||
const result = teacherCreateSchema.safeParse({ topic: "Rust ownership" });
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it("accepts null explorationId", () => {
|
||||
const result = teacherCreateSchema.safeParse({
|
||||
topic: "Rust ownership",
|
||||
explorationId: null,
|
||||
});
|
||||
expect(result.success).toBe(true);
|
||||
if (result.success) {
|
||||
expect(normalizeTeacherCreateBody(result.data).explorationId).toBeUndefined();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("mapErrorToResponse", () => {
|
||||
it("maps ValidationError to 400", () => {
|
||||
const mapped = mapErrorToResponse(new ValidationError("bad input"));
|
||||
expect(mapped.status).toBe(400);
|
||||
expect(mapped.message).toBe("bad input");
|
||||
});
|
||||
|
||||
it("maps Unauthorized to 401", () => {
|
||||
const mapped = mapErrorToResponse(new Error("Unauthorized"));
|
||||
expect(mapped.status).toBe(401);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user