Skip to content

Commit 0272495

Browse files
committed
test: fix fetch mocks to use res.text() in api-keys, webhooks, events, pairs tests
1 parent 433bd29 commit 0272495

4 files changed

Lines changed: 44 additions & 44 deletions

File tree

src/app/api-keys/page.test.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@ import { render, screen, waitFor } from "@testing-library/react";
22
import ApiKeysPage from "./page";
33

44
describe("ApiKeysPage", () => {
5-
let originalFetch: typeof globalThis.fetch;
5+
let originalFetch: typeof global.fetch;
66

77
beforeEach(() => {
8-
originalFetch = globalThis.fetch;
8+
originalFetch = global.fetch;
99
});
1010

1111
afterEach(() => {
12-
globalThis.fetch = originalFetch;
12+
global.fetch = originalFetch;
1313
});
1414

1515
it("shows loading before data arrives", () => {
16-
globalThis.fetch = jest.fn(() => new Promise(() => {})) as unknown as typeof globalThis.fetch;
16+
global.fetch = jest.fn(() => new Promise(() => {})) as unknown as typeof global.fetch;
1717
render(<ApiKeysPage />);
1818
expect(screen.getByText("Loading…")).toBeInTheDocument();
1919
});
2020

2121
it("renders api keys in a single polite live region", async () => {
22-
globalThis.fetch = jest.fn().mockResolvedValueOnce({
22+
global.fetch = jest.fn().mockResolvedValueOnce({
2323
ok: true,
24-
json: async () => ({
24+
text: async () => JSON.stringify({
2525
items: [{ prefix: "sk_abc", label: "Production", createdAt: Date.now() }],
2626
}),
2727
} as unknown as Response);
@@ -36,9 +36,9 @@ describe("ApiKeysPage", () => {
3636
});
3737

3838
it("announces empty state via live region", async () => {
39-
globalThis.fetch = jest.fn().mockResolvedValueOnce({
39+
global.fetch = jest.fn().mockResolvedValueOnce({
4040
ok: true,
41-
json: async () => ({ items: [] }),
41+
text: async () => JSON.stringify({ items: [] }),
4242
} as unknown as Response);
4343

4444
render(<ApiKeysPage />);
@@ -48,7 +48,7 @@ describe("ApiKeysPage", () => {
4848
});
4949

5050
it("surfaces errors with role=alert", async () => {
51-
globalThis.fetch = jest.fn().mockRejectedValueOnce(new Error("Unauthorized"));
51+
global.fetch = jest.fn().mockRejectedValueOnce(new Error("Unauthorized"));
5252

5353
render(<ApiKeysPage />);
5454
await waitFor(() => {
@@ -57,9 +57,9 @@ describe("ApiKeysPage", () => {
5757
});
5858

5959
it("has exactly one aria-live=polite region", async () => {
60-
globalThis.fetch = jest.fn().mockResolvedValueOnce({
60+
global.fetch = jest.fn().mockResolvedValueOnce({
6161
ok: true,
62-
json: async () => ({ items: [] }),
62+
text: async () => JSON.stringify({ items: [] }),
6363
} as unknown as Response);
6464

6565
render(<ApiKeysPage />);

src/app/events/page.test.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@ import { render, screen, waitFor } from "@testing-library/react";
22
import EventsPage from "./page";
33

44
describe("EventsPage", () => {
5-
let originalFetch: typeof globalThis.fetch;
5+
let originalFetch: typeof global.fetch;
66

77
beforeEach(() => {
8-
originalFetch = globalThis.fetch;
8+
originalFetch = global.fetch;
99
});
1010

1111
afterEach(() => {
12-
globalThis.fetch = originalFetch;
12+
global.fetch = originalFetch;
1313
});
1414

1515
it("shows loading before data arrives", () => {
16-
globalThis.fetch = jest.fn(() => new Promise(() => {})) as unknown as typeof globalThis.fetch;
16+
global.fetch = jest.fn(() => new Promise(() => {})) as unknown as typeof global.fetch;
1717
render(<EventsPage />);
1818
expect(screen.getByText("Loading…")).toBeInTheDocument();
1919
});
2020

2121
it("renders events in a single polite live region", async () => {
22-
globalThis.fetch = jest.fn().mockResolvedValueOnce({
22+
global.fetch = jest.fn().mockResolvedValueOnce({
2323
ok: true,
24-
json: async () => ({
24+
text: async () => JSON.stringify({
2525
items: [{ id: "evt1", ts: Date.now(), type: "pair.registered", payload: {} }],
2626
}),
2727
} as unknown as Response);
@@ -36,9 +36,9 @@ describe("EventsPage", () => {
3636
});
3737

3838
it("announces empty state via live region", async () => {
39-
globalThis.fetch = jest.fn().mockResolvedValueOnce({
39+
global.fetch = jest.fn().mockResolvedValueOnce({
4040
ok: true,
41-
json: async () => ({ items: [] }),
41+
text: async () => JSON.stringify({ items: [] }),
4242
} as unknown as Response);
4343

4444
render(<EventsPage />);
@@ -48,7 +48,7 @@ describe("EventsPage", () => {
4848
});
4949

5050
it("surfaces errors with role=alert", async () => {
51-
globalThis.fetch = jest.fn().mockRejectedValueOnce(new Error("Failed to load"));
51+
global.fetch = jest.fn().mockRejectedValueOnce(new Error("Failed to load"));
5252

5353
render(<EventsPage />);
5454
await waitFor(() => {
@@ -57,9 +57,9 @@ describe("EventsPage", () => {
5757
});
5858

5959
it("has exactly one aria-live=polite region", async () => {
60-
globalThis.fetch = jest.fn().mockResolvedValueOnce({
60+
global.fetch = jest.fn().mockResolvedValueOnce({
6161
ok: true,
62-
json: async () => ({ items: [] }),
62+
text: async () => JSON.stringify({ items: [] }),
6363
} as unknown as Response);
6464

6565
render(<EventsPage />);

src/app/pairs/page.test.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@ import { render, screen, waitFor } from "@testing-library/react";
22
import PairsPage from "./page";
33

44
describe("PairsPage", () => {
5-
let originalFetch: typeof globalThis.fetch;
5+
let originalFetch: typeof global.fetch;
66

77
beforeEach(() => {
8-
originalFetch = globalThis.fetch;
8+
originalFetch = global.fetch;
99
});
1010

1111
afterEach(() => {
12-
globalThis.fetch = originalFetch;
12+
global.fetch = originalFetch;
1313
});
1414

1515
it("shows loading before data arrives", () => {
16-
globalThis.fetch = jest.fn(() => new Promise(() => {})) as unknown as typeof globalThis.fetch;
16+
global.fetch = jest.fn(() => new Promise(() => {})) as unknown as typeof global.fetch;
1717
render(<PairsPage />);
1818
expect(screen.getByText("Loading…")).toBeInTheDocument();
1919
});
2020

2121
it("renders pairs in a single polite live region", async () => {
22-
globalThis.fetch = jest.fn().mockResolvedValueOnce({
22+
global.fetch = jest.fn().mockResolvedValueOnce({
2323
ok: true,
24-
json: async () => ({ pairs: [{ source: "USDC", destination: "EURC" }] }),
24+
text: async () => JSON.stringify({ pairs: [{ source: "USDC", destination: "EURC" }] }),
2525
} as unknown as Response);
2626

2727
render(<PairsPage />);
@@ -34,9 +34,9 @@ describe("PairsPage", () => {
3434
});
3535

3636
it("announces empty state via live region", async () => {
37-
globalThis.fetch = jest.fn().mockResolvedValueOnce({
37+
global.fetch = jest.fn().mockResolvedValueOnce({
3838
ok: true,
39-
json: async () => ({ pairs: [] }),
39+
text: async () => JSON.stringify({ pairs: [] }),
4040
} as unknown as Response);
4141

4242
render(<PairsPage />);
@@ -46,7 +46,7 @@ describe("PairsPage", () => {
4646
});
4747

4848
it("surfaces errors with role=alert", async () => {
49-
globalThis.fetch = jest.fn().mockRejectedValueOnce(new Error("Network error"));
49+
global.fetch = jest.fn().mockRejectedValueOnce(new Error("Network error"));
5050

5151
render(<PairsPage />);
5252
await waitFor(() => {
@@ -55,9 +55,9 @@ describe("PairsPage", () => {
5555
});
5656

5757
it("has exactly one aria-live=polite region", async () => {
58-
globalThis.fetch = jest.fn().mockResolvedValueOnce({
58+
global.fetch = jest.fn().mockResolvedValueOnce({
5959
ok: true,
60-
json: async () => ({ pairs: [] }),
60+
text: async () => JSON.stringify({ pairs: [] }),
6161
} as unknown as Response);
6262

6363
render(<PairsPage />);

src/app/webhooks/page.test.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@ import { render, screen, waitFor } from "@testing-library/react";
22
import WebhooksPage from "./page";
33

44
describe("WebhooksPage", () => {
5-
let originalFetch: typeof globalThis.fetch;
5+
let originalFetch: typeof global.fetch;
66

77
beforeEach(() => {
8-
originalFetch = globalThis.fetch;
8+
originalFetch = global.fetch;
99
});
1010

1111
afterEach(() => {
12-
globalThis.fetch = originalFetch;
12+
global.fetch = originalFetch;
1313
});
1414

1515
it("shows loading before data arrives", () => {
16-
globalThis.fetch = jest.fn(() => new Promise(() => {})) as unknown as typeof globalThis.fetch;
16+
global.fetch = jest.fn(() => new Promise(() => {})) as unknown as typeof global.fetch;
1717
render(<WebhooksPage />);
1818
expect(screen.getByText("Loading…")).toBeInTheDocument();
1919
});
2020

2121
it("renders webhooks in a single polite live region", async () => {
22-
globalThis.fetch = jest.fn().mockResolvedValueOnce({
22+
global.fetch = jest.fn().mockResolvedValueOnce({
2323
ok: true,
24-
json: async () => ({
24+
text: async () => JSON.stringify({
2525
items: [{ id: "wh1", url: "https://example.com/hook", events: ["pair.registered"], createdAt: Date.now() }],
2626
}),
2727
} as unknown as Response);
@@ -36,9 +36,9 @@ describe("WebhooksPage", () => {
3636
});
3737

3838
it("announces empty state via live region", async () => {
39-
globalThis.fetch = jest.fn().mockResolvedValueOnce({
39+
global.fetch = jest.fn().mockResolvedValueOnce({
4040
ok: true,
41-
json: async () => ({ items: [] }),
41+
text: async () => JSON.stringify({ items: [] }),
4242
} as unknown as Response);
4343

4444
render(<WebhooksPage />);
@@ -48,7 +48,7 @@ describe("WebhooksPage", () => {
4848
});
4949

5050
it("surfaces errors with role=alert", async () => {
51-
globalThis.fetch = jest.fn().mockRejectedValueOnce(new Error("Forbidden"));
51+
global.fetch = jest.fn().mockRejectedValueOnce(new Error("Forbidden"));
5252

5353
render(<WebhooksPage />);
5454
await waitFor(() => {
@@ -57,9 +57,9 @@ describe("WebhooksPage", () => {
5757
});
5858

5959
it("has exactly one aria-live=polite region", async () => {
60-
globalThis.fetch = jest.fn().mockResolvedValueOnce({
60+
global.fetch = jest.fn().mockResolvedValueOnce({
6161
ok: true,
62-
json: async () => ({ items: [] }),
62+
text: async () => JSON.stringify({ items: [] }),
6363
} as unknown as Response);
6464

6565
render(<WebhooksPage />);

0 commit comments

Comments
 (0)