Skip to content

Commit

Permalink
Completing refactoring controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Manoj Bisht committed Oct 2, 2023
1 parent 7b34d7b commit 557b68c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 75 deletions.
5 changes: 3 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ module.exports = {
testEnvironment: 'node',
// The glob patterns Jest uses to detect test files
testMatch: [
'**/test/controllers/bill-runs.controller.test.js',
'**/test/controllers/billing-accounts.controller.test.js',

'**/test/controllers/*.test.js',

'**/test/errors/*.test.js',
'**/test/models/**/*.test.js'
],
Expand Down
18 changes: 4 additions & 14 deletions test/controllers/check.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
'use strict'

// Test framework dependencies

const Sinon = require('sinon')

// Things we need to stub
const TwoPartService = require('../../app/services/check/two-part.service.js')

// For running our service
const { init } = require('../../app/server.js')

const { describe, beforeEach, afterEach, it, expect } = require('jest') // Importing from Jest

describe('Check controller', () => {
let server

Expand All @@ -21,16 +16,11 @@ describe('Check controller', () => {

// We silence any calls to server.logger.error made in the plugin to try and keep the test output as clean as
// possible
Sinon.stub(server.logger, 'error')
server.logger.error = jest.fn().mockResolvedValue()

// We silence sending a notification to our Errbit instance using Airbrake
Sinon.stub(server.app.airbrake, 'notify').resolvesThis()
server.app.airbrake.notify = jest.fn().mockResolvedValue()
})

afterEach(() => {
Sinon.restore()
})

describe('GET /check/two-part', () => {
const options = {
method: 'GET',
Expand All @@ -39,7 +29,7 @@ describe('Check controller', () => {

describe('when the request succeeds', () => {
beforeEach(async () => {
Sinon.stub(TwoPartService, 'go').resolves({ regionName: 'Fantasia' })
jest.spyOn(TwoPartService, 'go').mockResolvedValue({ regionName: 'Fantasia' })
})

it('displays the correct message', async () => {
Expand All @@ -55,7 +45,7 @@ describe('Check controller', () => {
describe('when the request fails', () => {
describe('because the TwoPartService errors', () => {
beforeEach(async () => {
Sinon.stub(TwoPartService, 'go').rejects()
jest.spyOn(TwoPartService, 'go').mockRejectedValue()
})

it('returns a 500 status', async () => {
Expand Down
46 changes: 16 additions & 30 deletions test/controllers/data.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script()
const { expect } = Code

// Things we need to stub
const ExportService = require('../../app/services/data/export/export.service.js')
const MockService = require('../../app/services/data/mock/mock.service.js')
Expand All @@ -26,14 +18,9 @@ describe('Data controller', () => {

// We silence any calls to server.logger.error made in the plugin to try and keep the test output as clean as
// possible
Sinon.stub(server.logger, 'error')

server.logger.error = jest.fn().mockResolvedValue()
// We silence sending a notification to our Errbit instance using Airbrake
Sinon.stub(server.app.airbrake, 'notify').resolvesThis()
})

afterEach(() => {
Sinon.restore()
server.app.airbrake.notify = jest.fn().mockResolvedValue()
})

describe('GET /data/export', () => {
Expand All @@ -44,13 +31,13 @@ describe('Data controller', () => {

describe('when the request succeeds', () => {
beforeEach(async () => {
Sinon.stub(ExportService, 'go').resolves()
jest.spyOn(ExportService, 'go').mockResolvedValue()
})

it('displays the correct message', async () => {
const response = await server.inject(options)

expect(response.statusCode).to.equal(204)
expect(response.statusCode).toEqual(204)
})
})
})
Expand All @@ -63,27 +50,27 @@ describe('Data controller', () => {

describe('when the request succeeds', () => {
beforeEach(async () => {
Sinon.stub(MockService, 'go').resolves({ data: 'mock' })
jest.spyOn(MockService, 'go').mockResolvedValue({ data: 'mock' })
})

it('displays the correct message', async () => {
const response = await server.inject(options)

expect(response.statusCode).to.equal(200)
expect(response.statusCode).toEqual(200)
// TODO: test the response object
})
})

describe('when the request fails', () => {
describe('because the MockService errors', () => {
beforeEach(async () => {
Sinon.stub(MockService, 'go').rejects()
jest.spyOn(MockService, 'go').mockRejectedValue()
})

it('returns a 500 status', async () => {
const response = await server.inject(options)

expect(response.statusCode).to.equal(500)
expect(response.statusCode).toEqual(500)
})
})
})
Expand All @@ -97,26 +84,25 @@ describe('Data controller', () => {

describe('when the request succeeds', () => {
beforeEach(async () => {
Sinon.stub(SeedService, 'go').resolves()
jest.spyOn(SeedService, 'go').mockResolvedValue()
})

it('displays the correct message', async () => {
const response = await server.inject(options)

expect(response.statusCode).to.equal(204)
expect(response.statusCode).toEqual(204)
})
})

describe('when the request fails', () => {
describe('because the SeedService errors', () => {
beforeEach(async () => {
Sinon.stub(SeedService, 'go').rejects()
jest.spyOn(SeedService, 'go').mockRejectedValue()
})

it('returns a 500 status', async () => {
const response = await server.inject(options)

expect(response.statusCode).to.equal(500)
expect(response.statusCode).toEqual(500)
})
})
})
Expand All @@ -130,26 +116,26 @@ describe('Data controller', () => {

describe('when the request succeeds', () => {
beforeEach(async () => {
Sinon.stub(TearDownService, 'go').resolves()
jest.spyOn(TearDownService, 'go').mockResolvedValue()
})

it('returns a 204 status', async () => {
const response = await server.inject(options)

expect(response.statusCode).to.equal(204)
expect(response.statusCode).toEqual(204)
})
})

describe('when the request fails', () => {
describe('because the TearDownService errors', () => {
beforeEach(async () => {
Sinon.stub(TearDownService, 'go').rejects()
jest.spyOn(TearDownService, 'go').mockRejectedValue()
})

it('returns a 500 status', async () => {
const response = await server.inject(options)

expect(response.statusCode).to.equal(500)
expect(response.statusCode).toEqual(500)
})
})
})
Expand Down
28 changes: 8 additions & 20 deletions test/controllers/health.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script()
const { expect } = Code

// Things we need to stub
const DatabaseHealthCheckService = require('../../app/services/health/database-health-check.service.js')
const InfoService = require('../../app/services/health/info.service.js')
Expand All @@ -25,14 +17,10 @@ describe('Health controller', () => {

// We silence any calls to server.logger.error made in the plugin to try and keep the test output as clean as
// possible
Sinon.stub(server.logger, 'error')
server.logger.error = jest.fn().mockResolvedValue()

// We silence sending a notification to our Errbit instance using Airbrake
airbrakeStub = Sinon.stub(server.app.airbrake, 'notify').resolvesThis()
})

afterEach(() => {
Sinon.restore()
airbrakeStub = jest.spyOn(server.app.airbrake, 'notify').mockResolvedValue(true)
})

describe('GET /health/airbrake', () => {
Expand All @@ -44,13 +32,13 @@ describe('Health controller', () => {
it('returns a 500 error', async () => {
const response = await server.inject(options)

expect(response.statusCode).to.equal(500)
expect(response.statusCode).toEqual(500)
})

it('causes Airbrake to send a notification', async () => {
await server.inject(options)

expect(airbrakeStub.called).to.equal(true)
expect(airbrakeStub).toHaveBeenCalled()
})
})

Expand All @@ -62,13 +50,13 @@ describe('Health controller', () => {

describe('when the request succeeds', () => {
beforeEach(async () => {
Sinon.stub(DatabaseHealthCheckService, 'go').resolves()
jest.spyOn(DatabaseHealthCheckService, 'go').mockResolvedValue()
})

it('returns stats about each table', async () => {
const response = await server.inject(options)

expect(response.statusCode).to.equal(200)
expect(response.statusCode).toEqual(200)
})
})
})
Expand All @@ -81,7 +69,7 @@ describe('Health controller', () => {

describe('when the request succeeds', () => {
beforeEach(async () => {
Sinon.stub(InfoService, 'go').resolves({
jest.spyOn(InfoService, 'go').mockResolvedValue({
virusScannerData: 'ClamAV 0.103.6/26738/Fri Dec 2 11:12:06 2022',
redisConnectivityData: 'ERROR: Command failed: redis-server --version /bin/sh: 1: redis-server: not found',
addressFacadeData: 'hola',
Expand All @@ -98,7 +86,7 @@ describe('Health controller', () => {
it('returns stats about each table', async () => {
const response = await server.inject(options)

expect(response.statusCode).to.equal(200)
expect(response.statusCode).toEqual(200)
})
})
})
Expand Down
11 changes: 2 additions & 9 deletions test/controllers/root.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it, beforeEach } = exports.lab = Lab.script()
const { expect } = Code

// For running our service
const { init } = require('../../app/server.js')

Expand All @@ -27,7 +20,7 @@ describe('Root controller: GET /', () => {
const response = await server.inject(options)
const payload = JSON.parse(response.payload)

expect(response.statusCode).to.equal(200)
expect(payload.status).to.equal('alive')
expect(response.statusCode).toEqual(200)
expect(payload.status).toEqual('alive')
})
})

0 comments on commit 557b68c

Please sign in to comment.