From 8c8ac18ac0a3fa2774e38b5b889675f51f258ffe Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 21 May 2021 16:02:21 +0100 Subject: [PATCH] Fixing broken server test cases. --- packages/server/__mocks__/pg.js | 18 ++++++++++++++---- .../src/api/routes/tests/application.spec.js | 3 ++- .../routes/tests/utilities/TestFunctions.js | 3 ++- .../src/integrations/tests/postgres.spec.js | 12 ++++++------ 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/packages/server/__mocks__/pg.js b/packages/server/__mocks__/pg.js index 0d8b8cc26a..5d4e3793fd 100644 --- a/packages/server/__mocks__/pg.js +++ b/packages/server/__mocks__/pg.js @@ -1,9 +1,6 @@ const pg = {} -// constructor -function Client() {} - -Client.prototype.query = jest.fn(() => ({ +const query = jest.fn(() => ({ rows: [ { a: "string", @@ -12,8 +9,21 @@ Client.prototype.query = jest.fn(() => ({ ], })) +// constructor +function Client() {} + +Client.prototype.query = query Client.prototype.connect = jest.fn() +Client.prototype.release = jest.fn() + +function Pool() {} +Pool.prototype.query = query +Pool.prototype.connect = jest.fn(() => { + return new Client() +}) pg.Client = Client +pg.Pool = Pool +pg.queryMock = query module.exports = pg diff --git a/packages/server/src/api/routes/tests/application.spec.js b/packages/server/src/api/routes/tests/application.spec.js index 692a5b8e84..dc550cd237 100644 --- a/packages/server/src/api/routes/tests/application.spec.js +++ b/packages/server/src/api/routes/tests/application.spec.js @@ -1,5 +1,6 @@ const { clearAllApps, checkBuilderEndpoint } = require("./utilities/TestFunctions") const setup = require("./utilities") +const { AppStatus } = require("../../../db/utils") jest.mock("../../../utilities/redis", () => ({ init: jest.fn(), @@ -52,7 +53,7 @@ describe("/applications", () => { await config.createApp(request, "app2") const res = await request - .get("/api/applications?status=dev") + .get(`/api/applications?status=${AppStatus.DEV}`) .set(config.defaultHeaders()) .expect('Content-Type', /json/) .expect(200) diff --git a/packages/server/src/api/routes/tests/utilities/TestFunctions.js b/packages/server/src/api/routes/tests/utilities/TestFunctions.js index f10989bf56..c49e44c949 100644 --- a/packages/server/src/api/routes/tests/utilities/TestFunctions.js +++ b/packages/server/src/api/routes/tests/utilities/TestFunctions.js @@ -1,6 +1,7 @@ const rowController = require("../../../controllers/row") const appController = require("../../../controllers/application") const CouchDB = require("../../../../db") +const { AppStatus } = require("../../../../db/utils") function Request(appId, params) { this.appId = appId @@ -14,7 +15,7 @@ exports.getAllTableRows = async config => { } exports.clearAllApps = async () => { - const req = { query: { status: "dev" } } + const req = { query: { status: AppStatus.DEV } } await appController.fetch(req) const apps = req.body if (!apps || apps.length <= 0) { diff --git a/packages/server/src/integrations/tests/postgres.spec.js b/packages/server/src/integrations/tests/postgres.spec.js index 8a8876a556..dc34fcf6bb 100644 --- a/packages/server/src/integrations/tests/postgres.spec.js +++ b/packages/server/src/integrations/tests/postgres.spec.js @@ -20,7 +20,7 @@ describe("Postgres Integration", () => { const response = await config.integration.create({ sql }) - expect(config.integration.client.query).toHaveBeenCalledWith(sql) + expect(pg.queryMock).toHaveBeenCalledWith(sql) }) it("calls the read method with the correct params", async () => { @@ -28,7 +28,7 @@ describe("Postgres Integration", () => { const response = await config.integration.read({ sql }) - expect(config.integration.client.query).toHaveBeenCalledWith(sql) + expect(pg.queryMock).toHaveBeenCalledWith(sql) }) it("calls the update method with the correct params", async () => { @@ -36,20 +36,20 @@ describe("Postgres Integration", () => { const response = await config.integration.update({ sql }) - expect(config.integration.client.query).toHaveBeenCalledWith(sql) + expect(pg.queryMock).toHaveBeenCalledWith(sql) }) it("calls the delete method with the correct params", async () => { const sql = "delete from users where name = 'todelete';" - const response = await config.integration.delete({ + await config.integration.delete({ sql }) - expect(config.integration.client.query).toHaveBeenCalledWith(sql) + expect(pg.queryMock).toHaveBeenCalledWith(sql) }) describe("no rows returned", () => { beforeEach(() => { - config.integration.client.query.mockImplementation(() => ({ rows: [] })) + pg.queryMock.mockImplementation(() => ({ rows: [] })) }) it("returns the correct response when the create response has no rows", async () => {