test: decouple unit tests from mock fixtures

This commit is contained in:
zetaloop
2026-03-01 22:30:05 +08:00
parent 9e64fb1201
commit 6ee14f6eef
2 changed files with 231 additions and 21 deletions
+23 -13
View File
@@ -1,24 +1,34 @@
import { createPaidOrder } from "@/lib/api/orders"
import { useAuthStore } from "@/store/auth"
import { describe, expect, it } from "vitest"
import { describe, expect, it, vi } from "vitest"
describe("lib/api/orders", () => {
it("returns { decision: { ok:false, error:{code,msg} } } when unauthenticated", async () => {
useAuthStore.getState().logout()
vi.stubGlobal(
"fetch",
vi.fn(
async () =>
new Response(JSON.stringify({ code: 401, msg: "请先登录" }), {
status: 401,
headers: { "Content-Type": "application/json" },
}),
),
)
const res = await Promise.resolve(
createPaidOrder({
try {
const res = await createPaidOrder({
playerId: "1005",
serviceId: "5001",
quantity: 1,
}),
)
})
expect(res).toEqual({
decision: {
ok: false,
error: { code: 401, msg: "请先登录" },
},
})
expect(res).toEqual({
decision: {
ok: false,
error: { code: 401, msg: "请先登录" },
},
})
} finally {
vi.unstubAllGlobals()
}
})
})