Skip to content

Commit d2490c4

Browse files
committed
testing fetchContributions
1 parent f77b23f commit d2490c4

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { describe, it, vi, expect, beforeEach, afterEach, Mock } from "vitest";
2+
import { fetchContributions } from "./fetchContributions";
3+
4+
describe("fetchContributions", () => {
5+
beforeEach(() => {
6+
// Mock the global fetch function
7+
globalThis.fetch = vi.fn();
8+
});
9+
10+
afterEach(() => {
11+
// Restore the original fetch implementation
12+
vi.restoreAllMocks();
13+
});
14+
15+
it("should return expected data on success", async () => {
16+
// Define a mock response
17+
const mockResponse = {
18+
json: vi.fn().mockResolvedValue({ message: "Hello, world!" }),
19+
};
20+
21+
// Mock fetch to resolve with the mock response
22+
(globalThis.fetch as Mock).mockResolvedValue(mockResponse);
23+
24+
// Call fetch
25+
const response = await fetchContributions({
26+
serverUrl: "https://chartlets-test",
27+
endpointName: "api",
28+
});
29+
const data = response.data;
30+
31+
// Assertions
32+
expect(fetch).toHaveBeenCalledOnce();
33+
expect(data).toEqual({ message: "Hello, world!" });
34+
});
35+
});

0 commit comments

Comments
 (0)