-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor db module exporting UsersCollection, unit-testing repository
- Loading branch information
1 parent
f4edbc0
commit b56b730
Showing
4 changed files
with
17 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
const test = require('ava') | ||
const sinon = require('sinon') | ||
const db = require('./lib/db') | ||
const { UsersCollection } = require('./lib/db') | ||
|
||
const userRepository = require('./lib/user-repository') | ||
const emailService = require('./lib/email-service') | ||
const newsletter = require('.') | ||
|
||
test.beforeEach(async () => { | ||
await db.get('users').remove({}) | ||
await UsersCollection.remove({}) | ||
}) | ||
|
||
test('sends a newsletter to users that did not yet receive it', async t => { | ||
await db.get('users').insert({ name: 'test', email: '[email protected]', lastEmailSentAt: null }) | ||
await UsersCollection.insert({ name: 'test', email: '[email protected]', lastEmailSentAt: null }) | ||
|
||
sinon.spy(userRepository, 'findNotYetReceivedNewsletter') | ||
sinon.stub(emailService, 'sendTo') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
const monk = require('monk') | ||
const db = monk(process.env.MONGO_URL) | ||
|
||
module.exports = db | ||
module.exports = { | ||
UsersCollection: db.get('users') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,21 @@ | ||
const test = require('ava') | ||
const db = require('./db') | ||
const sinon = require('sinon') | ||
const userRepository = require('./user-repository') | ||
const { UsersCollection } = require('./db') | ||
|
||
test.beforeEach(async () => { | ||
await db.get('users').remove({}) | ||
await UsersCollection.remove({}) | ||
}) | ||
|
||
test('find users that did not yet receive the newsletter', async t => { | ||
await db.get('users').insert({ name: 'test', email: '[email protected]', lastEmailSentAt: null }) | ||
await UsersCollection.insert({ name: 'test', email: '[email protected]', lastEmailSentAt: null }) | ||
|
||
sinon.spy(UsersCollection, 'find') | ||
|
||
const users = await userRepository.findNotYetReceivedNewsletter() | ||
t.true(Array.isArray(users)) | ||
t.is(users.length, 1) | ||
|
||
t.true(UsersCollection.find.calledOnce) | ||
t.true(UsersCollection.find.calledWith({ lastEmailSentAt: null })) | ||
}) |