-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathfetchPusherConfig.spec.ts
More file actions
70 lines (52 loc) · 2.01 KB
/
Copy pathfetchPusherConfig.spec.ts
File metadata and controls
70 lines (52 loc) · 2.01 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
import { fetchPusherConfig } from "../utils/fetchPusherConfig";
function mockFetch(status: number, body: unknown): void {
global.fetch = jest.fn().mockResolvedValue({
status,
json: () => Promise.resolve(body)
});
}
beforeEach(() => {
(window as any).mx = {
remoteUrl: "https://app.example.com/",
sessionData: { csrftoken: "test-csrf" }
};
});
afterEach(() => {
delete (window as any).mx;
jest.resetAllMocks();
});
describe("fetchPusherConfig", () => {
it("returns config on successful response", async () => {
mockFetch(200, { key: "app-key", cluster: "eu" });
const result = await fetchPusherConfig(new AbortController().signal);
expect(result).toEqual({
key: "app-key",
cluster: "eu",
authEndpoint: "https://app.example.com/rest/pusher/auth",
csrfToken: "test-csrf"
});
});
it("returns null on non-200 response", async () => {
mockFetch(403, {});
const result = await fetchPusherConfig(new AbortController().signal);
expect(result).toBeNull();
});
it("returns null on network error", async () => {
global.fetch = jest.fn().mockRejectedValue(new Error("Network failure"));
const result = await fetchPusherConfig(new AbortController().signal);
expect(result).toBeNull();
});
it("returns null when signal is already aborted", async () => {
const controller = new AbortController();
const abortError = new DOMException("Aborted", "AbortError");
global.fetch = jest.fn().mockRejectedValue(abortError);
controller.abort();
const result = await fetchPusherConfig(controller.signal);
expect(result).toBeNull();
});
it("returns null when response is missing required fields", async () => {
mockFetch(200, { key: "app-key" }); // missing cluster
const result = await fetchPusherConfig(new AbortController().signal);
expect(result).toBeNull();
});
});