Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDE-170: Scheduled Daily Digests Test #74

Merged
merged 3 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});