修复 TypeScript 基线并补齐测试类型
This commit is contained in:
@@ -9,10 +9,12 @@ export function createRequest(
|
||||
} = {},
|
||||
): NextRequest {
|
||||
const { method = "GET", body, headers = {} } = options;
|
||||
const init: RequestInit = { method, headers };
|
||||
type NextRequestInit = ConstructorParameters<typeof NextRequest>[1];
|
||||
const requestHeaders: Record<string, string> = { ...headers };
|
||||
const init: NextRequestInit = { method, headers: requestHeaders };
|
||||
if (body !== undefined) {
|
||||
init.body = JSON.stringify(body);
|
||||
(init.headers as Record<string, string>)["content-type"] = "application/json";
|
||||
requestHeaders["content-type"] = "application/json";
|
||||
}
|
||||
return new NextRequest(new URL(url, "http://localhost:3721"), init);
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ beforeEach(() => {
|
||||
|
||||
describe("POST /api/blindbox/draw", () => {
|
||||
it("draws a random idea", async () => {
|
||||
prismaMock.$transaction.mockImplementation(async (fn: (tx: unknown) => Promise<unknown>) => {
|
||||
prismaMock.$transaction.mockImplementation((async (fn: unknown) => {
|
||||
const tx = {
|
||||
blindBoxIdea: {
|
||||
findMany: vi.fn().mockResolvedValue([{ id: "idea-1" }]),
|
||||
@@ -34,8 +34,8 @@ describe("POST /api/blindbox/draw", () => {
|
||||
}),
|
||||
},
|
||||
};
|
||||
return fn(tx);
|
||||
});
|
||||
return (fn as (txArg: typeof tx) => Promise<unknown>)(tx);
|
||||
}) as never);
|
||||
|
||||
const req = createRequest("/api/blindbox/draw", {
|
||||
method: "POST",
|
||||
@@ -52,7 +52,7 @@ describe("POST /api/blindbox/draw", () => {
|
||||
});
|
||||
|
||||
it("returns 404 when pool is empty", async () => {
|
||||
prismaMock.$transaction.mockImplementation(async (fn: (tx: unknown) => Promise<unknown>) => {
|
||||
prismaMock.$transaction.mockImplementation((async (fn: unknown) => {
|
||||
const tx = {
|
||||
blindBoxIdea: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
@@ -60,8 +60,8 @@ describe("POST /api/blindbox/draw", () => {
|
||||
findUnique: vi.fn(),
|
||||
},
|
||||
};
|
||||
return fn(tx);
|
||||
});
|
||||
return (fn as (txArg: typeof tx) => Promise<unknown>)(tx);
|
||||
}) as never);
|
||||
|
||||
const req = createRequest("/api/blindbox/draw", {
|
||||
method: "POST",
|
||||
@@ -72,7 +72,7 @@ describe("POST /api/blindbox/draw", () => {
|
||||
});
|
||||
|
||||
it("returns 409 on race condition (count=0)", async () => {
|
||||
prismaMock.$transaction.mockImplementation(async (fn: (tx: unknown) => Promise<unknown>) => {
|
||||
prismaMock.$transaction.mockImplementation((async (fn: unknown) => {
|
||||
const tx = {
|
||||
blindBoxIdea: {
|
||||
findMany: vi.fn().mockResolvedValue([{ id: "idea-1" }]),
|
||||
@@ -80,8 +80,8 @@ describe("POST /api/blindbox/draw", () => {
|
||||
findUnique: vi.fn(),
|
||||
},
|
||||
};
|
||||
return fn(tx);
|
||||
});
|
||||
return (fn as (txArg: typeof tx) => Promise<unknown>)(tx);
|
||||
}) as never);
|
||||
|
||||
const req = createRequest("/api/blindbox/draw", {
|
||||
method: "POST",
|
||||
|
||||
@@ -45,8 +45,8 @@ describe("POST /api/blindbox/plan/stream", () => {
|
||||
onProgress?.("正在生成行程...");
|
||||
return {
|
||||
id: "plan-1",
|
||||
days: [{ date: "周六", items: [] }],
|
||||
createdAt: new Date(),
|
||||
days: [{ date: "周六", items: [], summary: "轻松逛吃" }],
|
||||
createdAt: "2025-03-01T09:00:00.000Z",
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ import { getAuthUserId } from "@/lib/auth";
|
||||
const mockSuggestAlternativeItems = vi.mocked(suggestAlternativeItems);
|
||||
const mockSearchPois = vi.mocked(searchPois);
|
||||
const mockGetAuthUserId = vi.mocked(getAuthUserId);
|
||||
const mockCtx = { params: Promise.resolve({}) };
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
@@ -35,7 +36,7 @@ describe("POST /api/blindbox/plan/suggest-item", () => {
|
||||
method: "POST",
|
||||
body: { activity: "看展" },
|
||||
});
|
||||
const res = await POST(req);
|
||||
const res = await POST(req, mockCtx);
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
|
||||
@@ -65,7 +66,7 @@ describe("POST /api/blindbox/plan/suggest-item", () => {
|
||||
location: "121.47,31.23",
|
||||
},
|
||||
});
|
||||
const res = await POST(req);
|
||||
const res = await POST(req, mockCtx);
|
||||
const { status, data } = await parseJsonResponse(res);
|
||||
|
||||
expect(status).toBe(200);
|
||||
|
||||
@@ -36,9 +36,9 @@ describe("GET /api/blindbox/rooms", () => {
|
||||
},
|
||||
] as never);
|
||||
|
||||
prismaMock.blindBoxIdea.groupBy.mockResolvedValue([
|
||||
prismaMock.blindBoxIdea.groupBy = vi.fn().mockResolvedValue([
|
||||
{ roomId: "bb-room-1", _count: 3 },
|
||||
] as never);
|
||||
]) as never;
|
||||
|
||||
const req = createRequest("/api/blindbox/rooms");
|
||||
const res = await GET(req, mockCtx);
|
||||
|
||||
@@ -46,6 +46,8 @@ vi.mock("@/hooks/useRoomPolling", () => ({
|
||||
users: ["user-1", "user-2"],
|
||||
userProfiles: {},
|
||||
scene: "eat",
|
||||
isLoading: false,
|
||||
error: undefined,
|
||||
}),
|
||||
}));
|
||||
|
||||
@@ -114,11 +116,13 @@ describe("RoomPage", () => {
|
||||
restaurants: [],
|
||||
notFound: true,
|
||||
mutate: vi.fn(),
|
||||
creatorId: null,
|
||||
creatorId: "",
|
||||
locked: false,
|
||||
users: [],
|
||||
userProfiles: {},
|
||||
scene: "eat",
|
||||
isLoading: false,
|
||||
error: undefined,
|
||||
});
|
||||
|
||||
renderPage();
|
||||
|
||||
@@ -36,6 +36,7 @@ const mockDays: WeekendPlanData[] = [
|
||||
duration: 90,
|
||||
lat: 39.9,
|
||||
lng: 116.4,
|
||||
reason: "补充体力",
|
||||
},
|
||||
],
|
||||
},
|
||||
@@ -51,6 +52,7 @@ const mockDays: WeekendPlanData[] = [
|
||||
duration: 180,
|
||||
lat: 39.9,
|
||||
lng: 116.4,
|
||||
reason: "文化体验",
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
@@ -41,7 +41,7 @@ const defaultProps = {
|
||||
userId: "user-1",
|
||||
initialIndex: 0,
|
||||
matchedRestaurantId: null,
|
||||
matchType: null as const,
|
||||
matchType: null,
|
||||
matchLikes: 0,
|
||||
runnerUps: [],
|
||||
likeCounts: {},
|
||||
|
||||
Reference in New Issue
Block a user