From c0e9ee282aa280478e1e4c87d8ee2556b9534dc1 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Tue, 16 Mar 2021 19:01:51 +0000 Subject: [PATCH] mysql tests --- packages/server/__mocks__/mysql.js | 2 +- packages/server/src/integrations/mysql.js | 7 +++---- .../src/integrations/tests/mysql.spec.js | 19 +++++++++++++------ 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/packages/server/__mocks__/mysql.js b/packages/server/__mocks__/mysql.js index 00c1bb93e..2b4df3e44 100644 --- a/packages/server/__mocks__/mysql.js +++ b/packages/server/__mocks__/mysql.js @@ -2,7 +2,7 @@ const mysql = {} const client = { connect: jest.fn(), - query: jest.fn(console.log), + query: jest.fn(), } mysql.createConnection = jest.fn(() => client) diff --git a/packages/server/src/integrations/mysql.js b/packages/server/src/integrations/mysql.js index 3ff6dbb9a..c505c4fc1 100644 --- a/packages/server/src/integrations/mysql.js +++ b/packages/server/src/integrations/mysql.js @@ -65,7 +65,6 @@ class MySQLIntegration { // Node MySQL is callback based, so we must wrap our call in a promise return new Promise((resolve, reject) => { this.client.connect() - console.log(this.client.query()) return this.client.query(query.sql, (error, results) => { if (error) return reject(error) resolve(results) @@ -76,7 +75,7 @@ class MySQLIntegration { async create(query) { const results = await this.query(query) - return results.length ? results : { created: true } + return results.length ? results : [{ created: true }] } read(query) { @@ -85,12 +84,12 @@ class MySQLIntegration { async update(query) { const results = await this.query(query) - return results.length ? results : { updated: true } + return results.length ? results : [{ updated: true }] } async delete(query) { const results = await this.query(query) - return results.length ? results : { deleted: true } + return results.length ? results : [{ deleted: true }] } } diff --git a/packages/server/src/integrations/tests/mysql.spec.js b/packages/server/src/integrations/tests/mysql.spec.js index 7abe1d0e9..eca3e523b 100644 --- a/packages/server/src/integrations/tests/mysql.spec.js +++ b/packages/server/src/integrations/tests/mysql.spec.js @@ -5,6 +5,8 @@ jest.mock("mysql") class TestConfiguration { constructor(config = { ssl: {} }) { this.integration = new MySQLIntegration.integration(config) + this.query = jest.fn(() => [{ id: 1 }]) + this.integration.query = this.query } } @@ -15,13 +17,12 @@ describe("MySQL Integration", () => { config = new TestConfiguration() }) - fit("calls the create method with the correct params", async () => { + it("calls the create method with the correct params", async () => { const sql = "insert into users (name, age) values ('Joe', 123);" const response = await config.integration.create({ sql }) - console.log(response) - expect(config.integration.client.query).resolves.toHaveBeenCalledWith(sql) + expect(config.query).toHaveBeenCalledWith({ sql }) }) it("calls the read method with the correct params", async () => { @@ -29,7 +30,9 @@ describe("MySQL Integration", () => { const response = await config.integration.read({ sql }) - expect(config.integration.client.query).toHaveBeenCalledWith(sql) + expect(config.query).toHaveBeenCalledWith({ + sql + }) }) it("calls the update method with the correct params", async () => { @@ -37,7 +40,7 @@ describe("MySQL Integration", () => { const response = await config.integration.update({ sql }) - expect(config.integration.client.query).toHaveBeenCalledWith(sql) + expect(config.query).toHaveBeenCalledWith({ sql }) }) it("calls the delete method with the correct params", async () => { @@ -45,10 +48,14 @@ describe("MySQL Integration", () => { const response = await config.integration.delete({ sql }) - expect(config.integration.client.query).toHaveBeenCalledWith(sql) + expect(config.query).toHaveBeenCalledWith({ sql }) }) describe("no rows returned", () => { + beforeEach(() => { + config.query.mockImplementation(() => []) + }) + it("returns the correct response when the create response has no rows", async () => { const sql = "insert into users (name, age) values ('Joe', 123);" const response = await config.integration.create({