diff --git a/chartlets.js/packages/lib/src/api/fetchCallback.test.ts b/chartlets.js/packages/lib/src/api/fetchCallback.test.ts new file mode 100644 index 00000000..b5516a1f --- /dev/null +++ b/chartlets.js/packages/lib/src/api/fetchCallback.test.ts @@ -0,0 +1,88 @@ +import { + describe, + it, + vi, + expect, + beforeEach, + afterEach, + type Mock, +} from "vitest"; +import { fetchCallback } from "./fetchCallback"; + +describe("fetchCallback", () => { + beforeEach(() => { + // Mock the global fetch function + globalThis.fetch = vi.fn(); + }); + + afterEach(() => { + // Restore the original fetch implementation + vi.restoreAllMocks(); + }); + + it("should return expected data on success", async () => { + // Define a mock response + const expectedResult = { + contribPoint: "panels", + contribIndex: 0, + stateChanges: [ + { + id: "checkbox0", + property: "value", + value: true, + }, + ], + }; + const mockResponse = { + ok: true, + status: 200, + statusText: "ok", + json: vi.fn().mockResolvedValue({ + result: expectedResult, + }), + }; + + // Mock fetch to resolve with the mock response + (globalThis.fetch as Mock).mockResolvedValue(mockResponse); + + // Call fetch + const response = await fetchCallback([], { + serverUrl: "https://chartlets-test", + endpointName: "api", + }); + + // Assertions + expect(fetch).toHaveBeenCalledOnce(); + expect(response).toEqual({ + status: "ok", + data: expectedResult, + }); + }); + + it("should return error for bad API responses", async () => { + // Define a mock response + const mockResponse = { + ok: true, + status: 200, + statusText: "ok", + json: vi.fn().mockResolvedValue({ message: "Hello, world!" }), + }; + + // Mock fetch to resolve with the mock response + (globalThis.fetch as Mock).mockResolvedValue(mockResponse); + + // Call fetch + const response = await fetchCallback([], { + serverUrl: "https://chartlets-test", + endpointName: "api", + }); + // Assertions + expect(fetch).toHaveBeenCalledOnce(); + expect(response).toEqual({ + status: "failed", + error: { + message: "unexpected response from https://chartlets-test/api/callback", + }, + }); + }); +}); diff --git a/chartlets.js/packages/lib/src/api/fetchContributions.test.ts b/chartlets.js/packages/lib/src/api/fetchContributions.test.ts new file mode 100644 index 00000000..ea95b046 --- /dev/null +++ b/chartlets.js/packages/lib/src/api/fetchContributions.test.ts @@ -0,0 +1,96 @@ +import { + describe, + it, + vi, + expect, + beforeEach, + afterEach, + type Mock, +} from "vitest"; +import { fetchContributions } from "./fetchContributions"; + +describe("fetchContributions", () => { + beforeEach(() => { + // Mock the global fetch function + globalThis.fetch = vi.fn(); + }); + + afterEach(() => { + // Restore the original fetch implementation + vi.restoreAllMocks(); + }); + + it("should return expected data on success", async () => { + // Define a mock response + const expectedResult = { + extensions: [{ name: "e0", version: "1", contributes: ["panels"] }], + contributions: { + panels: [{ name: "p0", extension: "e0", layout: {} }], + }, + }; + const mockResponse = { + ok: true, + status: 200, + statusText: "ok", + json: vi.fn().mockResolvedValue({ + result: expectedResult, + }), + }; + + // Mock fetch to resolve with the mock response + (globalThis.fetch as Mock).mockResolvedValue(mockResponse); + + // Call fetch + const response = await fetchContributions({ + serverUrl: "https://chartlets-test", + endpointName: "api", + }); + + // Assertions + expect(fetch).toHaveBeenCalledOnce(); + expect(response).toEqual({ + status: "ok", + data: { + extensions: [{ name: "e0", version: "1", contributes: ["panels"] }], + contributions: { + panels: [ + { + name: "p0", + extension: "e0", + layout: { inputs: [], outputs: [] }, + callbacks: [], + }, + ], + }, + }, + }); + }); + + it("should return error for bad responses", async () => { + // Define a mock response + const mockResponse = { + ok: true, + status: 200, + statusText: "ok", + json: vi.fn().mockResolvedValue({ message: "Hello, world!" }), + }; + + // Mock fetch to resolve with the mock response + (globalThis.fetch as Mock).mockResolvedValue(mockResponse); + + // Call fetch + const response = await fetchContributions({ + serverUrl: "https://chartlets-test", + endpointName: "api", + }); + // Assertions + expect(fetch).toHaveBeenCalledOnce(); + expect(response).toEqual({ + status: "failed", + error: { + message: + "unexpected response from https://chartlets-test/api/contributions", + }, + }); + }); +}); diff --git a/chartlets.js/packages/lib/src/api/fetchLayout.test.ts b/chartlets.js/packages/lib/src/api/fetchLayout.test.ts new file mode 100644 index 00000000..f8a26531 --- /dev/null +++ b/chartlets.js/packages/lib/src/api/fetchLayout.test.ts @@ -0,0 +1,82 @@ +import { + describe, + it, + vi, + expect, + beforeEach, + afterEach, + type Mock, +} from "vitest"; +import { fetchLayout } from "./fetchLayout"; + +describe("fetchLayout", () => { + beforeEach(() => { + // Mock the global fetch function + globalThis.fetch = vi.fn(); + }); + + afterEach(() => { + // Restore the original fetch implementation + vi.restoreAllMocks(); + }); + + it("should return expected data on success", async () => { + // Define a mock response + const expectedResult = { + type: "Button", + text: "Click me!", + }; + const mockResponse = { + ok: true, + status: 200, + statusText: "ok", + json: vi.fn().mockResolvedValue({ + result: expectedResult, + }), + }; + + // Mock fetch to resolve with the mock response + (globalThis.fetch as Mock).mockResolvedValue(mockResponse); + + // Call fetch + const response = await fetchLayout("panels", 0, [], { + serverUrl: "https://chartlets-test", + endpointName: "api", + }); + + // Assertions + expect(fetch).toHaveBeenCalledOnce(); + expect(response).toEqual({ + status: "ok", + data: expectedResult, + }); + }); + + it("should return error for bad responses", async () => { + // Define a mock response + const mockResponse = { + ok: true, + status: 200, + statusText: "ok", + json: vi.fn().mockResolvedValue({ message: "Hello, world!" }), + }; + + // Mock fetch to resolve with the mock response + (globalThis.fetch as Mock).mockResolvedValue(mockResponse); + + // Call fetch + const response = await fetchLayout("panels", 0, [], { + serverUrl: "https://chartlets-test", + endpointName: "api", + }); + // Assertions + expect(fetch).toHaveBeenCalledOnce(); + expect(response).toEqual({ + status: "failed", + error: { + message: + "unexpected response from https://chartlets-test/api/layout/panels/0", + }, + }); + }); +});