Skip to content
This repository has been archived by the owner on Jul 17, 2020. It is now read-only.

Add unit tests for donation controller #413

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions server/controllers/donation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {BadRequestError} from '../lib/errors'
import mailer from '../lib/mail/mail-helpers'

export default {
async create(req, res) {
async create(req, res) {
let newDonation = {
...(pick(req.body, ['donor', 'description', 'items'])),
total: req.body.items.reduce((acc, item) => {
Expand All @@ -24,7 +24,7 @@ export default {
throw new UnauthorizedError
}

const donation = await Donation.create(newDonation)
const donation = await Donation.create(newDonation)
const donor = await Donor.findByIdAndUpdate(donation.donor,
{$push: {donations: donation}},
{new: true}
Expand All @@ -38,7 +38,7 @@ export default {
})
},

async approve(req, res) {
async approve(req, res) {
const donation = await Donation.findByIdAndUpdate(
req.params.donationId,
{$set: {approved: true, dateIssued: Date.now()}},
Expand All @@ -50,11 +50,12 @@ export default {
res.json(donation)
},

async sendEmail(req, res) {
async sendEmail(req, res) {
const donation = await Donation.findById(req.params.donationId)
.populate('donor')

mailer.sendReceipt(donation)

res.json(donation)
},

Expand Down
153 changes: 153 additions & 0 deletions server/controllers/donation.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
const sinon = require('sinon')
const sandbox = sinon.createSandbox()
import Donation from '../models/donation'
import Donor from '../models/donor'
import donationCtrl from './donation'

describe('Donation controller', function() {
let res = {}

before(function() {
res = {
json: sandbox.spy()
}
})

after(function() {
res = {}
})

describe('create', function() {
afterEach(function() {
sandbox.restore()
})

it('should return created donation and donor objects', async function() {
const req = {
body: {
donor: 99999,
description: 'User Donation',
items: [ { name: 'Potatoes', value: 10 },
{ name: 'Carrots', value: 5 },
{ name: 'Spaghetti', value: 5 }
]
},
user: {
firstName: 'admin',
lastName: 'test',
roles: [ 'roles/admin' ],
created: Date.now,
notifications: [],
_id: 99999,
email: '[email protected]',
provider: 'local',
__v: 0
}
}
const dummyDonation = {
approved: false,
donor: 99999,
description: 'User Donation',
items: [ { name: 'Potatoes', value: 10 } ],
total: 10,
dateReceived: Date.now,
_id: 99999,
__v: 0
}
const dummyDonor = {
donations: [ 231 ],
fields: [],
dateReceived: Date.now,
_id: 99999,
__v: 0
}

const customerSaveStub = sandbox.stub(Donation, 'create').returns(dummyDonation)
const donarFindByIdAndUpdateStub = sandbox.stub(Donor, 'findByIdAndUpdate').returns(dummyDonor)
donationCtrl.__Rewire__('mailer', {
sendThanks: donation => donation
})

await donationCtrl.create(req, res)

donationCtrl.__ResetDependency__('mailer')

sinon.assert.calledOnce(customerSaveStub)
sinon.assert.calledOnce(donarFindByIdAndUpdateStub)
sinon.assert.calledWith(res.json, sinon.match.has("donation"))
sinon.assert.calledWith(res.json, sinon.match.has("donor"))
})
})

describe('approve', function() {
afterEach(function() {
sandbox.restore()
})

it('should return donation object requested for approval', async function() {
const req = {
params: {
donationId: '273'
}
}
const dummyDonation = {
_id: 99999,
donor: {
firstName: 'userdonor',
lastName: 'test',
_id: 99999
}
}
const mockFindByIdAndUpdate = {
populate: sandbox.stub().returns(dummyDonation)
}
const donationFindByIdAndUpdateStub = sandbox.stub(Donation, 'findByIdAndUpdate').returns(mockFindByIdAndUpdate)
donationCtrl.__Rewire__('mailer', {
sendReceipt: donation => donation
})

await donationCtrl.approve(req, res)

donationCtrl.__ResetDependency__('mailer')

sinon.assert.calledOnce(donationFindByIdAndUpdateStub)
sinon.assert.calledWith(res.json, sinon.match({ _id: 99999 }))
})
})

describe('sendEmail', function() {
afterEach(function() {
sandbox.restore()
})

it('should return donation object for email', function() {
const req = {
params: {
donationId: '273'
}
}
const dummyDonation = {
_id: 99999,
donor: {
firstName: 'userdonor',
lastName: 'test',
_id: 99999
}
}
const mockFindById = {
populate: sandbox.stub().returns(dummyDonation)
}
const donationFindByIdStub = sandbox.stub(Donation, 'findById').returns(mockFindById)
donationCtrl.__Rewire__('mailer', {
sendReceipt: donation => donation
})

donationCtrl.sendEmail(req, res)

donationCtrl.__ResetDependency__('mailer')

sinon.assert.calledOnce(donationFindByIdStub)
sinon.assert.calledWith(res.json, sinon.match({ _id: 99999 }))
})
})
})
4 changes: 2 additions & 2 deletions server/lib/mail/mail-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default {
)
},

async sendThanks(donation) {
async sendThanks(donation) {
const {donor} = donation
const {firstName, lastName, email} = donor
const fullName = `${firstName} ${lastName}`
Expand All @@ -99,7 +99,7 @@ export default {
)
},

async sendReceipt(donation) {
async sendReceipt(donation) {
const {donor, items} = donation
const {firstName, lastName, email} = donor
const fullName = `${firstName} ${lastName}`
Expand Down