-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackApiV2.test.ts
More file actions
83 lines (68 loc) · 3.17 KB
/
Copy pathstackApiV2.test.ts
File metadata and controls
83 lines (68 loc) · 3.17 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { afterEach, describe, expect, it, vi } from "vitest";
import { StackApiV2Client } from "./stackApiV2";
afterEach(() => {
vi.unstubAllGlobals();
});
describe("StackApiV2Client", () => {
it("fetches all pages and appends the team slug for Basic/Business", async () => {
const fetchMock = vi.fn()
.mockResolvedValueOnce(new Response(JSON.stringify({ items: [{ id: 1 }], has_more: true }), { status: 200 }))
.mockResolvedValueOnce(new Response(JSON.stringify({ items: [{ id: 2 }], has_more: false }), { status: 200 }));
const client = new StackApiV2Client({
apiV2Url: "https://api.stackoverflowteams.com/2.3",
teamSlug: "example-team",
headers: { "X-API-Access-Token": "token" },
fetchFn: fetchMock,
});
await expect(client.getPagedItems("/users", { pagesize: "100" })).resolves.toEqual([{ id: 1 }, { id: 2 }]);
expect(fetchMock.mock.calls[0][0].toString()).toContain("team=example-team");
expect(fetchMock.mock.calls[1][0].toString()).toContain("page=2");
});
it("stops pagination at the requested max pages", async () => {
const fetchMock = vi.fn()
.mockResolvedValueOnce(new Response(JSON.stringify({ items: [{ id: 1 }], has_more: true }), { status: 200 }))
.mockResolvedValueOnce(new Response(JSON.stringify({ items: [{ id: 2 }], has_more: true }), { status: 200 }));
const client = new StackApiV2Client({
apiV2Url: "https://api.stackoverflowteams.com/2.3",
teamSlug: "example-team",
fetchFn: fetchMock,
});
await expect(client.getPagedItems("/users", { pagesize: "50" }, { maxPages: 1 })).resolves.toEqual([{ id: 1 }]);
expect(fetchMock).toHaveBeenCalledTimes(1);
});
it("throws a mapped error on non-200 responses", async () => {
const fetchMock = vi.fn().mockResolvedValue(new Response("bad key", { status: 400 }));
const client = new StackApiV2Client({
apiV2Url: "https://demo.stackenterprise.co/api/2.3",
teamSlug: null,
headers: { "X-API-Key": "bad" },
fetchFn: fetchMock,
});
await expect(client.getPagedItems("/tags")).rejects.toThrow("Stack API v2.3 request failed with 400");
});
it("throws a contextual error on invalid JSON responses", async () => {
const fetchMock = vi.fn().mockResolvedValue(new Response("not json", { status: 200 }));
const client = new StackApiV2Client({
apiV2Url: "https://demo.stackenterprise.co/api/2.3",
teamSlug: null,
fetchFn: fetchMock,
});
await expect(client.getPagedItems("/tags")).rejects.toThrow("Stack API v2.3 returned invalid JSON");
});
it("calls the default browser fetch with the global receiver", async () => {
const fetchMock = vi.fn(function (this: unknown) {
if (this !== globalThis) {
throw new TypeError("Illegal invocation");
}
return Promise.resolve(
new Response(JSON.stringify({ items: [{ id: 1 }], has_more: false }), { status: 200 }),
);
});
vi.stubGlobal("fetch", fetchMock);
const client = new StackApiV2Client({
apiV2Url: "https://api.stackoverflowteams.com/2.3",
teamSlug: "example-team",
});
await expect(client.getPagedItems("/users")).resolves.toEqual([{ id: 1 }]);
});
});