forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract-api-error.test.ts
More file actions
42 lines (36 loc) · 1.45 KB
/
Copy pathextract-api-error.test.ts
File metadata and controls
42 lines (36 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// src/lib/__tests__/extract-api-error.test.ts
import { describe, expect, it } from "vitest"
import { extractApiError } from "@/lib/extract-api-error"
describe("extractApiError", () => {
it("returns the error field from a JSON response", async () => {
const res = new Response(JSON.stringify({ error: "Bad request" }), {
status: 400,
headers: { "Content-Type": "application/json" },
})
expect(await extractApiError(res)).toBe("Bad request")
})
it("returns the default fallback when error field is undefined", async () => {
const res = new Response(JSON.stringify({ error: undefined }), {
status: 400,
headers: { "Content-Type": "application/json" },
})
expect(await extractApiError(res)).toBe("Request failed")
})
it("returns the default fallback when JSON has no error field", async () => {
const res = new Response(JSON.stringify({}), {
status: 500,
headers: { "Content-Type": "application/json" },
})
expect(await extractApiError(res)).toBe("Request failed")
})
it("returns the default fallback on invalid JSON", async () => {
const res = new Response("not json", {
status: 500,
})
expect(await extractApiError(res)).toBe("Request failed")
})
it("returns a custom fallback string when provided", async () => {
const res = new Response("not json", { status: 503 })
expect(await extractApiError(res, "Service unavailable")).toBe("Service unavailable")
})
})