test: cover error model and unauth order create
This commit is contained in:
@@ -0,0 +1,29 @@
|
|||||||
|
import { isApiError, toApiError } from "@/lib/errors"
|
||||||
|
import { describe, expect, it } from "vitest"
|
||||||
|
|
||||||
|
describe("lib/errors", () => {
|
||||||
|
describe("toApiError", () => {
|
||||||
|
it("passes through {code,msg} ApiError", () => {
|
||||||
|
const err = { code: 400, msg: "bad request" }
|
||||||
|
expect(isApiError(err)).toBe(true)
|
||||||
|
|
||||||
|
try {
|
||||||
|
throw err
|
||||||
|
} catch (e) {
|
||||||
|
expect(toApiError(e)).toBe(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it("converts Error to safe {code,msg}", () => {
|
||||||
|
expect(toApiError(new Error("x"))).toEqual({ code: 500, msg: "x" })
|
||||||
|
})
|
||||||
|
|
||||||
|
it("converts string to safe {code,msg}", () => {
|
||||||
|
expect(toApiError("x")).toEqual({ code: 500, msg: "x" })
|
||||||
|
})
|
||||||
|
|
||||||
|
it("converts unknown input to safe {code,msg}", () => {
|
||||||
|
expect(toApiError(null)).toEqual({ code: 500, msg: "Unknown error" })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import { createPaidOrder } from "@/lib/api/orders"
|
||||||
|
import { useAuthStore } from "@/store/auth"
|
||||||
|
import { describe, expect, it } from "vitest"
|
||||||
|
|
||||||
|
describe("lib/api/orders", () => {
|
||||||
|
it("returns { decision: { ok:false, error:{code,msg} } } when unauthenticated", () => {
|
||||||
|
useAuthStore.getState().logout()
|
||||||
|
|
||||||
|
const res = createPaidOrder({
|
||||||
|
playerId: "1005",
|
||||||
|
serviceId: "5001",
|
||||||
|
quantity: 1,
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(res).toEqual({
|
||||||
|
decision: {
|
||||||
|
ok: false,
|
||||||
|
error: { code: 401, msg: "请先登录" },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user