Skip to content

Commit d97eab0

Browse files
committed
Added some more API tests
1 parent b5f36ae commit d97eab0

File tree

3 files changed

+235
-4
lines changed

3 files changed

+235
-4
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import {
2+
describe,
3+
it,
4+
vi,
5+
expect,
6+
beforeEach,
7+
afterEach,
8+
type Mock,
9+
} from "vitest";
10+
import { fetchCallback } from "./fetchCallback";
11+
12+
describe("fetchCallback", () => {
13+
beforeEach(() => {
14+
// Mock the global fetch function
15+
globalThis.fetch = vi.fn();
16+
});
17+
18+
afterEach(() => {
19+
// Restore the original fetch implementation
20+
vi.restoreAllMocks();
21+
});
22+
23+
it("should return expected data on success", async () => {
24+
// Define a mock response
25+
const expectedResult = {
26+
contribPoint: "panels",
27+
contribIndex: 0,
28+
stateChanges: [
29+
{
30+
id: "checkbox0",
31+
property: "value",
32+
value: true,
33+
},
34+
],
35+
};
36+
const mockResponse = {
37+
ok: true,
38+
status: 200,
39+
statusText: "ok",
40+
json: vi.fn().mockResolvedValue({
41+
result: expectedResult,
42+
}),
43+
};
44+
45+
// Mock fetch to resolve with the mock response
46+
(globalThis.fetch as Mock).mockResolvedValue(mockResponse);
47+
48+
// Call fetch
49+
const response = await fetchCallback([], {
50+
serverUrl: "https://chartlets-test",
51+
endpointName: "api",
52+
});
53+
54+
// Assertions
55+
expect(fetch).toHaveBeenCalledOnce();
56+
expect(response).toEqual({
57+
status: "ok",
58+
data: expectedResult,
59+
});
60+
});
61+
62+
it("should return error for bad API responses", async () => {
63+
// Define a mock response
64+
const mockResponse = {
65+
ok: true,
66+
status: 200,
67+
statusText: "ok",
68+
json: vi.fn().mockResolvedValue({ message: "Hello, world!" }),
69+
};
70+
71+
// Mock fetch to resolve with the mock response
72+
(globalThis.fetch as Mock).mockResolvedValue(mockResponse);
73+
74+
// Call fetch
75+
const response = await fetchCallback([], {
76+
serverUrl: "https://chartlets-test",
77+
endpointName: "api",
78+
});
79+
// Assertions
80+
expect(fetch).toHaveBeenCalledOnce();
81+
expect(response).toEqual({
82+
status: "failed",
83+
error: {
84+
message: "unexpected response from https://chartlets-test/api/callback",
85+
},
86+
});
87+
});
88+
});

chartlets.js/packages/lib/src/api/fetchContributions.test.ts

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { describe, it, vi, expect, beforeEach, afterEach, Mock } from "vitest";
1+
import {
2+
describe,
3+
it,
4+
vi,
5+
expect,
6+
beforeEach,
7+
afterEach,
8+
type Mock,
9+
} from "vitest";
210
import { fetchContributions } from "./fetchContributions";
311

412
describe("fetchContributions", () => {
@@ -14,8 +22,19 @@ describe("fetchContributions", () => {
1422

1523
it("should return expected data on success", async () => {
1624
// Define a mock response
25+
const expectedResult = {
26+
extensions: [{ name: "e0", version: "1", contributes: ["panels"] }],
27+
contributions: {
28+
panels: [{ name: "p0", extension: "e0", layout: {} }],
29+
},
30+
};
1731
const mockResponse = {
18-
json: vi.fn().mockResolvedValue({ message: "Hello, world!" }),
32+
ok: true,
33+
status: 200,
34+
statusText: "ok",
35+
json: vi.fn().mockResolvedValue({
36+
result: expectedResult,
37+
}),
1938
};
2039

2140
// Mock fetch to resolve with the mock response
@@ -26,10 +45,52 @@ describe("fetchContributions", () => {
2645
serverUrl: "https://chartlets-test",
2746
endpointName: "api",
2847
});
29-
const data = response.data;
3048

3149
// Assertions
3250
expect(fetch).toHaveBeenCalledOnce();
33-
expect(data).toEqual({ message: "Hello, world!" });
51+
expect(response).toEqual({
52+
status: "ok",
53+
data: {
54+
extensions: [{ name: "e0", version: "1", contributes: ["panels"] }],
55+
contributions: {
56+
panels: [
57+
{
58+
name: "p0",
59+
extension: "e0",
60+
layout: { inputs: [], outputs: [] },
61+
callbacks: [],
62+
},
63+
],
64+
},
65+
},
66+
});
67+
});
68+
69+
it("should return error for bad responses", async () => {
70+
// Define a mock response
71+
const mockResponse = {
72+
ok: true,
73+
status: 200,
74+
statusText: "ok",
75+
json: vi.fn().mockResolvedValue({ message: "Hello, world!" }),
76+
};
77+
78+
// Mock fetch to resolve with the mock response
79+
(globalThis.fetch as Mock).mockResolvedValue(mockResponse);
80+
81+
// Call fetch
82+
const response = await fetchContributions({
83+
serverUrl: "https://chartlets-test",
84+
endpointName: "api",
85+
});
86+
// Assertions
87+
expect(fetch).toHaveBeenCalledOnce();
88+
expect(response).toEqual({
89+
status: "failed",
90+
error: {
91+
message:
92+
"unexpected response from https://chartlets-test/api/contributions",
93+
},
94+
});
3495
});
3596
});
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import {
2+
describe,
3+
it,
4+
vi,
5+
expect,
6+
beforeEach,
7+
afterEach,
8+
type Mock,
9+
} from "vitest";
10+
import { fetchLayout } from "./fetchLayout";
11+
12+
describe("fetchLayout", () => {
13+
beforeEach(() => {
14+
// Mock the global fetch function
15+
globalThis.fetch = vi.fn();
16+
});
17+
18+
afterEach(() => {
19+
// Restore the original fetch implementation
20+
vi.restoreAllMocks();
21+
});
22+
23+
it("should return expected data on success", async () => {
24+
// Define a mock response
25+
const expectedResult = {
26+
type: "Button",
27+
text: "Click me!",
28+
};
29+
const mockResponse = {
30+
ok: true,
31+
status: 200,
32+
statusText: "ok",
33+
json: vi.fn().mockResolvedValue({
34+
result: expectedResult,
35+
}),
36+
};
37+
38+
// Mock fetch to resolve with the mock response
39+
(globalThis.fetch as Mock).mockResolvedValue(mockResponse);
40+
41+
// Call fetch
42+
const response = await fetchLayout("panels", 0, [], {
43+
serverUrl: "https://chartlets-test",
44+
endpointName: "api",
45+
});
46+
47+
// Assertions
48+
expect(fetch).toHaveBeenCalledOnce();
49+
expect(response).toEqual({
50+
status: "ok",
51+
data: expectedResult,
52+
});
53+
});
54+
55+
it("should return error for bad responses", async () => {
56+
// Define a mock response
57+
const mockResponse = {
58+
ok: true,
59+
status: 200,
60+
statusText: "ok",
61+
json: vi.fn().mockResolvedValue({ message: "Hello, world!" }),
62+
};
63+
64+
// Mock fetch to resolve with the mock response
65+
(globalThis.fetch as Mock).mockResolvedValue(mockResponse);
66+
67+
// Call fetch
68+
const response = await fetchLayout("panels", 0, [], {
69+
serverUrl: "https://chartlets-test",
70+
endpointName: "api",
71+
});
72+
// Assertions
73+
expect(fetch).toHaveBeenCalledOnce();
74+
expect(response).toEqual({
75+
status: "failed",
76+
error: {
77+
message:
78+
"unexpected response from https://chartlets-test/api/layout/panels/0",
79+
},
80+
});
81+
});
82+
});

0 commit comments

Comments
 (0)