test: 添加完整测试套件(52 个文件,326 个用例)
基于 Vitest 搭建测试基础设施,覆盖后端纯函数、API 路由、 前端 hooks、UI 组件和页面级集成测试。
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { prismaMock, resetPrismaMock } from "@/__tests__/helpers/prisma-mock";
|
||||
import { createRequest, createRouteContext, parseJsonResponse } from "@/__tests__/helpers/api-test-utils";
|
||||
import { TEST_BLINDBOX_ROOM, TEST_USER } from "@/__tests__/helpers/fixtures";
|
||||
|
||||
vi.mock("@/lib/blindbox", () => ({
|
||||
getRoomByCode: vi.fn(),
|
||||
requireMembership: vi.fn().mockResolvedValue({}),
|
||||
}));
|
||||
|
||||
import { GET, PATCH, DELETE } from "./route";
|
||||
import { getRoomByCode } from "@/lib/blindbox";
|
||||
|
||||
const mockGetRoomByCode = vi.mocked(getRoomByCode);
|
||||
|
||||
beforeEach(() => {
|
||||
resetPrismaMock();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("GET /api/blindbox/room/[code]", () => {
|
||||
it("returns room data", async () => {
|
||||
mockGetRoomByCode.mockResolvedValue({
|
||||
...TEST_BLINDBOX_ROOM,
|
||||
_count: { ideas: 3 },
|
||||
members: [
|
||||
{ user: { id: "user-1", username: "test", avatar: "🐱" }, joinedAt: new Date() },
|
||||
],
|
||||
} as never);
|
||||
|
||||
const req = createRequest("/api/blindbox/room/ABC123");
|
||||
const ctx = createRouteContext({ code: "ABC123" });
|
||||
const res = await GET(req, ctx);
|
||||
const { status, data } = await parseJsonResponse(res);
|
||||
|
||||
expect(status).toBe(200);
|
||||
expect(data.code).toBe("ABC123");
|
||||
expect(data.poolCount).toBe(3);
|
||||
expect(data.members).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("returns 404 for nonexistent room", async () => {
|
||||
mockGetRoomByCode.mockResolvedValue(null);
|
||||
|
||||
const req = createRequest("/api/blindbox/room/BADCODE");
|
||||
const ctx = createRouteContext({ code: "BADCODE" });
|
||||
const res = await GET(req, ctx);
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PATCH /api/blindbox/room/[code]", () => {
|
||||
it("updates room location", async () => {
|
||||
prismaMock.blindBoxRoom.findUnique.mockResolvedValue(TEST_BLINDBOX_ROOM as never);
|
||||
prismaMock.blindBoxRoom.update.mockResolvedValue({
|
||||
...TEST_BLINDBOX_ROOM,
|
||||
city: "上海",
|
||||
lat: 31.2,
|
||||
lng: 121.4,
|
||||
} as never);
|
||||
|
||||
const req = createRequest("/api/blindbox/room/ABC123", {
|
||||
method: "PATCH",
|
||||
body: { userId: "user-1", city: "上海", lat: 31.2, lng: 121.4 },
|
||||
});
|
||||
const ctx = createRouteContext({ code: "ABC123" });
|
||||
const res = await PATCH(req, ctx);
|
||||
const { status, data } = await parseJsonResponse(res);
|
||||
|
||||
expect(status).toBe(200);
|
||||
expect(data.city).toBe("上海");
|
||||
});
|
||||
|
||||
it("returns 400 for invalid coordinates", async () => {
|
||||
prismaMock.blindBoxRoom.findUnique.mockResolvedValue(TEST_BLINDBOX_ROOM as never);
|
||||
|
||||
const req = createRequest("/api/blindbox/room/ABC123", {
|
||||
method: "PATCH",
|
||||
body: { userId: "user-1", lat: 999, lng: 121.4 },
|
||||
});
|
||||
const ctx = createRouteContext({ code: "ABC123" });
|
||||
const res = await PATCH(req, ctx);
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE /api/blindbox/room/[code]", () => {
|
||||
it("deletes room when creator", async () => {
|
||||
prismaMock.blindBoxRoom.findUnique.mockResolvedValue(TEST_BLINDBOX_ROOM as never);
|
||||
prismaMock.blindBoxRoom.delete.mockResolvedValue({} as never);
|
||||
|
||||
const req = createRequest("/api/blindbox/room/ABC123", {
|
||||
method: "DELETE",
|
||||
body: { userId: "user-1" },
|
||||
});
|
||||
const ctx = createRouteContext({ code: "ABC123" });
|
||||
const res = await DELETE(req, ctx);
|
||||
const { data } = await parseJsonResponse(res);
|
||||
|
||||
expect(data.action).toBe("deleted");
|
||||
});
|
||||
|
||||
it("leaves room when not creator", async () => {
|
||||
prismaMock.blindBoxRoom.findUnique.mockResolvedValue(TEST_BLINDBOX_ROOM as never);
|
||||
prismaMock.blindBoxMember.findUnique.mockResolvedValue({ id: "member-2" } as never);
|
||||
prismaMock.blindBoxMember.delete.mockResolvedValue({} as never);
|
||||
|
||||
const req = createRequest("/api/blindbox/room/ABC123", {
|
||||
method: "DELETE",
|
||||
body: { userId: "user-2" },
|
||||
});
|
||||
const ctx = createRouteContext({ code: "ABC123" });
|
||||
const res = await DELETE(req, ctx);
|
||||
const { data } = await parseJsonResponse(res);
|
||||
|
||||
expect(data.action).toBe("left");
|
||||
});
|
||||
|
||||
it("returns 403 when not a member and not creator", async () => {
|
||||
prismaMock.blindBoxRoom.findUnique.mockResolvedValue(TEST_BLINDBOX_ROOM as never);
|
||||
prismaMock.blindBoxMember.findUnique.mockResolvedValue(null as never);
|
||||
|
||||
const req = createRequest("/api/blindbox/room/ABC123", {
|
||||
method: "DELETE",
|
||||
body: { userId: "stranger" },
|
||||
});
|
||||
const ctx = createRouteContext({ code: "ABC123" });
|
||||
const res = await DELETE(req, ctx);
|
||||
expect(res.status).toBe(403);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user