diff --git a/packages/server/__mocks__/airtable.js b/packages/server/__mocks__/airtable.js index 34a248ac7f..f9b3a1b35d 100644 --- a/packages/server/__mocks__/airtable.js +++ b/packages/server/__mocks__/airtable.js @@ -1,13 +1,5 @@ -class Airtable { - constructor() { - this.create = jest.fn() - } - - base() { - return () => ({ - create: this.create, - }) - } +function Airtable() { + this.base = jest.fn() } module.exports = Airtable diff --git a/packages/server/__mocks__/mysql.js b/packages/server/__mocks__/mysql.js index 03036e8ce8..00c1bb93e5 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((sql, cb) => cb), + query: jest.fn(console.log), } mysql.createConnection = jest.fn(() => client) diff --git a/packages/server/src/integrations/microsoftSqlServer.js b/packages/server/src/integrations/microsoftSqlServer.js index eea67a7256..f5e30fd65b 100644 --- a/packages/server/src/integrations/microsoftSqlServer.js +++ b/packages/server/src/integrations/microsoftSqlServer.js @@ -65,7 +65,7 @@ class SqlServerIntegration { try { await this.connect() const response = await this.client.query(query.sql) - return response.recordset + return response.recordset || [{ created: true }] } catch (err) { console.error("Error querying MS SQL Server", err) throw err diff --git a/packages/server/src/integrations/mysql.js b/packages/server/src/integrations/mysql.js index af1a1baf92..3ff6dbb9a6 100644 --- a/packages/server/src/integrations/mysql.js +++ b/packages/server/src/integrations/mysql.js @@ -65,6 +65,7 @@ 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) @@ -73,20 +74,23 @@ class MySQLIntegration { }) } - create(query) { - return this.query(query) + async create(query) { + const results = await this.query(query) + return results.length ? results : { created: true } } read(query) { return this.query(query) } - update(query) { - return this.query(query) + async update(query) { + const results = await this.query(query) + return results.length ? results : { updated: true } } - delete(query) { - return this.query(query) + async delete(query) { + const results = await this.query(query) + return results.length ? results : { deleted: true } } } diff --git a/packages/server/src/integrations/tests/airtable.spec.js b/packages/server/src/integrations/tests/airtable.spec.js index bc3c6e93e9..e6654f6f71 100644 --- a/packages/server/src/integrations/tests/airtable.spec.js +++ b/packages/server/src/integrations/tests/airtable.spec.js @@ -1,15 +1,21 @@ -const { ElectronHttpExecutor } = require("electron-updater/out/electronHttpExecutor") -const { integration } = require("../airtable") const Airtable = require("airtable") +const AirtableIntegration = require("../airtable") jest.mock("airtable") class TestConfiguration { constructor(config = {}) { - this.integration = new integration(config) + this.integration = new AirtableIntegration.integration(config) + this.client = { + create: jest.fn(), + select: jest.fn(), + update: jest.fn(), + destroy: jest.fn(), + } + this.integration.client = () => this.client } } -xdescribe("Airtable Integration", () => { +describe("Airtable Integration", () => { let config beforeEach(() => { @@ -21,19 +27,44 @@ xdescribe("Airtable Integration", () => { table: "test", json: {} }) - console.log(config.integration.client) - expect(config.integration.client.create).toHaveBeenCalledWith({}) + expect(config.client.create).toHaveBeenCalledWith([ + { + fields: {} + } + ]) }) - it("calls the read method with the correct params", () => { - + it("calls the read method with the correct params", async () => { + const response = await config.integration.read({ + table: "test", + view: "Grid view" + }) + expect(config.client.select).toHaveBeenCalledWith({ + maxRecords: 10, view: "Grid view" + }) }) - it("calls the update method with the correct params", () => { - + it("calls the update method with the correct params", async () => { + const response = await config.integration.update({ + table: "test", + id: "123", + json: { + name: "test" + } + }) + expect(config.client.update).toHaveBeenCalledWith([ + { + id: "123", + fields: { name: "test" } + } + ]) }) - it("calls the delete method with the correct params", () => { - + it("calls the delete method with the correct params", async () => { + const ids = [1,2,3,4] + const response = await config.integration.delete({ + ids + }) + expect(config.client.destroy).toHaveBeenCalledWith(ids) }) }) \ No newline at end of file diff --git a/packages/server/src/integrations/tests/microsoftSqlServer.spec.js b/packages/server/src/integrations/tests/microsoftSqlServer.spec.js index 6b0fd62000..29399b840f 100644 --- a/packages/server/src/integrations/tests/microsoftSqlServer.spec.js +++ b/packages/server/src/integrations/tests/microsoftSqlServer.spec.js @@ -43,13 +43,5 @@ describe("MS SQL Server Integration", () => { }) expect(response).toEqual([{ created: true }]) }) - - it("returns the correct response when the delete response has no rows", async () => { - const sql = "delete from users where name = 'todelete';" - const response = await config.integration.delete({ - sql - }) - expect(response).toEqual([{ deleted: true }]) - }) }) }) \ No newline at end of file diff --git a/packages/server/src/integrations/tests/mysql.spec.js b/packages/server/src/integrations/tests/mysql.spec.js index e69ae7083d..7abe1d0e99 100644 --- a/packages/server/src/integrations/tests/mysql.spec.js +++ b/packages/server/src/integrations/tests/mysql.spec.js @@ -8,19 +8,20 @@ class TestConfiguration { } } -xdescribe("MySQL Integration", () => { +describe("MySQL Integration", () => { let config beforeEach(() => { config = new TestConfiguration() }) - it("calls the create method with the correct params", async () => { + fit("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 }) - expect(config.integration.client.query).toHaveBeenCalledWith(sql) + console.log(response) + expect(config.integration.client.query).resolves.toHaveBeenCalledWith(sql) }) it("calls the read method with the correct params", async () => {