Skip to content

Commit

Permalink
Merge pull request #1084 from uktrade/unit-tests/tidy-up-sandbox-usage
Browse files Browse the repository at this point in the history
Unit tests tidy up sandbox usage
  • Loading branch information
feedmypixel authored Dec 14, 2017
2 parents 2e1f729 + 7428990 commit 6ae603f
Show file tree
Hide file tree
Showing 106 changed files with 561 additions and 1,058 deletions.
3 changes: 2 additions & 1 deletion test/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"nock": true,
"rootPath": true,
"globalReq": true,
"globalRes": true
"globalRes": true,
"sandbox": true
},
"rules": {
"no-unused-expressions": 0
Expand Down
15 changes: 5 additions & 10 deletions test/unit/apps/api/controllers.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
describe('api controllers', () => {
beforeEach(() => {
this.req = { params: { postcode: 'dn21 6fg' } }
this.sandbox = sinon.sandbox.create()
this.resMock = {
send: this.sandbox.spy(),
json: this.sandbox.spy(),
status: this.sandbox.stub().returnsThis(),
send: sandbox.spy(),
json: sandbox.spy(),
status: sandbox.stub().returnsThis(),
}
this.lookupAddressStub = this.sandbox.stub().resolves([
this.lookupAddressStub = sandbox.stub().resolves([
{ country: 'united kingdom' },
])
this.controller = proxyquire('~/src/apps/api/controllers', {
Expand All @@ -23,10 +22,6 @@ describe('api controllers', () => {
})
})

afterEach(() => {
this.sandbox.restore()
})

describe('postcodeLookupHandler', () => {
context('when the external api service returns successful response', () => {
beforeEach(async () => {
Expand All @@ -41,7 +36,7 @@ describe('api controllers', () => {
beforeEach(async () => {
this.statusCode = 400
this.message = 'error'
this.lookupAddressStub = this.sandbox.stub().throws({
this.lookupAddressStub = sandbox.stub().throws({
statusCode: this.statusCode, message: this.message,
})
await this.controller.postcodeLookupHandler(this.req, this.resMock)
Expand Down
6 changes: 0 additions & 6 deletions test/unit/apps/audit/transformers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ const auditLog = require('~/test/unit/data/audit/contact-audit.json')

describe('Audit transformers', () => {
beforeEach(() => {
this.sandbox = sinon.sandbox.create()

this.buildPaginationStub = sinon.sandbox.stub()
this.transformers = proxyquire('~/src/apps/audit/transformers', {
'../../lib/pagination': {
Expand All @@ -18,10 +16,6 @@ describe('Audit transformers', () => {
this.transformer = this.transformers.transformAuditLogToListItem(contactAuditLabels)
})

afterEach(() => {
this.sandbox.restore()
})

it('should return a formatted audit history item when there are changes', () => {
const transformedItem = this.transformer(auditLog.results[0])

Expand Down
15 changes: 4 additions & 11 deletions test/unit/apps/builders.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,12 @@ describe('Global builders', () => {
describe('#buildFormWithStateAndErrors', () => {
beforeEach(() => {
this.builders = rewire('~/src/apps/builders')
this.sandbox = sinon.sandbox.create()
this.buildFormWithStateSpy = this.sandbox.stub()
this.buildFormWithErrorsSpy = this.sandbox.stub()
this.buildFormWithStateSpy = sandbox.stub()
this.buildFormWithErrorsSpy = sandbox.stub()
this.builders.__set__('buildFormWithState', this.buildFormWithStateSpy)
this.builders.__set__('buildFormWithErrors', this.buildFormWithErrorsSpy)
})

afterEach(() => {
this.sandbox.restore()
})

it('should return form without calling any functions', () => {
const formObject = {
children: [
Expand All @@ -305,21 +300,19 @@ describe('Global builders', () => {
this.builders.buildFormWithStateAndErrors(formObject, requestBody, errorsObject)

expect(this.buildFormWithStateSpy).to.be.calledWith(formObject, requestBody)
expect(this.buildFormWithErrorsSpy).to.be.calledWith(this.sandbox.match.any, errorsObject)
expect(this.buildFormWithErrorsSpy).to.be.calledWith(sandbox.match.any, errorsObject)
})
})

describe('#buildSelectedFiltersSummary', () => {
beforeEach(() => {
this.sandbox = sinon.sandbox.create()

this.fields = [
{
macroName: 'MultipleChoiceField',
name: 'stage',
label: 'State',
type: 'checkbox',
options: this.sandbox.stub().returns([
options: sandbox.stub().returns([
{ value: 'a', label: 'A' },
{ value: 'b', label: 'B' },
{ value: 'c', label: 'C' },
Expand Down
15 changes: 5 additions & 10 deletions test/unit/apps/companies/controllers/account-management.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
describe('Companies account management controller', () => {
beforeEach(() => {
this.sandbox = sinon.sandbox.create()
this.controller = require('~/src/apps/companies/controllers/account-management')
this.reqMock = {
session: {
Expand All @@ -9,10 +8,10 @@ describe('Companies account management controller', () => {
body: {},
}
this.resMock = {
breadcrumb: this.sandbox.stub().returnsThis(),
title: this.sandbox.stub().returnsThis(),
render: this.sandbox.spy(),
redirect: this.sandbox.spy(),
breadcrumb: sandbox.stub().returnsThis(),
title: sandbox.stub().returnsThis(),
render: sandbox.spy(),
redirect: sandbox.spy(),
locals: {
entityName: 'company',
returnLink: 'return',
Expand All @@ -23,11 +22,7 @@ describe('Companies account management controller', () => {
advisers: [],
},
}
this.nextSpy = this.sandbox.spy()
})

afterEach(() => {
this.sandbox.restore()
this.nextSpy = sandbox.spy()
})

describe('#renderAccountManagementEditPage', () => {
Expand Down
12 changes: 6 additions & 6 deletions test/unit/apps/companies/controllers/add.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ const mockMetadataRepository = {
],
}

describe('Company add controller', function () {
describe('Company add controller', () => {
let searchLimitedCompaniesStub
let getDisplayCHStub
let getCHCompanyStub
let companyAddController

beforeEach(function () {
beforeEach(() => {
searchLimitedCompaniesStub = sinon.stub().resolves(companiesHouseAndLtdCompanies)
getDisplayCHStub = sinon.stub().resolves(displayHouseCompany)
getCHCompanyStub = sinon.stub().resolves(companiesHouseCompany)
Expand All @@ -47,7 +47,7 @@ describe('Company add controller', function () {
})
})

describe('Get step 1', function () {
describe('Get step 1', () => {
it('should return options for company types', function (done) {
const req = { session: {} }
const expected = [
Expand Down Expand Up @@ -109,8 +109,8 @@ describe('Company add controller', function () {
companyAddController.renderAddStepOne(req, res, next)
})
})
describe('Post step 1', function () {
describe('forward to next page', function () {
describe('Post step 1', () => {
describe('forward to next page', () => {
it('should forward the user to step 2 when adding a uk ltd.', function (done) {
const req = {
body: {
Expand Down Expand Up @@ -162,7 +162,7 @@ describe('Company add controller', function () {
companyAddController.postAddStepOne(req, res, next)
})
})
describe('errors', function () {
describe('errors', () => {
it('should show an error when no option selected', function (done) {
const req = {
body: {},
Expand Down
16 changes: 5 additions & 11 deletions test/unit/apps/companies/controllers/archive.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ const companyMock = {

describe('Company controller, archive', () => {
beforeEach(() => {
this.sandbox = sinon.sandbox.create()

this.archiveCompanyStub = this.sandbox.stub()
this.unarchiveCompanyStub = this.sandbox.stub()
this.errorLoggerSpy = this.sandbox.spy()
this.redirectSpy = this.sandbox.spy()
this.flashSpy = this.sandbox.spy()
this.archiveCompanyStub = sandbox.stub()
this.unarchiveCompanyStub = sandbox.stub()
this.errorLoggerSpy = sandbox.spy()
this.redirectSpy = sandbox.spy()
this.flashSpy = sandbox.spy()

this.controller = proxyquire('~/src/apps/companies/controllers/archive', {
'../repos': {
Expand All @@ -46,10 +44,6 @@ describe('Company controller, archive', () => {
}
})

afterEach(() => {
this.sandbox.restore()
})

describe('archiveCompany()', () => {
context('when no reason is supplied', () => {
beforeEach(() => {
Expand Down
18 changes: 6 additions & 12 deletions test/unit/apps/companies/controllers/audit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ const tokenMock = '12345abcde'

describe('Company audit controller', () => {
beforeEach(() => {
this.sandbox = sinon.sandbox.create()

this.getCompanyAuditLogStub = this.sandbox.stub()
this.transformApiResponseToCollectionSpy = this.sandbox.spy()
this.transformAuditLogToListItemSpy = this.sandbox.spy()
this.breadcrumbStub = this.sandbox.stub().returnsThis()
this.renderSpy = this.sandbox.spy()
this.nextSpy = this.sandbox.spy()
this.getCompanyAuditLogStub = sandbox.stub()
this.transformApiResponseToCollectionSpy = sandbox.spy()
this.transformAuditLogToListItemSpy = sandbox.spy()
this.breadcrumbStub = sandbox.stub().returnsThis()
this.renderSpy = sandbox.spy()
this.nextSpy = sandbox.spy()

this.controller = proxyquire('~/src/apps/companies/controllers/audit', {
'../repos': {
Expand Down Expand Up @@ -40,10 +38,6 @@ describe('Company audit controller', () => {
}
})

afterEach(() => {
this.sandbox.restore()
})

context('when audit returns successfully', () => {
beforeEach(() => {
this.getCompanyAuditLogStub.resolves(auditLogMock)
Expand Down
13 changes: 4 additions & 9 deletions test/unit/apps/companies/controllers/contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@ const { assign } = require('lodash')

describe('Company contact list controller', () => {
beforeEach(() => {
this.sandbox = sinon.sandbox.create()
this.next = this.sandbox.spy()
this.next = sandbox.spy()
this.resMock = assign({}, globalRes, {
locals: {
company: {
id: '1234334',
name: 'mock company',
},
},
breadcrumb: function () { return this },
render: this.sandbox.spy(),
breadcrumb: sandbox.stub().returnsThis(),
render: sandbox.spy(),
query: {},
})
this.reqMock = assign({}, globalReq, {
session: {
token: 'abcd',
},
})
this.buildSelectedFiltersSummaryStub = this.sandbox.spy()
this.buildSelectedFiltersSummaryStub = sandbox.spy()

this.controller = proxyquire('~/src/apps/companies/controllers/contacts', {
'../../builders': {
Expand All @@ -35,10 +34,6 @@ describe('Company contact list controller', () => {
})
})

afterEach(() => {
this.sandbox.restore()
})

describe('#renderContacts', () => {
it('should render collection page with locals', () => {
this.controller.renderContacts(this.reqMock, this.resMock, this.next)
Expand Down
13 changes: 3 additions & 10 deletions test/unit/apps/companies/controllers/contacts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ const companyMock = require('~/test/unit/data/companies/company.json')

describe('Company contacts controller', () => {
beforeEach(() => {
this.sandbox = sinon.sandbox.create()

this.nextSpy = this.sandbox.spy()
this.breadcrumbStub = this.sandbox.stub().returnsThis()
this.renderSpy = this.sandbox.spy()
this.transformContactToListItemSpy = this.sandbox.spy()
this.breadcrumbStub = sandbox.stub().returnsThis()
this.renderSpy = sandbox.spy()
this.transformContactToListItemSpy = sandbox.spy()

this.controller = require('~/src/apps/companies/controllers/contacts')

Expand All @@ -25,10 +22,6 @@ describe('Company contacts controller', () => {
}
})

afterEach(() => {
this.sandbox.restore()
})

describe('renderContacts()', () => {
describe('all scenarios', () => {
beforeEach(() => {
Expand Down
10 changes: 2 additions & 8 deletions test/unit/apps/companies/controllers/details.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ const { renderDetails } = require('~/src/apps/companies/controllers/details')

describe('Companies details controller', () => {
beforeEach(() => {
this.sandbox = sinon.sandbox.create()

this.req = {
session: {
token: '1234',
Expand All @@ -17,15 +15,11 @@ describe('Companies details controller', () => {

this.res = {
locals: {},
breadcrumb: this.sandbox.stub().returnsThis(),
render: this.sandbox.stub(),
breadcrumb: sandbox.stub().returnsThis(),
render: sandbox.stub(),
}
})

afterEach(() => {
this.sandbox.restore()
})

describe('#renderDetails', () => {
context('when the company contains companes house data', () => {
beforeEach(() => {
Expand Down
15 changes: 5 additions & 10 deletions test/unit/apps/companies/controllers/edit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ const { assign } = require('lodash')

describe('Company export controller', () => {
beforeEach(() => {
this.sandbox = sinon.sandbox.create()
this.saveCompany = this.sandbox.stub()
this.breadcrumbStub = this.sandbox.stub().returnsThis()
this.renderSpy = this.sandbox.spy()
this.nextSpy = this.sandbox.spy()
this.redirectSpy = this.sandbox.spy()
this.saveCompany = sandbox.stub()
this.breadcrumbStub = sandbox.stub().returnsThis()
this.renderSpy = sandbox.spy()
this.nextSpy = sandbox.spy()
this.redirectSpy = sandbox.spy()

const ukOtherCompanyOptions = [
{
Expand Down Expand Up @@ -72,10 +71,6 @@ describe('Company export controller', () => {
this.resMock = this.buildRes({})
})

afterEach(() => {
this.sandbox.restore()
})

describe('getBusinessTypeLabel()', () => {
it('should handle UK limited company', () => {
const label = this.controller.getBusinessTypeLabel('limited company', false, null)
Expand Down
Loading

0 comments on commit 6ae603f

Please sign in to comment.