|
3 | 3 |
|
4 | 4 | const mongoose = require('mongoose')
|
5 | 5 | mongoose.Promomise = global.Promise
|
| 6 | + |
6 | 7 | const sinon = require('sinon')
|
7 | 8 | require('sinon-mongoose')
|
8 |
| -const proxyquire = require('proxyquire').noCallThru() |
| 9 | + |
| 10 | +const proxyquire = require('proxyquire') |
9 | 11 | const httpMocks = require('node-mocks-http')
|
10 |
| -const School = require('../../models/school') |
| 12 | + |
11 | 13 | const Pupil = require('../../models/pupil')
|
12 |
| -const pupilValidator = require('../../lib/validator/pupil-validator') |
13 |
| -const ValidationError = require('../../lib/validation-error') |
| 14 | + |
| 15 | +const pupilService = require('../../services/pupil.service') |
| 16 | +const pupilsNotTakingCheckService = require('../../services/pupils-not-taking-check.service') |
| 17 | +const pupilsNotTakingCheckDataService = require('../../services/data-access/pupils-not-taking-check.data.service') |
| 18 | + |
| 19 | +const pupilMock = require('../mocks/pupil-with-reason') |
| 20 | +const attendanceCodesMock = require('../mocks/attendance-codes') |
| 21 | +const pupilsWithReasonsMock = require('../mocks/pupils-with-reason-2') |
| 22 | +const pupilsWithReasonsFormattedMock = require('../mocks/pupils-with-reason-formatted') |
14 | 23 |
|
15 | 24 | describe('school controller:', () => {
|
| 25 | + function getRes () { |
| 26 | + const res = httpMocks.createResponse() |
| 27 | + res.locals = {} |
| 28 | + return res |
| 29 | + } |
| 30 | + |
| 31 | + function getReq (params) { |
| 32 | + const req = httpMocks.createRequest(params) |
| 33 | + req.user = { School: 9991001 } |
| 34 | + req.breadcrumbs = jasmine.createSpy('breadcrumbs') |
| 35 | + req.flash = jasmine.createSpy('flash') |
| 36 | + return req |
| 37 | + } |
| 38 | + |
| 39 | + describe('Check routes', () => { |
| 40 | + let controller |
| 41 | + let sandbox |
| 42 | + let next |
| 43 | + let goodReqParams = { |
| 44 | + method: 'GET', |
| 45 | + url: '/school/pupils-not-taking-check' |
| 46 | + } |
| 47 | + |
| 48 | + beforeEach(() => { |
| 49 | + sandbox = sinon.sandbox.create() |
| 50 | + next = jasmine.createSpy('next') |
| 51 | + }) |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + sandbox.restore() |
| 55 | + }) |
| 56 | + |
| 57 | + describe('When there are pupils for the active school', () => { |
| 58 | + beforeEach(() => { |
| 59 | + spyOn(pupilsNotTakingCheckDataService, 'getAttendanceCodes').and.returnValue(Promise.resolve(attendanceCodesMock)) |
| 60 | + spyOn(pupilsNotTakingCheckDataService, 'fetchPupilsWithReasons').and.returnValue(Promise.resolve(pupilsWithReasonsMock)) |
| 61 | + spyOn(pupilsNotTakingCheckService, 'formatPupilsWithReasons').and.returnValue(Promise.resolve(pupilsWithReasonsFormattedMock)) |
| 62 | + controller = proxyquire('../../controllers/school', {}).getPupilNotTakingCheck |
| 63 | + }) |
| 64 | + |
| 65 | + it('should display \'pupils not taking the check\' initial page', async (done) => { |
| 66 | + const res = getRes() |
| 67 | + const req = getReq(goodReqParams) |
| 68 | + await controller(req, res, next) |
| 69 | + expect(res.statusCode).toBe(200) |
| 70 | + expect(res.locals.pageTitle).toBe('Pupils not taking the check') |
| 71 | + expect(next).not.toHaveBeenCalled() |
| 72 | + done() |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + describe('Select reason for pupils', () => { |
| 77 | + beforeEach(() => { |
| 78 | + spyOn(pupilsNotTakingCheckDataService, 'getAttendanceCodes').and.returnValue(Promise.resolve(attendanceCodesMock)) |
| 79 | + spyOn(pupilService, 'fetchSortedPupilsData').and.returnValue(Promise.resolve(pupilsWithReasonsMock)) |
| 80 | + spyOn(pupilsNotTakingCheckService, 'formatPupilsWithReasons').and.returnValue(Promise.resolve(pupilsWithReasonsFormattedMock)) |
| 81 | + spyOn(pupilsNotTakingCheckService, 'sortPupilsByReason').and.returnValue(Promise.resolve(pupilsWithReasonsFormattedMock)) |
| 82 | + controller = proxyquire('../../controllers/school', {}).getSelectPupilNotTakingCheck |
| 83 | + }) |
| 84 | + |
| 85 | + it('should display reasons and pupils', async (done) => { |
| 86 | + const res = getRes() |
| 87 | + const req = getReq(goodReqParams) |
| 88 | + await controller(req, res, next) |
| 89 | + expect(res.statusCode).toBe(200) |
| 90 | + expect(res.locals.pageTitle).toBe('Select pupils not taking the check') |
| 91 | + expect(next).not.toHaveBeenCalled() |
| 92 | + done() |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + describe('Save reason for pupil', () => { |
| 97 | + beforeEach(() => { |
| 98 | + spyOn(pupilService, 'fetchMultiplePupils').and.returnValue(Promise.resolve(pupilsWithReasonsMock)) |
| 99 | + spyOn(Pupil, 'create').and.returnValue(Promise.resolve(pupilMock)) |
| 100 | + spyOn(pupilsNotTakingCheckDataService, 'getAttendanceCodes').and.returnValue(Promise.resolve(attendanceCodesMock)) |
| 101 | + spyOn(pupilsNotTakingCheckDataService, 'fetchPupilsWithReasons').and.returnValue(Promise.resolve(pupilsWithReasonsMock)) |
| 102 | + controller = proxyquire('../../controllers/school', {}).savePupilNotTakingCheck |
| 103 | + }) |
| 104 | + |
| 105 | + it('should save and display reasons and pupils', async (done) => { |
| 106 | + const res = getRes() |
| 107 | + const req = getReq( |
| 108 | + { |
| 109 | + method: 'POST', |
| 110 | + url: '/school/pupils-not-taking-check/save-pupils', |
| 111 | + body: { |
| 112 | + attendanceCode: '59df7e1c283960b43172ac6c', |
| 113 | + pupil: { |
| 114 | + _id: ['595cd5416e5ca13e48ed2518'] |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + ) |
| 119 | + await controller(req, res, next) |
| 120 | + expect(res.statusCode).toBe(200) |
| 121 | + expect(res.locals.pageTitle).toBe('Save pupils not taking the check') |
| 122 | + expect(next).not.toHaveBeenCalled() |
| 123 | + done() |
| 124 | + }) |
| 125 | + }) |
16 | 126 |
|
| 127 | + describe('Remove reason for pupil', () => { |
| 128 | + beforeEach(() => { |
| 129 | + spyOn(pupilService, 'fetchOnePupil').and.returnValue(Promise.resolve(pupilsWithReasonsMock)) |
| 130 | + spyOn(Pupil, 'create').and.returnValue(Promise.resolve(pupilMock)) |
| 131 | + controller = proxyquire('../../controllers/school', {}).removePupilNotTakingCheck |
| 132 | + }) |
| 133 | + it('should delete reason from pupils document and redirect', async (done) => { |
| 134 | + const res = getRes() |
| 135 | + const req = getReq( |
| 136 | + { |
| 137 | + method: 'GET', |
| 138 | + url: '/school/pupils-not-taking-check/remove', |
| 139 | + params: '59d02ab09b865f35a3f51940' |
| 140 | + } |
| 141 | + ) |
| 142 | + await controller(req, res, next) |
| 143 | + expect(res.statusCode).toBe(302) |
| 144 | + expect(next).not.toHaveBeenCalled() |
| 145 | + done() |
| 146 | + }) |
| 147 | + }) |
| 148 | + }) |
17 | 149 | })
|
0 commit comments