From d5ae0fa2a9beefd8c224ca8d2c5ba1367dc2748c Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 15 Mar 2021 19:45:39 +0000 Subject: [PATCH] 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