@@ -2,14 +2,12 @@ const expect = require('chai').expect;
2
2
const sinon = require ( 'sinon' ) ;
3
3
const httpMocks = require ( 'node-mocks-http' ) ;
4
4
const buildUser = require ( '../../../../factories/user' ) ;
5
- const models = require ( '../../../../../models' ) ;
6
5
const Training = require ( '../../../../../models/classes/training' ) . Training ;
7
6
const { deleteTrainingRecord } = require ( '../../../../../routes/establishments/training/index' ) ;
8
7
const {
9
8
mockTrainingRecordWithCertificates,
10
9
mockTrainingRecordWithoutCertificates,
11
10
} = require ( '../../../mockdata/training' ) ;
12
- const WorkerCertificateService = require ( '../../../../../routes/establishments/workerCertificate/workerCertificateService' ) ;
13
11
const HttpError = require ( '../../../../../utils/errors/httpError' ) ;
14
12
15
13
describe ( 'server/routes/establishments/training/index.js' , ( ) => {
@@ -25,8 +23,8 @@ describe('server/routes/establishments/training/index.js', () => {
25
23
let workerUid = mockTrainingRecordWithCertificates . workerUid ;
26
24
let trainingUid = mockTrainingRecordWithCertificates . uid ;
27
25
let establishmentUid = user . establishment . uid ;
28
-
29
- let stubs = { } ;
26
+ let trainingRecord ;
27
+ let trainingCertificateService ;
30
28
31
29
beforeEach ( ( ) => {
32
30
req = httpMocks . createRequest ( {
@@ -37,76 +35,110 @@ describe('server/routes/establishments/training/index.js', () => {
37
35
} ) ;
38
36
res = httpMocks . createResponse ( ) ;
39
37
40
- stubs = {
41
- trainingRecord : sinon . createStubInstance ( Training ) ,
42
- restoreTrainingRecord : sinon . stub ( Training . prototype , 'restore' ) ,
43
- getWorkerCertificateServiceInstance : sinon . stub ( WorkerCertificateService , 'initialiseTraining' ) . returns ( new WorkerCertificateService ( ) ) ,
44
- deleteCertificates : sinon . stub ( WorkerCertificateService . prototype , 'deleteCertificates' ) ,
45
- destroyTrainingRecord : sinon . stub ( models . workerTraining , 'destroy' ) ,
46
- }
38
+ trainingRecord = sinon . createStubInstance ( Training ) ;
39
+ trainingRecord . restore . resolves ( true ) ;
40
+
41
+ trainingCertificateService = {
42
+ deleteCertificates : sinon . stub ( ) ,
43
+ } ;
47
44
} ) ;
48
45
49
- it ( 'should return with a status of 204 when the training record is deleted with training certificates' , async ( ) => {
50
- stubs . restoreTrainingRecord . returns ( mockTrainingRecordWithCertificates ) ;
51
- stubs . destroyTrainingRecord . returns ( 1 ) ;
46
+ describe ( 'With training certificates' , ( ) => {
47
+ beforeEach ( ( ) => {
48
+ trainingRecord . trainingCertificates = mockTrainingRecordWithCertificates . trainingCertificates ;
49
+ trainingRecord . delete . resolves ( 1 ) ;
50
+ } ) ;
52
51
53
- await deleteTrainingRecord ( req , res ) ;
52
+ it ( 'should return with a status of 204' , async ( ) => {
53
+ await deleteTrainingRecord ( req , res , trainingRecord , trainingCertificateService ) ;
54
+
55
+ expect ( res . statusCode ) . to . equal ( 204 ) ;
56
+ } ) ;
54
57
55
- expect ( res . statusCode ) . to . equal ( 204 ) ;
58
+ it ( 'should call delete on training instance' , async ( ) => {
59
+ await deleteTrainingRecord ( req , res , trainingRecord , trainingCertificateService ) ;
60
+
61
+ expect ( trainingRecord . delete ) . to . have . been . calledOnce ;
62
+ } ) ;
63
+
64
+ it ( 'should call deleteCertificates' , async ( ) => {
65
+ await deleteTrainingRecord ( req , res , trainingRecord , trainingCertificateService ) ;
66
+
67
+ expect ( trainingCertificateService . deleteCertificates . calledOnce ) . to . be . true ;
68
+ expect (
69
+ trainingCertificateService . deleteCertificates . calledWith (
70
+ mockTrainingRecordWithCertificates . trainingCertificates ,
71
+ establishmentUid ,
72
+ workerUid ,
73
+ trainingUid ,
74
+ ) ,
75
+ ) . to . be . true ;
76
+ } ) ;
56
77
} ) ;
57
78
58
- it ( 'should return with a status of 204 when the training record is deleted with no training certificates' , async ( ) => {
59
- stubs . restoreTrainingRecord . returns ( mockTrainingRecordWithoutCertificates ) ;
60
- stubs . destroyTrainingRecord . returns ( 1 ) ;
79
+ describe ( 'Without training certificates' , ( ) => {
80
+ beforeEach ( ( ) => {
81
+ trainingRecord . trainingCertificates = null ;
82
+ trainingRecord . delete . resolves ( 1 ) ;
83
+ } ) ;
84
+
85
+ it ( 'should return with a status of 204 when successful' , async ( ) => {
86
+ await deleteTrainingRecord ( req , res , trainingRecord , trainingCertificateService ) ;
87
+
88
+ expect ( res . statusCode ) . to . equal ( 204 ) ;
89
+ } ) ;
61
90
62
- await deleteTrainingRecord ( req , res ) ;
91
+ it ( 'should call delete on training instance' , async ( ) => {
92
+ await deleteTrainingRecord ( req , res , trainingRecord , trainingCertificateService ) ;
63
93
64
- expect ( res . statusCode ) . to . equal ( 204 ) ;
94
+ expect ( trainingRecord . delete ) . to . have . been . calledOnce ;
95
+ } ) ;
96
+
97
+ it ( 'should not call deleteCertificates when no training certificates' , async ( ) => {
98
+ await deleteTrainingRecord ( req , res , trainingRecord , trainingCertificateService ) ;
99
+
100
+ expect ( trainingCertificateService . deleteCertificates . called ) . to . be . false ;
101
+ } ) ;
65
102
} ) ;
66
103
67
104
describe ( 'errors' , ( ) => {
68
105
it ( 'should pass through status code if one is provided' , async ( ) => {
69
- stubs . restoreTrainingRecord . throws ( new HttpError ( 'Test error message' , 123 ) ) ;
70
- req . params . trainingUid = 'mockTrainingUid' ;
106
+ trainingRecord . restore . throws ( new HttpError ( 'Test error message' , 123 ) ) ;
71
107
72
- await deleteTrainingRecord ( req , res ) ;
108
+ await deleteTrainingRecord ( req , res , trainingRecord , trainingCertificateService ) ;
73
109
74
110
expect ( res . statusCode ) . to . equal ( 123 ) ;
75
111
} ) ;
76
112
77
113
it ( 'should default to status code 500 if no error code is provided' , async ( ) => {
78
- stubs . restoreTrainingRecord . throws ( new Error ( ) ) ;
79
- req . params . trainingUid = 'mockTrainingUid' ;
114
+ trainingRecord . restore . throws ( new Error ( ) ) ;
80
115
81
- await deleteTrainingRecord ( req , res ) ;
116
+ await deleteTrainingRecord ( req , res , trainingRecord , trainingCertificateService ) ;
82
117
83
118
expect ( res . statusCode ) . to . equal ( 500 ) ;
84
119
} ) ;
85
120
86
- describe ( 'restoring training record' , ( ) => {
87
- it ( 'should return a 404 status code if there is an unknown worker uid' , async ( ) => {
88
- trainingRecord_workerUid = 'mockWorkerUid' ;
121
+ it ( 'should return a 404 status code when failed to restore training (e.g. unknown worker uid)' , async ( ) => {
122
+ trainingRecord . restore . resolves ( false ) ;
89
123
90
- await deleteTrainingRecord ( req , res ) ;
124
+ await deleteTrainingRecord ( req , res , trainingRecord , trainingCertificateService ) ;
91
125
92
- const response = res . _getData ( ) ;
126
+ const response = res . _getData ( ) ;
93
127
94
- expect ( res . statusCode ) . to . equal ( 404 ) ;
95
- expect ( response ) . to . equal ( 'Not Found' ) ;
96
- } ) ;
128
+ expect ( res . statusCode ) . to . equal ( 404 ) ;
129
+ expect ( response ) . to . equal ( 'Not Found' ) ;
97
130
} ) ;
98
131
99
- describe ( ' deleting training record' , ( ) => {
100
- it ( 'should return with a status of 404 when there is an error deleting the training record from the database' , async ( ) => {
101
- stubs . destroyTrainingRecord . returns ( 0 ) ;
132
+ it ( 'should return with a status of 404 when there is an error deleting the training record from the database' , async ( ) => {
133
+ trainingRecord . restore . resolves ( true ) ;
134
+ trainingRecord . delete . resolves ( 0 ) ;
102
135
103
- await deleteTrainingRecord ( req , res ) ;
136
+ await deleteTrainingRecord ( req , res , trainingRecord , trainingCertificateService ) ;
104
137
105
- const response = res . _getData ( ) ;
138
+ const response = res . _getData ( ) ;
106
139
107
- expect ( res . statusCode ) . to . equal ( 404 ) ;
108
- expect ( response ) . to . equal ( 'Not Found' ) ;
109
- } ) ;
140
+ expect ( res . statusCode ) . to . equal ( 404 ) ;
141
+ expect ( response ) . to . equal ( 'Not Found' ) ;
110
142
} ) ;
111
143
} ) ;
112
144
} ) ;
0 commit comments