1
+ import { AssessmentEndedError } from '../../../../../src/shared/domain/errors.js' ;
1
2
import { Assessment } from '../../../../../src/shared/domain/models/index.js' ;
2
3
import { getNextChallenge } from '../../../../../src/shared/domain/usecases/get-next-challenge.js' ;
3
- import { domainBuilder , expect , preventStubsToBeCalledUnexpectedly , sinon } from '../../../../test-helper.js' ;
4
+ import { catchErr , domainBuilder , expect , preventStubsToBeCalledUnexpectedly , sinon } from '../../../../test-helper.js' ;
4
5
5
6
describe ( 'Shared | Unit | Domain | Use Cases | get-next-challenge' , function ( ) {
6
7
describe ( '#getNextChallenge' , function ( ) {
@@ -69,12 +70,37 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
69
70
} ;
70
71
} ) ;
71
72
73
+ context ( 'when assessment is not started' , function ( ) {
74
+ let assessment ;
75
+
76
+ beforeEach ( function ( ) {
77
+ assessment = domainBuilder . buildAssessment ( { id : assessmentId , type : Assessment . types . PREVIEW } ) ;
78
+ } ) ;
79
+
80
+ // eslint-disable-next-line mocha/no-setup-in-describe
81
+ Object . values ( Assessment . states )
82
+ . filter ( ( assessmentState ) => assessmentState !== Assessment . states . STARTED )
83
+ . forEach ( ( assessmentState ) => {
84
+ it ( `should throw a AssessmentEndedError when assessment is ${ assessmentState } ` , async function ( ) {
85
+ assessment . state = assessmentState ;
86
+ assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
87
+
88
+ const err = await catchErr ( getNextChallenge ) ( dependencies ) ;
89
+
90
+ expect ( err ) . to . be . instanceOf ( AssessmentEndedError ) ;
91
+ } ) ;
92
+ } ) ;
93
+ } ) ;
94
+
72
95
context ( 'Assessment types' , function ( ) {
73
96
context ( 'for assessment of type PREVIEW' , function ( ) {
74
97
let assessment ;
75
98
76
99
beforeEach ( function ( ) {
77
- assessment = domainBuilder . buildAssessment ( { type : Assessment . types . PREVIEW } ) ;
100
+ assessment = domainBuilder . buildAssessment ( {
101
+ state : Assessment . states . STARTED ,
102
+ type : Assessment . types . PREVIEW ,
103
+ } ) ;
78
104
assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
79
105
assessmentRepository_updateLastQuestionDateStub . resolves ( ) ;
80
106
assessmentRepository_updateWhenNewChallengeIsAskedStub . resolves ( ) ;
@@ -93,7 +119,10 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
93
119
let assessment ;
94
120
95
121
beforeEach ( function ( ) {
96
- assessment = domainBuilder . buildAssessment ( { type : Assessment . types . DEMO } ) ;
122
+ assessment = domainBuilder . buildAssessment ( {
123
+ state : Assessment . states . STARTED ,
124
+ type : Assessment . types . DEMO ,
125
+ } ) ;
97
126
assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
98
127
assessmentRepository_updateLastQuestionDateStub . resolves ( ) ;
99
128
assessmentRepository_updateWhenNewChallengeIsAskedStub . resolves ( ) ;
@@ -112,7 +141,10 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
112
141
let assessment ;
113
142
114
143
beforeEach ( function ( ) {
115
- assessment = domainBuilder . buildAssessment ( { type : Assessment . types . CAMPAIGN } ) ;
144
+ assessment = domainBuilder . buildAssessment ( {
145
+ state : Assessment . states . STARTED ,
146
+ type : Assessment . types . CAMPAIGN ,
147
+ } ) ;
116
148
assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
117
149
assessmentRepository_updateLastQuestionDateStub . resolves ( ) ;
118
150
assessmentRepository_updateWhenNewChallengeIsAskedStub . resolves ( ) ;
@@ -133,7 +165,10 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
133
165
let assessment ;
134
166
135
167
beforeEach ( function ( ) {
136
- assessment = domainBuilder . buildAssessment ( { type : Assessment . types . COMPETENCE_EVALUATION } ) ;
168
+ assessment = domainBuilder . buildAssessment ( {
169
+ state : Assessment . states . STARTED ,
170
+ type : Assessment . types . COMPETENCE_EVALUATION ,
171
+ } ) ;
137
172
assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
138
173
assessmentRepository_updateLastQuestionDateStub . resolves ( ) ;
139
174
assessmentRepository_updateWhenNewChallengeIsAskedStub . resolves ( ) ;
@@ -154,7 +189,11 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
154
189
let assessment ;
155
190
156
191
beforeEach ( function ( ) {
157
- assessment = domainBuilder . buildAssessment ( { id : assessmentId , type : Assessment . types . CERTIFICATION } ) ;
192
+ assessment = domainBuilder . buildAssessment ( {
193
+ state : Assessment . states . STARTED ,
194
+ id : assessmentId ,
195
+ type : Assessment . types . CERTIFICATION ,
196
+ } ) ;
158
197
assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
159
198
assessmentRepository_updateLastQuestionDateStub . resolves ( ) ;
160
199
assessmentRepository_updateWhenNewChallengeIsAskedStub . resolves ( ) ;
@@ -175,7 +214,11 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
175
214
let assessment ;
176
215
177
216
beforeEach ( function ( ) {
178
- assessment = domainBuilder . buildAssessment ( { id : assessmentId , type : 'coucou les zamis' } ) ;
217
+ assessment = domainBuilder . buildAssessment ( {
218
+ state : Assessment . states . STARTED ,
219
+ id : assessmentId ,
220
+ type : 'coucou les zamis' ,
221
+ } ) ;
179
222
assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
180
223
assessmentRepository_updateLastQuestionDateStub . resolves ( ) ;
181
224
assessmentRepository_updateWhenNewChallengeIsAskedStub . resolves ( ) ;
@@ -196,7 +239,11 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
196
239
197
240
beforeEach ( function ( ) {
198
241
clock = sinon . useFakeTimers ( new Date ( '2023-10-05' ) ) ;
199
- assessment = domainBuilder . buildAssessment ( { id : assessmentId , type : Assessment . types . PREVIEW } ) ;
242
+ assessment = domainBuilder . buildAssessment ( {
243
+ state : Assessment . states . STARTED ,
244
+ id : assessmentId ,
245
+ type : Assessment . types . PREVIEW ,
246
+ } ) ;
200
247
evaluationUsecases_getNextChallengeForPreviewStub . withArgs ( { } ) . resolves ( { id : 'someChallengeId' } ) ;
201
248
assessmentRepository_updateWhenNewChallengeIsAskedStub . resolves ( ) ;
202
249
} ) ;
@@ -205,22 +252,7 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
205
252
clock . restore ( ) ;
206
253
} ) ;
207
254
208
- // eslint-disable-next-line mocha/no-setup-in-describe
209
- Object . values ( Assessment . states )
210
- . filter ( ( assessmentState ) => assessmentState !== Assessment . states . STARTED )
211
- . forEach ( ( assessmentState ) => {
212
- it ( `should not update the last question date when assessment is in state ${ assessmentState } ` , async function ( ) {
213
- assessment . state = assessmentState ;
214
- assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
215
-
216
- await getNextChallenge ( dependencies ) ;
217
-
218
- expect ( assessmentRepository_updateLastQuestionDateStub ) . to . not . have . been . called ;
219
- } ) ;
220
- } ) ;
221
-
222
- it ( `should update the last question date when assessment is in state Started` , async function ( ) {
223
- assessment . state = Assessment . states . STARTED ;
255
+ it ( `should update the last question date` , async function ( ) {
224
256
assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
225
257
assessmentRepository_updateLastQuestionDateStub . resolves ( ) ;
226
258
@@ -239,7 +271,11 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
239
271
240
272
context ( 'when no next challenge has been found' , function ( ) {
241
273
it ( 'should not update last challenge asked' , async function ( ) {
242
- const assessment = domainBuilder . buildAssessment ( { id : assessmentId , type : Assessment . types . PREVIEW } ) ;
274
+ const assessment = domainBuilder . buildAssessment ( {
275
+ id : assessmentId ,
276
+ state : Assessment . states . STARTED ,
277
+ type : Assessment . types . PREVIEW ,
278
+ } ) ;
243
279
assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
244
280
evaluationUsecases_getNextChallengeForPreviewStub . withArgs ( { } ) . resolves ( null ) ;
245
281
@@ -253,6 +289,7 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
253
289
it ( 'should not update last challenge asked' , async function ( ) {
254
290
const assessment = domainBuilder . buildAssessment ( {
255
291
id : assessmentId ,
292
+ state : Assessment . states . STARTED ,
256
293
type : Assessment . types . PREVIEW ,
257
294
lastChallengeId : 'currentChallengeId' ,
258
295
} ) ;
@@ -271,6 +308,7 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
271
308
it ( 'should update last challenge asked' , async function ( ) {
272
309
const assessment = domainBuilder . buildAssessment ( {
273
310
id : assessmentId ,
311
+ state : Assessment . states . STARTED ,
274
312
type : Assessment . types . PREVIEW ,
275
313
lastChallengeId : 'previousChallengeId' ,
276
314
} ) ;
0 commit comments