test: 添加完整测试套件(52 个文件,326 个用例)
基于 Vitest 搭建测试基础设施,覆盖后端纯函数、API 路由、 前端 hooks、UI 组件和页面级集成测试。
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { prismaMock, resetPrismaMock } from "@/__tests__/helpers/prisma-mock";
|
||||
import { createRequest, parseJsonResponse } from "@/__tests__/helpers/api-test-utils";
|
||||
import { TEST_USER, TEST_BLINDBOX_ROOM } from "@/__tests__/helpers/fixtures";
|
||||
|
||||
vi.mock("@/lib/blindbox", () => ({
|
||||
generateUniqueRoomCode: vi.fn().mockResolvedValue("XYZ789"),
|
||||
}));
|
||||
|
||||
import { POST } from "./route";
|
||||
|
||||
const mockCtx = { params: Promise.resolve({}) };
|
||||
|
||||
beforeEach(() => {
|
||||
resetPrismaMock();
|
||||
});
|
||||
|
||||
describe("POST /api/blindbox/room", () => {
|
||||
it("creates a blindbox room", async () => {
|
||||
prismaMock.user.findUnique.mockResolvedValue(TEST_USER as never);
|
||||
prismaMock.blindBoxRoom.create.mockResolvedValue({
|
||||
...TEST_BLINDBOX_ROOM,
|
||||
code: "XYZ789",
|
||||
} as never);
|
||||
|
||||
const req = createRequest("/api/blindbox/room", {
|
||||
method: "POST",
|
||||
body: { userId: "user-1", name: "周末计划" },
|
||||
});
|
||||
const res = await POST(req, mockCtx);
|
||||
const { status, data } = await parseJsonResponse(res);
|
||||
|
||||
expect(status).toBe(201);
|
||||
expect(data.code).toBe("XYZ789");
|
||||
});
|
||||
|
||||
it("uses default room name when not provided", async () => {
|
||||
prismaMock.user.findUnique.mockResolvedValue(TEST_USER as never);
|
||||
prismaMock.blindBoxRoom.create.mockResolvedValue(TEST_BLINDBOX_ROOM as never);
|
||||
|
||||
const req = createRequest("/api/blindbox/room", {
|
||||
method: "POST",
|
||||
body: { userId: "user-1" },
|
||||
});
|
||||
const res = await POST(req, mockCtx);
|
||||
expect(res.status).toBe(201);
|
||||
});
|
||||
|
||||
it("returns 401 when no userId", async () => {
|
||||
const req = createRequest("/api/blindbox/room", {
|
||||
method: "POST",
|
||||
body: { name: "test" },
|
||||
});
|
||||
const res = await POST(req, mockCtx);
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
|
||||
it("returns 400 when room name too long", async () => {
|
||||
const req = createRequest("/api/blindbox/room", {
|
||||
method: "POST",
|
||||
body: { userId: "user-1", name: "a".repeat(31) },
|
||||
});
|
||||
const res = await POST(req, mockCtx);
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user