Skip to content

Commit

Permalink
Merge pull request #74 from ScottLogic/SDE-170-test-scheduled-daily-d…
Browse files Browse the repository at this point in the history
…igests

SDE-170: Scheduled Daily Digests Test
  • Loading branch information
antreaspas authored Aug 22, 2019
2 parents 5a843c0 + 113a162 commit 58fd2f5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ jobs:
- run:
name: Run ESLint
command: npm run lint
- run:
name: Run Unit Tests
command: npm test
serverless-deploy:
docker:
- image: circleci/node:10.15.1
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ circleci.exe

# Env directories
env/

# VS Code Config Directory
.vscode/
9 changes: 4 additions & 5 deletions functions/scheduledDailyDigests.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { DynamoDB, Lambda } from 'aws-sdk';

const dynamoDbDocumentClient = new DynamoDB.DocumentClient();
const lambda = new Lambda();

const { SUBSCRIPTIONS_TABLE: TableName, USER_DIGEST_LAMBDA_NAME: FunctionName } = process.env;

export const handler = async () => {
const { SUBSCRIPTIONS_TABLE: TableName, USER_DIGEST_LAMBDA_NAME: FunctionName } = process.env;
const dynamoDbDocumentClient = new DynamoDB.DocumentClient();
const lambda = new Lambda();

const scanParams = { TableName };
const { Items } = await dynamoDbDocumentClient.scan(scanParams).promise();
await Promise.all(
Expand Down
40 changes: 40 additions & 0 deletions tests/scheduledDailyDigests.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { DynamoDB, Lambda } from 'aws-sdk';
import { handler } from '../functions/scheduledDailyDigests';

jest.mock('aws-sdk');

const subscriptionsTableData = {
Items: [
{ userId: 1, subscriptions: [] },
{ userId: 2, subscriptions: ['test'] },
{ userId: 3, subscriptions: ['test1', 'test2'] }
]
};

const mockDynamoDbScan = {
promise: jest.fn()
};

const mockLambdaInvoke = {
promise: jest.fn()
};

DynamoDB.DocumentClient.mockImplementation(() => ({ scan: () => mockDynamoDbScan }));

Lambda.mockImplementation(() => ({ invoke: () => mockLambdaInvoke }));

describe('scheduledDailyDigests function', () => {
beforeAll(() => {
process.env = Object.assign(process.env, {
SUBSCRIPTIONS_TABLE: 'subscriptions',
USER_DIGEST_LAMBDA_NAME: 'calculateUserDailyDigest'
});
});

it('invokes calculateUserDailyDigest for each user with at least one subscription', async () => {
mockDynamoDbScan.promise.mockResolvedValue(subscriptionsTableData);
mockLambdaInvoke.promise.mockResolvedValue();
await handler();
expect(mockLambdaInvoke.promise).toHaveBeenCalledTimes(2);
});
});

0 comments on commit 58fd2f5

Please sign in to comment.