From 37ed93c12191c8ad1bcb4931cc60dd27c7a2a130 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Thu, 11 Mar 2021 15:28:43 +0000 Subject: [PATCH 01/11] merge --- packages/server/src/integrations/tests/airtable.spec.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 packages/server/src/integrations/tests/airtable.spec.js diff --git a/packages/server/src/integrations/tests/airtable.spec.js b/packages/server/src/integrations/tests/airtable.spec.js new file mode 100644 index 0000000000..e69de29bb2 From ff5477f3012f2267b34941ea23debed9cce6f14a Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Fri, 12 Mar 2021 09:29:27 +0000 Subject: [PATCH 02/11] airtable tests --- packages/server/__mocks__/airtable.js | 15 +++++++ packages/server/src/integrations/airtable.js | 1 + .../integrations/tests/TestConfiguration.js | 0 .../src/integrations/tests/airtable.spec.js | 39 +++++++++++++++++++ .../src/integrations/tests/postgres.spec.js | 39 +++++++++++++++++++ 5 files changed, 94 insertions(+) create mode 100644 packages/server/__mocks__/airtable.js create mode 100644 packages/server/src/integrations/tests/TestConfiguration.js create mode 100644 packages/server/src/integrations/tests/postgres.spec.js diff --git a/packages/server/__mocks__/airtable.js b/packages/server/__mocks__/airtable.js new file mode 100644 index 0000000000..4a4d413978 --- /dev/null +++ b/packages/server/__mocks__/airtable.js @@ -0,0 +1,15 @@ +class Airtable { + constructor() {} + + base() { + return () => ({ + query: jest.fn(), + create: jest.fn(), + select: jest.fn(), + update: jest.fn(), + destroy: jest.fn(), + }) + } +} + +module.exports = Airtable diff --git a/packages/server/src/integrations/airtable.js b/packages/server/src/integrations/airtable.js index 37e552a7b8..02190fd5d5 100644 --- a/packages/server/src/integrations/airtable.js +++ b/packages/server/src/integrations/airtable.js @@ -66,6 +66,7 @@ class AirtableIntegration { constructor(config) { this.config = config this.client = new Airtable(config).base(config.base) + console.log(new Airtable().base()) } async create(query) { diff --git a/packages/server/src/integrations/tests/TestConfiguration.js b/packages/server/src/integrations/tests/TestConfiguration.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/server/src/integrations/tests/airtable.spec.js b/packages/server/src/integrations/tests/airtable.spec.js index e69de29bb2..ee3622db5e 100644 --- a/packages/server/src/integrations/tests/airtable.spec.js +++ b/packages/server/src/integrations/tests/airtable.spec.js @@ -0,0 +1,39 @@ +const Airtable = require("airtable") +const { ElectronHttpExecutor } = require("electron-updater/out/electronHttpExecutor") +const { integration } = require("../airtable") + +jest.mock("airtable") + +class TestConfiguration { + constructor(config = {}) { + this.integration = new integration(config) + } +} + +describe("Airtable Integration", () => { + let config + + beforeEach(() => { + config = new TestConfiguration() + }) + + it("calls the create method with the correct params", async () => { + const response = await config.integration.create({ + table: "test", + json: "" + }) + expect(Airtable.create).toHaveBeenCalledWith({}) + }) + + it("calls the read method with the correct params", () => { + + }) + + it("calls the update method with the correct params", () => { + + }) + + it("calls the delete method with the correct params", () => { + + }) +}) \ No newline at end of file diff --git a/packages/server/src/integrations/tests/postgres.spec.js b/packages/server/src/integrations/tests/postgres.spec.js new file mode 100644 index 0000000000..c991ead14f --- /dev/null +++ b/packages/server/src/integrations/tests/postgres.spec.js @@ -0,0 +1,39 @@ + +const Airtable = require("airtable") +const { ElectronHttpExecutor } = require("electron-updater/out/electronHttpExecutor") +const AirtableIntegration = require("../airtable") +jest.mock("airtable") + +class TestConfiguration { + constructor(config = {}) { + this.integration = new AirtableIntegration.integration(config) + } +} + +describe("Airtable Integration", () => { + let config + + beforeEach(() => { + config = new TestConfiguration() + }) + + it("calls the create method with the correct params", async () => { + const response = await config.integration.create({ + table: "test", + json: "" + }) + expect(config.integration.client.create).toHaveBeenCalledWith({}) + }) + + it("calls the read method with the correct params", () => { + + }) + + it("calls the update method with the correct params", () => { + + }) + + it("calls the delete method with the correct params", () => { + + }) +}) \ No newline at end of file From 53278c264812370815f78fffbc899537b1ea18d4 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 15 Mar 2021 16:07:04 +0000 Subject: [PATCH 03/11] postgres integration tests --- packages/server/__mocks__/airtable.js | 10 ++- packages/server/__mocks__/pg.js | 24 +++---- packages/server/src/integrations/airtable.js | 1 - .../src/integrations/tests/airtable.spec.js | 10 +-- .../src/integrations/tests/postgres.spec.js | 67 ++++++++++++++----- 5 files changed, 73 insertions(+), 39 deletions(-) diff --git a/packages/server/__mocks__/airtable.js b/packages/server/__mocks__/airtable.js index 4a4d413978..34a248ac7f 100644 --- a/packages/server/__mocks__/airtable.js +++ b/packages/server/__mocks__/airtable.js @@ -1,13 +1,11 @@ class Airtable { - constructor() {} + constructor() { + this.create = jest.fn() + } base() { return () => ({ - query: jest.fn(), - create: jest.fn(), - select: jest.fn(), - update: jest.fn(), - destroy: jest.fn(), + create: this.create, }) } } diff --git a/packages/server/__mocks__/pg.js b/packages/server/__mocks__/pg.js index 2bda8afad0..39da49aae0 100644 --- a/packages/server/__mocks__/pg.js +++ b/packages/server/__mocks__/pg.js @@ -1,20 +1,20 @@ const pg = {} // constructor -function Client() {} - -Client.prototype.query = async function() { - return { - rows: [ - { - a: "string", - b: 1, - }, - ], - } +function Client() { + this.query = jest.fn(() => ({ rows: [] })) } -Client.prototype.connect = async function() {} +Client.prototype.query = jest.fn(() => ({ + rows: [ + { + a: "string", + b: 1, + }, + ], +})) + +Client.prototype.connect = jest.fn() pg.Client = Client diff --git a/packages/server/src/integrations/airtable.js b/packages/server/src/integrations/airtable.js index 02190fd5d5..37e552a7b8 100644 --- a/packages/server/src/integrations/airtable.js +++ b/packages/server/src/integrations/airtable.js @@ -66,7 +66,6 @@ class AirtableIntegration { constructor(config) { this.config = config this.client = new Airtable(config).base(config.base) - console.log(new Airtable().base()) } async create(query) { diff --git a/packages/server/src/integrations/tests/airtable.spec.js b/packages/server/src/integrations/tests/airtable.spec.js index ee3622db5e..bc3c6e93e9 100644 --- a/packages/server/src/integrations/tests/airtable.spec.js +++ b/packages/server/src/integrations/tests/airtable.spec.js @@ -1,7 +1,6 @@ -const Airtable = require("airtable") const { ElectronHttpExecutor } = require("electron-updater/out/electronHttpExecutor") const { integration } = require("../airtable") - +const Airtable = require("airtable") jest.mock("airtable") class TestConfiguration { @@ -10,7 +9,7 @@ class TestConfiguration { } } -describe("Airtable Integration", () => { +xdescribe("Airtable Integration", () => { let config beforeEach(() => { @@ -20,9 +19,10 @@ describe("Airtable Integration", () => { it("calls the create method with the correct params", async () => { const response = await config.integration.create({ table: "test", - json: "" + json: {} }) - expect(Airtable.create).toHaveBeenCalledWith({}) + console.log(config.integration.client) + expect(config.integration.client.create).toHaveBeenCalledWith({}) }) it("calls the read method with the correct params", () => { diff --git a/packages/server/src/integrations/tests/postgres.spec.js b/packages/server/src/integrations/tests/postgres.spec.js index c991ead14f..572af2a740 100644 --- a/packages/server/src/integrations/tests/postgres.spec.js +++ b/packages/server/src/integrations/tests/postgres.spec.js @@ -1,16 +1,15 @@ -const Airtable = require("airtable") -const { ElectronHttpExecutor } = require("electron-updater/out/electronHttpExecutor") -const AirtableIntegration = require("../airtable") -jest.mock("airtable") +const pg = require("pg") +const PostgresIntegration = require("../postgres") +jest.mock("pg") class TestConfiguration { constructor(config = {}) { - this.integration = new AirtableIntegration.integration(config) + this.integration = new PostgresIntegration.integration(config) } } -describe("Airtable Integration", () => { +describe("Postgres Integration", () => { let config beforeEach(() => { @@ -18,22 +17,60 @@ describe("Airtable Integration", () => { }) it("calls the create method with the correct params", async () => { - const response = await config.integration.create({ - table: "test", - json: "" + const sql = "insert into users (name, age) values ('Joe', 123);" + const response = await config.integration.create({ + sql }) - expect(config.integration.client.create).toHaveBeenCalledWith({}) + expect(config.integration.client.query).toHaveBeenCalledWith(sql) }) - it("calls the read method with the correct params", () => { - + it("calls the read method with the correct params", async () => { + const sql = "select * from users;" + const response = await config.integration.read({ + sql + }) + expect(config.integration.client.query).toHaveBeenCalledWith(sql) }) - it("calls the update method with the correct params", () => { - + it("calls the update method with the correct params", async () => { + const sql = "update table users set name = 'test';" + const response = await config.integration.update({ + sql + }) + expect(config.integration.client.query).toHaveBeenCalledWith(sql) }) - it("calls the delete method with the correct params", () => { + it("calls the delete method with the correct params", async () => { + const sql = "delete from users where name = 'todelete';" + const response = await config.integration.delete({ + sql + }) + expect(config.integration.client.query).toHaveBeenCalledWith(sql) + }) + describe("no rows returned", () => { + 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({ + sql + }) + expect(response).toEqual([{ created: true }]) + }) + + it("returns the correct response when the update response has no rows", async () => { + const sql = "update table users set name = 'test';" + const response = await config.integration.update({ + sql + }) + expect(response).toEqual([{ updated: 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 From 813ea1ede8bd7c88258fa65d10760688cf193e23 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 15 Mar 2021 16:26:46 +0000 Subject: [PATCH 04/11] couchDB tests --- packages/server/__mocks__/mysql.js | 10 +++ packages/server/__mocks__/pg.js | 4 +- packages/server/__mocks__/pouchdb.js | 8 ++ .../src/integrations/tests/couchdb.spec.js | 61 +++++++++++++++ .../src/integrations/tests/mysql.spec.js | 75 +++++++++++++++++++ .../src/integrations/tests/postgres.spec.js | 5 +- 6 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 packages/server/__mocks__/mysql.js create mode 100644 packages/server/__mocks__/pouchdb.js create mode 100644 packages/server/src/integrations/tests/couchdb.spec.js create mode 100644 packages/server/src/integrations/tests/mysql.spec.js diff --git a/packages/server/__mocks__/mysql.js b/packages/server/__mocks__/mysql.js new file mode 100644 index 0000000000..03036e8ce8 --- /dev/null +++ b/packages/server/__mocks__/mysql.js @@ -0,0 +1,10 @@ +const mysql = {} + +const client = { + connect: jest.fn(), + query: jest.fn((sql, cb) => cb), +} + +mysql.createConnection = jest.fn(() => client) + +module.exports = mysql diff --git a/packages/server/__mocks__/pg.js b/packages/server/__mocks__/pg.js index 39da49aae0..0d8b8cc26a 100644 --- a/packages/server/__mocks__/pg.js +++ b/packages/server/__mocks__/pg.js @@ -1,9 +1,7 @@ const pg = {} // constructor -function Client() { - this.query = jest.fn(() => ({ rows: [] })) -} +function Client() {} Client.prototype.query = jest.fn(() => ({ rows: [ diff --git a/packages/server/__mocks__/pouchdb.js b/packages/server/__mocks__/pouchdb.js new file mode 100644 index 0000000000..bb63ca6446 --- /dev/null +++ b/packages/server/__mocks__/pouchdb.js @@ -0,0 +1,8 @@ +function CouchDB() { + this.post = jest.fn() + this.allDocs = jest.fn(() => ({ rows: [] })) + this.put = jest.fn() + this.remove = jest.fn() +} + +module.exports = CouchDB diff --git a/packages/server/src/integrations/tests/couchdb.spec.js b/packages/server/src/integrations/tests/couchdb.spec.js new file mode 100644 index 0000000000..fc84f4ace9 --- /dev/null +++ b/packages/server/src/integrations/tests/couchdb.spec.js @@ -0,0 +1,61 @@ +const PouchDB = require("pouchdb") +const CouchDBIntegration = require("../couchdb") +jest.mock("pouchdb") + +class TestConfiguration { + constructor(config = {}) { + this.integration = new CouchDBIntegration.integration(config) + } +} + +describe("CouchDB Integration", () => { + let config + + beforeEach(() => { + config = new TestConfiguration() + }) + + it("calls the create method with the correct params", async () => { + const doc = { + test: 1 + } + const response = await config.integration.create({ + json: doc + }) + expect(config.integration.client.post).toHaveBeenCalledWith(doc) + }) + + it("calls the read method with the correct params", async () => { + const doc = { + name: "search" + } + + const response = await config.integration.read({ + json: doc + }) + + expect(config.integration.client.allDocs).toHaveBeenCalledWith({ + include_docs: true, + name: "search" + }) + }) + + it("calls the update method with the correct params", async () => { + const doc = { + _id: "1234", + name: "search" + } + + const response = await config.integration.update({ + json: doc + }) + + expect(config.integration.client.put).toHaveBeenCalledWith(doc) + }) + + it("calls the delete method with the correct params", async () => { + const id = "1234" + const response = await config.integration.delete({ id }) + expect(config.integration.client.remove).toHaveBeenCalledWith(id) + }) +}) \ 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 new file mode 100644 index 0000000000..e275dbccfa --- /dev/null +++ b/packages/server/src/integrations/tests/mysql.spec.js @@ -0,0 +1,75 @@ +const pg = require("mysql") +const MySQLIntegration = require("../mysql") +jest.mock("mysql") + +class TestConfiguration { + constructor(config = { ssl: {} }) { + this.integration = new MySQLIntegration.integration(config) + } +} + +describe("MySQL Integration", () => { + let config + + beforeEach(() => { + config = new TestConfiguration() + }) + + 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 + }) + expect(config.integration.client.query).toHaveBeenCalledWith(sql) + }) + + it("calls the read method with the correct params", async () => { + const sql = "select * from users;" + const response = await config.integration.read({ + sql + }) + expect(config.integration.client.query).toHaveBeenCalledWith(sql) + }) + + it("calls the update method with the correct params", async () => { + const sql = "update table users set name = 'test';" + const response = await config.integration.update({ + sql + }) + expect(config.integration.client.query).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({ + sql + }) + expect(config.integration.client.query).toHaveBeenCalledWith(sql) + }) + + describe("no rows returned", () => { + 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({ + sql + }) + expect(response).toEqual([{ created: true }]) + }) + + it("returns the correct response when the update response has no rows", async () => { + const sql = "update table users set name = 'test';" + const response = await config.integration.update({ + sql + }) + expect(response).toEqual([{ updated: 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/postgres.spec.js b/packages/server/src/integrations/tests/postgres.spec.js index 572af2a740..8a8876a556 100644 --- a/packages/server/src/integrations/tests/postgres.spec.js +++ b/packages/server/src/integrations/tests/postgres.spec.js @@ -1,4 +1,3 @@ - const pg = require("pg") const PostgresIntegration = require("../postgres") jest.mock("pg") @@ -49,6 +48,10 @@ describe("Postgres Integration", () => { }) describe("no rows returned", () => { + beforeEach(() => { + config.integration.client.query.mockImplementation(() => ({ rows: [] })) + }) + 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({ From 6ba84b420c681b885d02d15115de5bda1b55e021 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 15 Mar 2021 19:45:39 +0000 Subject: [PATCH 05/11] dynamoDB tests --- packages/server/__mocks__/aws-sdk.js | 37 +++++++ packages/server/src/integrations/dynamodb.js | 2 +- .../src/integrations/tests/dynamodb.spec.js | 103 ++++++++++++++++++ 3 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 packages/server/__mocks__/aws-sdk.js create mode 100644 packages/server/src/integrations/tests/dynamodb.spec.js diff --git a/packages/server/__mocks__/aws-sdk.js b/packages/server/__mocks__/aws-sdk.js new file mode 100644 index 0000000000..49b430603b --- /dev/null +++ b/packages/server/__mocks__/aws-sdk.js @@ -0,0 +1,37 @@ +const aws = {} + +const response = body => () => ({ promise: () => body }) + +function DocumentClient() { + this.put = jest.fn(response({})) + this.query = jest.fn( + response({ + Items: [], + }) + ) + this.scan = jest.fn( + response({ + Items: [ + { + Name: "test", + }, + ], + }) + ) + this.get = jest.fn(response({})) + this.update = jest.fn(response({})) + this.delete = jest.fn(response({})) +} + +function S3() { + this.listObjects = jest.fn( + response({ + foo: {}, + }) + ) +} + +aws.DynamoDB = { DocumentClient } +aws.config = { update: jest.fn() } + +module.exports = aws diff --git a/packages/server/src/integrations/dynamodb.js b/packages/server/src/integrations/dynamodb.js index 668e11e263..4897690075 100644 --- a/packages/server/src/integrations/dynamodb.js +++ b/packages/server/src/integrations/dynamodb.js @@ -166,7 +166,7 @@ class DynamoDBIntegration { async update(query) { const params = { - TableName: query.Table, + TableName: query.table, ...query.json, } return this.client.update(params).promise() diff --git a/packages/server/src/integrations/tests/dynamodb.spec.js b/packages/server/src/integrations/tests/dynamodb.spec.js new file mode 100644 index 0000000000..4c6b931090 --- /dev/null +++ b/packages/server/src/integrations/tests/dynamodb.spec.js @@ -0,0 +1,103 @@ +const AWS = require("aws-sdk") +const DynamoDBIntegration = require("../dynamodb") +jest.mock("aws-sdk") + +class TestConfiguration { + constructor(config = {}) { + this.integration = new DynamoDBIntegration.integration(config) + } +} + +describe("DynamoDB Integration", () => { + let config + let tableName = "Users" + + beforeEach(() => { + config = new TestConfiguration() + }) + + it("calls the create method with the correct params", async () => { + const response = await config.integration.create({ + table: tableName, + json: { + Name: "John" + } + }) + expect(config.integration.client.put).toHaveBeenCalledWith({ + TableName: tableName, + Name: "John" + }) + }) + + it("calls the read method with the correct params", async () => { + const indexName = "Test" + + const response = await config.integration.read({ + table: tableName, + index: indexName, + json: {} + }) + expect(config.integration.client.query).toHaveBeenCalledWith({ + TableName: tableName, + IndexName: indexName, + }) + expect(response).toEqual([]) + }) + + it("calls the scan method with the correct params", async () => { + const indexName = "Test" + + const response = await config.integration.scan({ + table: tableName, + index: indexName, + json: {} + }) + expect(config.integration.client.scan).toHaveBeenCalledWith({ + TableName: tableName, + IndexName: indexName, + }) + expect(response).toEqual([{ + Name: "test" + }]) + }) + + it("calls the get method with the correct params", async () => { + const response = await config.integration.get({ + table: tableName, + json: { + Id: 123 + } + }) + + expect(config.integration.client.get).toHaveBeenCalledWith({ + TableName: tableName, + Id: 123 + }) + }) + + it("calls the update method with the correct params", async () => { + const response = await config.integration.update({ + table: tableName, + json: { + Name: "John" + } + }) + expect(config.integration.client.update).toHaveBeenCalledWith({ + TableName: tableName, + Name: "John" + }) + }) + + it("calls the delete method with the correct params", async () => { + const response = await config.integration.delete({ + table: tableName, + json: { + Name: "John" + } + }) + expect(config.integration.client.delete).toHaveBeenCalledWith({ + TableName: tableName, + Name: "John" + }) + }) +}) \ No newline at end of file From 8fa368585221db63beb6e90a6d0d8b115b712a23 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Tue, 16 Mar 2021 11:46:13 +0000 Subject: [PATCH 06/11] elasticsearch tests --- .../__mocks__/@elastic/elasticsearch.js | 24 ++++++ .../integrations/tests/elasticsearch.spec.js | 81 +++++++++++++++++++ .../src/integrations/tests/mysql.spec.js | 2 +- 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 packages/server/__mocks__/@elastic/elasticsearch.js create mode 100644 packages/server/src/integrations/tests/elasticsearch.spec.js diff --git a/packages/server/__mocks__/@elastic/elasticsearch.js b/packages/server/__mocks__/@elastic/elasticsearch.js new file mode 100644 index 0000000000..c6b2bad48a --- /dev/null +++ b/packages/server/__mocks__/@elastic/elasticsearch.js @@ -0,0 +1,24 @@ +const elastic = {} + +elastic.Client = function() { + this.index = jest.fn().mockResolvedValue({ body: [] }) + this.search = jest.fn().mockResolvedValue({ + body: { + hits: { + hits: [ + { + _source: { + name: "test", + }, + }, + ], + }, + }, + }) + this.update = jest.fn().mockResolvedValue({ body: [] }) + this.delete = jest.fn().mockResolvedValue({ body: [] }) + + this.close = jest.fn() +} + +module.exports = elastic diff --git a/packages/server/src/integrations/tests/elasticsearch.spec.js b/packages/server/src/integrations/tests/elasticsearch.spec.js new file mode 100644 index 0000000000..fc97e04bcc --- /dev/null +++ b/packages/server/src/integrations/tests/elasticsearch.spec.js @@ -0,0 +1,81 @@ +const elasticsearch = require("@elastic/elasticsearch") +const ElasticSearchIntegration = require("../elasticsearch") +jest.mock("@elastic/elasticsearch") + +class TestConfiguration { + constructor(config = {}) { + this.integration = new ElasticSearchIntegration.integration(config) + } +} + +describe("Elasticsearch Integration", () => { + let config + let indexName = "Users" + + beforeEach(() => { + config = new TestConfiguration() + }) + + it("calls the create method with the correct params", async () => { + const body = { + name: "Hello" + } + const response = await config.integration.create({ + index: indexName, + json: body + }) + expect(config.integration.client.index).toHaveBeenCalledWith({ + index: indexName, + body + }) + }) + + it("calls the read method with the correct params", async () => { + const body = { + query: { + term: { + name: "kimchy" + } + } + } + const response = await config.integration.read({ + index: indexName, + json: body + }) + expect(config.integration.client.search).toHaveBeenCalledWith({ + index: indexName, + body + }) + expect(response).toEqual(expect.any(Array)) + }) + + it("calls the update method with the correct params", async () => { + const body = { + name: "updated" + } + + const response = await config.integration.update({ + id: "1234", + index: indexName, + json: body + }) + + expect(config.integration.client.update).toHaveBeenCalledWith({ + id: "1234", + index: indexName, + body + }) + expect(response).toEqual(expect.any(Array)) + }) + + it("calls the delete method with the correct params", async () => { + const body = { + id: "1234" + } + + const response = await config.integration.delete(body) + + expect(config.integration.client.delete).toHaveBeenCalledWith(body) + expect(response).toEqual(expect.any(Array)) + }) +}) \ 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 e275dbccfa..e69ae7083d 100644 --- a/packages/server/src/integrations/tests/mysql.spec.js +++ b/packages/server/src/integrations/tests/mysql.spec.js @@ -8,7 +8,7 @@ class TestConfiguration { } } -describe("MySQL Integration", () => { +xdescribe("MySQL Integration", () => { let config beforeEach(() => { From 2da1d27ba63d848af6e47ce29384cb24008c7181 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Tue, 16 Mar 2021 13:54:39 +0000 Subject: [PATCH 07/11] arangodb tests --- packages/server/__mocks__/arangojs.js | 21 +++++++ packages/server/__mocks__/aws-sdk.js | 3 +- packages/server/__mocks__/mongodb.js | 19 +++++++ packages/server/__mocks__/mssql.js | 14 +++++ .../src/integrations/tests/arangodb.spec.js | 35 ++++++++++++ .../tests/microsoftSqlServer.spec.js | 55 +++++++++++++++++++ .../src/integrations/tests/mongo.spec.js | 40 ++++++++++++++ .../src/integrations/tests/rest.spec.js | 0 .../server/src/integrations/tests/s3.spec.js | 26 +++++++++ .../middleware/tests/authenticated.spec.js | 1 + 10 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 packages/server/__mocks__/arangojs.js create mode 100644 packages/server/__mocks__/mongodb.js create mode 100644 packages/server/__mocks__/mssql.js create mode 100644 packages/server/src/integrations/tests/arangodb.spec.js create mode 100644 packages/server/src/integrations/tests/microsoftSqlServer.spec.js create mode 100644 packages/server/src/integrations/tests/mongo.spec.js create mode 100644 packages/server/src/integrations/tests/rest.spec.js create mode 100644 packages/server/src/integrations/tests/s3.spec.js diff --git a/packages/server/__mocks__/arangojs.js b/packages/server/__mocks__/arangojs.js new file mode 100644 index 0000000000..1a40529ca0 --- /dev/null +++ b/packages/server/__mocks__/arangojs.js @@ -0,0 +1,21 @@ +const arangodb = {} + +arangodb.Database = function() { + this.query = jest.fn(() => ({ + all: jest.fn(), + })) + this.collection = jest.fn(() => "collection") + this.close = jest.fn() +} + +arangodb.aql = (strings, ...args) => { + let str = strings.join("{}") + + for (let arg of args) { + str = str.replace("{}", arg) + } + + return str +} + +module.exports = arangodb diff --git a/packages/server/__mocks__/aws-sdk.js b/packages/server/__mocks__/aws-sdk.js index 49b430603b..503d098256 100644 --- a/packages/server/__mocks__/aws-sdk.js +++ b/packages/server/__mocks__/aws-sdk.js @@ -26,12 +26,13 @@ function DocumentClient() { function S3() { this.listObjects = jest.fn( response({ - foo: {}, + Contents: {}, }) ) } aws.DynamoDB = { DocumentClient } +aws.S3 = S3 aws.config = { update: jest.fn() } module.exports = aws diff --git a/packages/server/__mocks__/mongodb.js b/packages/server/__mocks__/mongodb.js new file mode 100644 index 0000000000..160ca89ebe --- /dev/null +++ b/packages/server/__mocks__/mongodb.js @@ -0,0 +1,19 @@ +const mongodb = {} + +mongodb.MongoClient = function() { + this.connect = jest.fn() + this.close = jest.fn() + this.insertOne = jest.fn() + this.find = jest.fn(() => ({ toArray: () => [] })) + + this.collection = jest.fn(() => ({ + insertOne: this.insertOne, + find: this.find, + })) + + this.db = () => ({ + collection: this.collection, + }) +} + +module.exports = mongodb diff --git a/packages/server/__mocks__/mssql.js b/packages/server/__mocks__/mssql.js new file mode 100644 index 0000000000..6119c014da --- /dev/null +++ b/packages/server/__mocks__/mssql.js @@ -0,0 +1,14 @@ +const mssql = {} + +mssql.query = jest.fn(() => ({ + recordset: [ + { + a: "string", + b: 1, + }, + ], +})) + +mssql.connect = jest.fn(() => ({ recordset: [] })) + +module.exports = mssql diff --git a/packages/server/src/integrations/tests/arangodb.spec.js b/packages/server/src/integrations/tests/arangodb.spec.js new file mode 100644 index 0000000000..437a7fd3ec --- /dev/null +++ b/packages/server/src/integrations/tests/arangodb.spec.js @@ -0,0 +1,35 @@ +const arangodb = require("arangojs") +const ArangoDBIntegration = require("../arangodb") +jest.mock("arangojs") + +class TestConfiguration { + constructor(config = {}) { + this.integration = new ArangoDBIntegration.integration(config) + } +} + +describe("ArangoDB Integration", () => { + let config + let indexName = "Users" + + beforeEach(() => { + config = new TestConfiguration() + }) + + it("calls the create method with the correct params", async () => { + const body = { + json: "Hello" + } + + const response = await config.integration.create(body) + expect(config.integration.client.query).toHaveBeenCalledWith(`INSERT Hello INTO collection RETURN NEW`) + }) + + it("calls the read method with the correct params", async () => { + const query = { + json: `test`, + } + const response = await config.integration.read(query) + expect(config.integration.client.query).toHaveBeenCalledWith(query.sql) + }) +}) \ 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 new file mode 100644 index 0000000000..6b0fd62000 --- /dev/null +++ b/packages/server/src/integrations/tests/microsoftSqlServer.spec.js @@ -0,0 +1,55 @@ +const sqlServer = require("mssql") +const MSSQLIntegration = require("../microsoftSqlServer") +jest.mock("mssql") + +class TestConfiguration { + constructor(config = {}) { + this.integration = new MSSQLIntegration.integration(config) + } +} + +describe("MS SQL Server Integration", () => { + let config + + beforeEach(() => { + config = new TestConfiguration() + }) + + 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 + }) + expect(config.integration.client.query).toHaveBeenCalledWith(sql) + }) + + it("calls the read method with the correct params", async () => { + const sql = "select * from users;" + const response = await config.integration.read({ + sql + }) + expect(config.integration.client.query).toHaveBeenCalledWith(sql) + }) + + describe("no rows returned", () => { + beforeEach(() => { + config.integration.client.query.mockImplementation(() => ({ rows: [] })) + }) + + 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({ + sql + }) + 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/mongo.spec.js b/packages/server/src/integrations/tests/mongo.spec.js new file mode 100644 index 0000000000..1e37d5dd70 --- /dev/null +++ b/packages/server/src/integrations/tests/mongo.spec.js @@ -0,0 +1,40 @@ +const mongo = require("mongodb") +const MongoDBIntegration = require("../mongodb") +jest.mock("mongodb") + +class TestConfiguration { + constructor(config = {}) { + this.integration = new MongoDBIntegration.integration(config) + } +} + +describe("MongoDB Integration", () => { + let config + let indexName = "Users" + + beforeEach(() => { + config = new TestConfiguration() + }) + + it("calls the create method with the correct params", async () => { + const body = { + name: "Hello" + } + const response = await config.integration.create({ + index: indexName, + json: body + }) + expect(config.integration.client.insertOne).toHaveBeenCalledWith(body) + }) + + it("calls the read method with the correct params", async () => { + const query = { + json: { + address: "test" + } + } + const response = await config.integration.read(query) + expect(config.integration.client.find).toHaveBeenCalledWith(query.json) + expect(response).toEqual(expect.any(Array)) + }) +}) \ No newline at end of file diff --git a/packages/server/src/integrations/tests/rest.spec.js b/packages/server/src/integrations/tests/rest.spec.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/server/src/integrations/tests/s3.spec.js b/packages/server/src/integrations/tests/s3.spec.js new file mode 100644 index 0000000000..7ac403dbd4 --- /dev/null +++ b/packages/server/src/integrations/tests/s3.spec.js @@ -0,0 +1,26 @@ +const AWS = require("aws-sdk") +const S3Integration = require("../s3") +jest.mock("aws-sdk") + +class TestConfiguration { + constructor(config = {}) { + this.integration = new S3Integration.integration(config) + } +} + +describe("S3 Integration", () => { + let config + + beforeEach(() => { + config = new TestConfiguration() + }) + + it("calls the read method with the correct params", async () => { + const response = await config.integration.read({ + bucket: "test" + }) + expect(config.integration.client.listObjects).toHaveBeenCalledWith({ + Bucket: "test" + }) + }) +}) \ No newline at end of file diff --git a/packages/server/src/middleware/tests/authenticated.spec.js b/packages/server/src/middleware/tests/authenticated.spec.js index fe7e592528..26c9e3f15d 100644 --- a/packages/server/src/middleware/tests/authenticated.spec.js +++ b/packages/server/src/middleware/tests/authenticated.spec.js @@ -2,6 +2,7 @@ const { AuthTypes } = require("../../constants") const authenticatedMiddleware = require("../authenticated") const jwt = require("jsonwebtoken") jest.mock("jsonwebtoken") +jest.dontMock("pouchdb") class TestConfiguration { constructor(middleware) { From 4ab88daf28b706b3af312dcd5cb6c5d56c8436d1 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Tue, 16 Mar 2021 14:51:14 +0000 Subject: [PATCH 08/11] 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 From d37283addad7b78d0218b0a93f164f6403dda257 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Tue, 16 Mar 2021 18:43:56 +0000 Subject: [PATCH 09/11] airtable tests --- packages/server/__mocks__/airtable.js | 12 +--- packages/server/__mocks__/mysql.js | 2 +- .../src/integrations/microsoftSqlServer.js | 2 +- packages/server/src/integrations/mysql.js | 16 ++++-- .../src/integrations/tests/airtable.spec.js | 55 +++++++++++++++---- .../tests/microsoftSqlServer.spec.js | 8 --- .../src/integrations/tests/mysql.spec.js | 7 ++- 7 files changed, 61 insertions(+), 41 deletions(-) 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 () => { From 6698641182ad7a9b39a891b76a7c0221f3657418 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Tue, 16 Mar 2021 19:01:51 +0000 Subject: [PATCH 10/11] 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 00c1bb93e5..2b4df3e44b 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 3ff6dbb9a6..c505c4fc14 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 7abe1d0e99..eca3e523b0 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({ From b7a230065b7df8212cc6dee8ca09461ccf1e44ea Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Tue, 16 Mar 2021 19:27:18 +0000 Subject: [PATCH 11/11] more branch cov --- packages/server/__mocks__/node-fetch.js | 2 +- packages/server/{__mocks__ => build}/pouchdb.js | 1 + packages/server/src/integrations/tests/couchdb.spec.js | 9 ++++++++- packages/server/src/integrations/tests/rest.spec.js | 6 +++--- .../server/src/middleware/tests/authenticated.spec.js | 1 - 5 files changed, 13 insertions(+), 6 deletions(-) rename packages/server/{__mocks__ => build}/pouchdb.js (86%) diff --git a/packages/server/__mocks__/node-fetch.js b/packages/server/__mocks__/node-fetch.js index 334dc73cb6..1113791ec2 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/__mocks__/pouchdb.js b/packages/server/build/pouchdb.js similarity index 86% rename from packages/server/__mocks__/pouchdb.js rename to packages/server/build/pouchdb.js index bb63ca6446..16942a7a86 100644 --- a/packages/server/__mocks__/pouchdb.js +++ b/packages/server/build/pouchdb.js @@ -3,6 +3,7 @@ function CouchDB() { this.allDocs = jest.fn(() => ({ rows: [] })) this.put = jest.fn() this.remove = jest.fn() + this.plugin = jest.fn() } module.exports = CouchDB diff --git a/packages/server/src/integrations/tests/couchdb.spec.js b/packages/server/src/integrations/tests/couchdb.spec.js index fc84f4ace9..cfe522c617 100644 --- a/packages/server/src/integrations/tests/couchdb.spec.js +++ b/packages/server/src/integrations/tests/couchdb.spec.js @@ -1,6 +1,13 @@ const PouchDB = require("pouchdb") const CouchDBIntegration = require("../couchdb") -jest.mock("pouchdb") +jest.mock("pouchdb", () => function CouchDBMock() { + this.post = jest.fn() + this.allDocs = jest.fn(() => ({ rows: [] })) + this.put = jest.fn() + this.remove = jest.fn() + this.plugin = jest.fn() +}) + class TestConfiguration { constructor(config = {}) { diff --git a/packages/server/src/integrations/tests/rest.spec.js b/packages/server/src/integrations/tests/rest.spec.js index 065d8263d1..db8749baa1 100644 --- a/packages/server/src/integrations/tests/rest.spec.js +++ b/packages/server/src/integrations/tests/rest.spec.js @@ -1,6 +1,6 @@ const fetch = require("node-fetch") const RestIntegration = require("../rest") -jest.mock("node-fetch", () => jest.fn(() => ({ json: jest.fn() }))) +jest.mock("node-fetch", () => jest.fn(() => ({ json: jest.fn(), text: jest.fn() }))) class TestConfiguration { constructor(config = {}) { @@ -44,13 +44,13 @@ describe("REST Integration", () => { path: "/api", queryString: "?test=1", headers: { - Accept: "application/json" + Accept: "text/html" } } const response = await config.integration.read(query) expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, { headers: { - Accept: "application/json" + Accept: "text/html" } }) }) diff --git a/packages/server/src/middleware/tests/authenticated.spec.js b/packages/server/src/middleware/tests/authenticated.spec.js index 26c9e3f15d..fe7e592528 100644 --- a/packages/server/src/middleware/tests/authenticated.spec.js +++ b/packages/server/src/middleware/tests/authenticated.spec.js @@ -2,7 +2,6 @@ const { AuthTypes } = require("../../constants") const authenticatedMiddleware = require("../authenticated") const jwt = require("jsonwebtoken") jest.mock("jsonwebtoken") -jest.dontMock("pouchdb") class TestConfiguration { constructor(middleware) {