test: 添加完整测试套件(52 个文件,326 个用例)
基于 Vitest 搭建测试基础设施,覆盖后端纯函数、API 路由、 前端 hooks、UI 组件和页面级集成测试。
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
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 { GET } from "./route";
|
||||
|
||||
const mockCtx = { params: Promise.resolve({}) };
|
||||
|
||||
beforeEach(() => {
|
||||
resetPrismaMock();
|
||||
});
|
||||
|
||||
describe("GET /api/blindbox/rooms", () => {
|
||||
it("returns user rooms list", async () => {
|
||||
prismaMock.blindBoxMember.findMany.mockResolvedValue([
|
||||
{
|
||||
room: {
|
||||
id: "bb-room-1",
|
||||
code: "ABC123",
|
||||
name: "周末",
|
||||
creatorId: "user-1",
|
||||
_count: { members: 2, ideas: 5 },
|
||||
members: [
|
||||
{ user: { id: "user-1", username: "test", avatar: "🐱" } },
|
||||
],
|
||||
ideas: [{ content: "去公园", createdAt: new Date() }],
|
||||
},
|
||||
joinedAt: new Date(),
|
||||
},
|
||||
] as never);
|
||||
|
||||
prismaMock.blindBoxIdea.groupBy.mockResolvedValue([
|
||||
{ roomId: "bb-room-1", _count: 3 },
|
||||
] as never);
|
||||
|
||||
const req = createRequest("/api/blindbox/rooms?userId=user-1");
|
||||
const res = await GET(req, mockCtx);
|
||||
const { status, data } = await parseJsonResponse(res);
|
||||
|
||||
expect(status).toBe(200);
|
||||
expect(data.rooms).toHaveLength(1);
|
||||
expect(data.rooms[0].code).toBe("ABC123");
|
||||
expect(data.rooms[0].poolCount).toBe(3);
|
||||
});
|
||||
|
||||
it("returns 401 when no userId", async () => {
|
||||
const req = createRequest("/api/blindbox/rooms");
|
||||
const res = await GET(req, mockCtx);
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user