From d2699a275516af3fc43dfff13bf5b52061193171 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Tue, 16 Mar 2021 14:51:14 +0000 Subject: [PATCH] REST integration tests --- packages/server/__mocks__/node-fetch.js | 2 +- .../src/integrations/tests/rest.spec.js | 98 +++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/packages/server/__mocks__/node-fetch.js b/packages/server/__mocks__/node-fetch.js index 1113791ec2..334dc73cb6 100644 --- a/packages/server/__mocks__/node-fetch.js +++ b/packages/server/__mocks__/node-fetch.js @@ -1,6 +1,6 @@ const fetch = jest.requireActual("node-fetch") -module.exports = async (url, opts) => { +module.exports = async url , opts=> { // mocked data based on url if (url.includes("api/apps")) { return { diff --git a/packages/server/src/integrations/tests/rest.spec.js b/packages/server/src/integrations/tests/rest.spec.js index e69de29bb2..065d8263d1 100644 --- a/packages/server/src/integrations/tests/rest.spec.js +++ b/packages/server/src/integrations/tests/rest.spec.js @@ -0,0 +1,98 @@ +const fetch = require("node-fetch") +const RestIntegration = require("../rest") +jest.mock("node-fetch", () => jest.fn(() => ({ json: jest.fn() }))) + +class TestConfiguration { + constructor(config = {}) { + this.integration = new RestIntegration.integration(config) + } +} + +describe("REST Integration", () => { + const BASE_URL = "https://myapi.com" + let config + + beforeEach(() => { + config = new TestConfiguration({ + url: BASE_URL + }) + }) + + it("calls the create method with the correct params", async () => { + const query = { + path: "/api", + queryString: "?test=1", + headers: { + Accept: "application/json" + }, + json: { + name: "test" + } + } + const response = await config.integration.create(query) + expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, { + method: "POST", + body: "{\"name\":\"test\"}", + headers: { + Accept: "application/json" + } + }) + }) + + it("calls the read method with the correct params", async () => { + const query = { + path: "/api", + queryString: "?test=1", + headers: { + Accept: "application/json" + } + } + const response = await config.integration.read(query) + expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, { + headers: { + Accept: "application/json" + } + }) + }) + + it("calls the update method with the correct params", async () => { + const query = { + path: "/api", + queryString: "?test=1", + headers: { + Accept: "application/json" + }, + json: { + name: "test" + } + } + const response = await config.integration.update(query) + expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, { + method: "POST", + body: "{\"name\":\"test\"}", + headers: { + Accept: "application/json" + } + }) + }) + + it("calls the delete method with the correct params", async () => { + const query = { + path: "/api", + queryString: "?test=1", + headers: { + Accept: "application/json" + }, + json: { + name: "test" + } + } + const response = await config.integration.delete(query) + expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, { + method: "DELETE", + headers: { + Accept: "application/json" + } + }) + }) +}) \ No newline at end of file