@@ -9,6 +9,8 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
9
9
let assessmentRepository_getStub ;
10
10
let assessmentRepository_updateLastQuestionDateStub ;
11
11
let assessmentRepository_updateWhenNewChallengeIsAskedStub ;
12
+ let answerRepository_findByAssessmentStub ;
13
+ let challengeRepository_getStub ;
12
14
let evaluationUsecases_getNextChallengeForPreviewStub ;
13
15
let evaluationUsecases_getNextChallengeForDemoStub ;
14
16
let evaluationUsecases_getNextChallengeForCampaignAssessmentStub ;
@@ -22,6 +24,8 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
22
24
assessmentRepository_getStub = sinon . stub ( ) . named ( 'get' ) ;
23
25
assessmentRepository_updateLastQuestionDateStub = sinon . stub ( ) . named ( 'updateLastQuestionDate' ) ;
24
26
assessmentRepository_updateWhenNewChallengeIsAskedStub = sinon . stub ( ) . named ( 'updateWhenNewChallengeIsAsked' ) ;
27
+ answerRepository_findByAssessmentStub = sinon . stub ( ) . named ( 'findByAssessment' ) ;
28
+ challengeRepository_getStub = sinon . stub ( ) . named ( 'get' ) ;
25
29
evaluationUsecases_getNextChallengeForPreviewStub = sinon . stub ( ) . named ( 'getNextChallengeForPreview' ) ;
26
30
evaluationUsecases_getNextChallengeForDemoStub = sinon . stub ( ) . named ( 'getNextChallengeForDemo' ) ;
27
31
evaluationUsecases_getNextChallengeForCampaignAssessmentStub = sinon
@@ -34,8 +38,11 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
34
38
. stub ( )
35
39
. named ( 'selectNextCertificationChallenge' ) ;
36
40
preventStubsToBeCalledUnexpectedly ( [
41
+ assessmentRepository_getStub ,
37
42
assessmentRepository_updateLastQuestionDateStub ,
38
43
assessmentRepository_updateWhenNewChallengeIsAskedStub ,
44
+ answerRepository_findByAssessmentStub ,
45
+ challengeRepository_getStub ,
39
46
evaluationUsecases_getNextChallengeForPreviewStub ,
40
47
evaluationUsecases_getNextChallengeForDemoStub ,
41
48
evaluationUsecases_getNextChallengeForCampaignAssessmentStub ,
@@ -49,6 +56,14 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
49
56
updateWhenNewChallengeIsAsked : assessmentRepository_updateWhenNewChallengeIsAskedStub ,
50
57
} ;
51
58
59
+ const answerRepository = {
60
+ findByAssessment : answerRepository_findByAssessmentStub ,
61
+ } ;
62
+
63
+ const challengeRepository = {
64
+ get : challengeRepository_getStub ,
65
+ } ;
66
+
52
67
const evaluationUsecases = {
53
68
getNextChallengeForPreview : evaluationUsecases_getNextChallengeForPreviewStub ,
54
69
getNextChallengeForDemo : evaluationUsecases_getNextChallengeForDemoStub ,
@@ -65,6 +80,8 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
65
80
userId,
66
81
locale,
67
82
assessmentRepository,
83
+ answerRepository,
84
+ challengeRepository,
68
85
evaluationUsecases,
69
86
certificationEvaluationRepository,
70
87
} ;
@@ -92,6 +109,75 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
92
109
} ) ;
93
110
} ) ;
94
111
112
+ context ( 'latest challenge asked' , function ( ) {
113
+ context ( 'when there are no challenge saved as latest challenge asked in assessment' , function ( ) {
114
+ it ( 'should compute next challenge' , async function ( ) {
115
+ const assessment = domainBuilder . buildAssessment ( {
116
+ state : Assessment . states . STARTED ,
117
+ type : Assessment . types . PREVIEW ,
118
+ lastChallengeId : null ,
119
+ } ) ;
120
+ assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
121
+ assessmentRepository_updateLastQuestionDateStub . resolves ( ) ;
122
+ assessmentRepository_updateWhenNewChallengeIsAskedStub . resolves ( ) ;
123
+ const challenge = domainBuilder . buildChallenge ( { id : 'challengeForPreview' } ) ;
124
+ evaluationUsecases_getNextChallengeForPreviewStub . withArgs ( { } ) . resolves ( challenge ) ;
125
+ answerRepository_findByAssessmentStub . withArgs ( assessment . id ) . resolves ( [ domainBuilder . buildAnswer ( ) ] ) ;
126
+
127
+ const actualNextChallenge = await getNextChallenge ( dependencies ) ;
128
+
129
+ expect ( actualNextChallenge ) . to . deepEqualInstance ( challenge ) ;
130
+ } ) ;
131
+ } ) ;
132
+
133
+ context ( 'when the latest challenge asked has been answered' , function ( ) {
134
+ it ( 'should compute next challenge' , async function ( ) {
135
+ const assessment = domainBuilder . buildAssessment ( {
136
+ state : Assessment . states . STARTED ,
137
+ type : Assessment . types . PREVIEW ,
138
+ lastChallengeId : 'previousChallengeId' ,
139
+ } ) ;
140
+ assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
141
+ assessmentRepository_updateLastQuestionDateStub . resolves ( ) ;
142
+ assessmentRepository_updateWhenNewChallengeIsAskedStub . resolves ( ) ;
143
+ const challenge = domainBuilder . buildChallenge ( { id : 'challengeForPreview' } ) ;
144
+ evaluationUsecases_getNextChallengeForPreviewStub . withArgs ( { } ) . resolves ( challenge ) ;
145
+ answerRepository_findByAssessmentStub
146
+ . withArgs ( assessment . id )
147
+ . resolves ( [ domainBuilder . buildAnswer ( { challengeId : 'previousChallengeId' } ) ] ) ;
148
+
149
+ const actualNextChallenge = await getNextChallenge ( dependencies ) ;
150
+
151
+ expect ( actualNextChallenge ) . to . deepEqualInstance ( challenge ) ;
152
+ } ) ;
153
+ } ) ;
154
+
155
+ context ( 'when the latest challenge asked has not been answered yet' , function ( ) {
156
+ it ( 'should return latest challenge' , async function ( ) {
157
+ const assessment = domainBuilder . buildAssessment ( {
158
+ state : Assessment . states . STARTED ,
159
+ type : Assessment . types . PREVIEW ,
160
+ lastChallengeId : 'previousChallengeId' ,
161
+ } ) ;
162
+ assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
163
+ assessmentRepository_updateLastQuestionDateStub . resolves ( ) ;
164
+ assessmentRepository_updateWhenNewChallengeIsAskedStub . resolves ( ) ;
165
+ const previousChallenge = domainBuilder . buildChallenge ( { id : 'previousChallengeId' } ) ;
166
+ answerRepository_findByAssessmentStub
167
+ . withArgs ( assessment . id )
168
+ . resolves ( [
169
+ domainBuilder . buildAnswer ( { challengeId : 'someChallengeId' } ) ,
170
+ domainBuilder . buildAnswer ( { challengeId : 'someOtherChallengeId' } ) ,
171
+ ] ) ;
172
+ challengeRepository_getStub . withArgs ( 'previousChallengeId' ) . resolves ( previousChallenge ) ;
173
+
174
+ const actualNextChallenge = await getNextChallenge ( dependencies ) ;
175
+
176
+ expect ( actualNextChallenge ) . to . deepEqualInstance ( previousChallenge ) ;
177
+ } ) ;
178
+ } ) ;
179
+ } ) ;
180
+
95
181
context ( 'Assessment types' , function ( ) {
96
182
context ( 'for assessment of type PREVIEW' , function ( ) {
97
183
let assessment ;
@@ -104,6 +190,7 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
104
190
assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
105
191
assessmentRepository_updateLastQuestionDateStub . resolves ( ) ;
106
192
assessmentRepository_updateWhenNewChallengeIsAskedStub . resolves ( ) ;
193
+ answerRepository_findByAssessmentStub . resolves ( [ ] ) ;
107
194
} ) ;
108
195
109
196
it ( 'should call usecase and return value from preview usecase' , async function ( ) {
@@ -126,6 +213,7 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
126
213
assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
127
214
assessmentRepository_updateLastQuestionDateStub . resolves ( ) ;
128
215
assessmentRepository_updateWhenNewChallengeIsAskedStub . resolves ( ) ;
216
+ answerRepository_findByAssessmentStub . resolves ( [ ] ) ;
129
217
} ) ;
130
218
131
219
it ( 'should call usecase and return value from demo usecase' , async function ( ) {
@@ -148,6 +236,7 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
148
236
assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
149
237
assessmentRepository_updateLastQuestionDateStub . resolves ( ) ;
150
238
assessmentRepository_updateWhenNewChallengeIsAskedStub . resolves ( ) ;
239
+ answerRepository_findByAssessmentStub . resolves ( [ ] ) ;
151
240
} ) ;
152
241
153
242
it ( 'should call usecase and return value from campaign usecase' , async function ( ) {
@@ -172,6 +261,7 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
172
261
assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
173
262
assessmentRepository_updateLastQuestionDateStub . resolves ( ) ;
174
263
assessmentRepository_updateWhenNewChallengeIsAskedStub . resolves ( ) ;
264
+ answerRepository_findByAssessmentStub . resolves ( [ ] ) ;
175
265
} ) ;
176
266
177
267
it ( 'should call usecase and return value from competence evaluation usecase' , async function ( ) {
@@ -197,6 +287,7 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
197
287
assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
198
288
assessmentRepository_updateLastQuestionDateStub . resolves ( ) ;
199
289
assessmentRepository_updateWhenNewChallengeIsAskedStub . resolves ( ) ;
290
+ answerRepository_findByAssessmentStub . resolves ( [ ] ) ;
200
291
} ) ;
201
292
202
293
it ( 'should call usecase and return value from certification usecase' , async function ( ) {
@@ -222,6 +313,7 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
222
313
assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
223
314
assessmentRepository_updateLastQuestionDateStub . resolves ( ) ;
224
315
assessmentRepository_updateWhenNewChallengeIsAskedStub . resolves ( ) ;
316
+ answerRepository_findByAssessmentStub . resolves ( [ ] ) ;
225
317
} ) ;
226
318
227
319
it ( 'should return null' , async function ( ) {
@@ -246,6 +338,7 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
246
338
} ) ;
247
339
evaluationUsecases_getNextChallengeForPreviewStub . withArgs ( { } ) . resolves ( { id : 'someChallengeId' } ) ;
248
340
assessmentRepository_updateWhenNewChallengeIsAskedStub . resolves ( ) ;
341
+ answerRepository_findByAssessmentStub . resolves ( [ ] ) ;
249
342
} ) ;
250
343
251
344
afterEach ( async function ( ) {
@@ -267,6 +360,7 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
267
360
context ( 'updating last challenge asked' , function ( ) {
268
361
beforeEach ( function ( ) {
269
362
assessmentRepository_updateLastQuestionDateStub . resolves ( ) ;
363
+ answerRepository_findByAssessmentStub . resolves ( [ ] ) ;
270
364
} ) ;
271
365
272
366
context ( 'when no next challenge has been found' , function ( ) {
@@ -293,6 +387,9 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
293
387
type : Assessment . types . PREVIEW ,
294
388
lastChallengeId : 'currentChallengeId' ,
295
389
} ) ;
390
+ answerRepository_findByAssessmentStub . resolves ( [
391
+ domainBuilder . buildAnswer ( { challengeId : 'currentChallengeId' } ) ,
392
+ ] ) ;
296
393
assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
297
394
evaluationUsecases_getNextChallengeForPreviewStub
298
395
. withArgs ( { } )
@@ -312,6 +409,9 @@ describe('Shared | Unit | Domain | Use Cases | get-next-challenge', function ()
312
409
type : Assessment . types . PREVIEW ,
313
410
lastChallengeId : 'previousChallengeId' ,
314
411
} ) ;
412
+ answerRepository_findByAssessmentStub . resolves ( [
413
+ domainBuilder . buildAnswer ( { challengeId : 'previousChallengeId' } ) ,
414
+ ] ) ;
315
415
assessmentRepository_getStub . withArgs ( assessmentId ) . resolves ( assessment ) ;
316
416
evaluationUsecases_getNextChallengeForPreviewStub
317
417
. withArgs ( { } )
0 commit comments