From d2490c4d1c7f60b8292b3f89fdb68ab278aa6d54 Mon Sep 17 00:00:00 2001 From: Norman Fomferra Date: Thu, 12 Dec 2024 08:23:48 +0100 Subject: [PATCH 1/2] testing fetchContributions --- .../lib/src/api/fetchContributions.test.ts | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 chartlets.js/packages/lib/src/api/fetchContributions.test.ts 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..bc843633 --- /dev/null +++ b/chartlets.js/packages/lib/src/api/fetchContributions.test.ts @@ -0,0 +1,35 @@ +import { describe, it, vi, expect, beforeEach, afterEach, 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 mockResponse = { + 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", + }); + const data = response.data; + + // Assertions + expect(fetch).toHaveBeenCalledOnce(); + expect(data).toEqual({ message: "Hello, world!" }); + }); +}); From d97eab018f7e8878d895a67d3af54dff12d49b41 Mon Sep 17 00:00:00 2001 From: Norman Fomferra Date: Thu, 12 Dec 2024 14:56:38 +0100 Subject: [PATCH 2/2] Added some more API tests --- .../lib/src/api/fetchCallback.test.ts | 88 +++++++++++++++++++ .../lib/src/api/fetchContributions.test.ts | 69 ++++++++++++++- .../packages/lib/src/api/fetchLayout.test.ts | 82 +++++++++++++++++ 3 files changed, 235 insertions(+), 4 deletions(-) create mode 100644 chartlets.js/packages/lib/src/api/fetchCallback.test.ts create mode 100644 chartlets.js/packages/lib/src/api/fetchLayout.test.ts 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 index bc843633..ea95b046 100644 --- a/chartlets.js/packages/lib/src/api/fetchContributions.test.ts +++ b/chartlets.js/packages/lib/src/api/fetchContributions.test.ts @@ -1,4 +1,12 @@ -import { describe, it, vi, expect, beforeEach, afterEach, Mock } from "vitest"; +import { + describe, + it, + vi, + expect, + beforeEach, + afterEach, + type Mock, +} from "vitest"; import { fetchContributions } from "./fetchContributions"; describe("fetchContributions", () => { @@ -14,8 +22,19 @@ describe("fetchContributions", () => { 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 = { - json: vi.fn().mockResolvedValue({ message: "Hello, world!" }), + ok: true, + status: 200, + statusText: "ok", + json: vi.fn().mockResolvedValue({ + result: expectedResult, + }), }; // Mock fetch to resolve with the mock response @@ -26,10 +45,52 @@ describe("fetchContributions", () => { serverUrl: "https://chartlets-test", endpointName: "api", }); - const data = response.data; // Assertions expect(fetch).toHaveBeenCalledOnce(); - expect(data).toEqual({ message: "Hello, world!" }); + 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", + }, + }); + }); +});