forked from source-academy/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionReducer.test.ts
More file actions
648 lines (586 loc) · 16.4 KB
/
Copy pathSessionReducer.test.ts
File metadata and controls
648 lines (586 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
import { Chapter, Variant } from 'js-slang/dist/langs';
import { GradingOverview, GradingQuery } from '../../../../features/grading/GradingTypes';
import {
Assessment,
AssessmentOverview,
AssessmentStatuses,
ProgressStatuses
} from '../../../assessment/AssessmentTypes';
import { Notification } from '../../../notificationBadge/NotificationBadgeTypes';
import CommonsActions from '../../actions/CommonsActions';
import SessionActions from '../../actions/SessionActions';
import { defaultSession, GameState, Role, Story } from '../../ApplicationTypes';
import { SessionState } from '../../types/SessionTypes';
import { SessionsReducer } from '../SessionsReducer';
test('LOG_OUT works correctly on default session', () => {
const action = {
type: CommonsActions.logOut.type,
payload: {}
} as const;
const result: SessionState = SessionsReducer(defaultSession, action);
expect(result).toEqual(defaultSession);
});
test('SET_TOKEN sets accessToken and refreshToken correctly', () => {
const accessToken = 'access_token_test';
const refreshToken = 'refresh_token_test';
const action = {
type: SessionActions.setTokens.type,
payload: {
accessToken,
refreshToken
}
} as const;
const result: SessionState = SessionsReducer(defaultSession, action);
expect(result).toEqual({
...defaultSession,
...action.payload
});
});
test('SET_USER works correctly', () => {
const payload = {
userId: 123,
username: 'E1234567',
name: 'test student',
courses: [
{
courseId: 1,
courseName: `CS1101 Programming Methodology (AY20/21 Sem 1)`,
courseShortName: `CS1101S`,
viewable: true,
role: Role.Student
},
{
courseId: 2,
courseName: `CS2030S Programming Methodology II (AY20/21 Sem 2)`,
courseShortName: `CS2030S`,
viewable: true,
role: Role.Staff
}
]
};
const action = {
type: SessionActions.setUser.type,
payload
} as const;
const result: SessionState = SessionsReducer(defaultSession, action);
expect(result).toEqual({
...defaultSession,
...payload
});
});
test('SET_COURSE_CONFIGURATION works correctly', () => {
const payload = {
courseName: `CS1101 Programming Methodology (AY20/21 Sem 1)`,
courseShortName: `CS1101S`,
viewable: true,
enableGame: true,
enableAchievements: true,
enableSourcecast: true,
enableStories: false,
sourceChapter: Chapter.SOURCE_1,
sourceVariant: Variant.DEFAULT,
moduleHelpText: 'Help text',
assessmentTypes: ['Missions', 'Quests', 'Paths', 'Contests', 'Others']
};
const action = {
type: SessionActions.setCourseConfiguration.type,
payload
} as const;
const result: SessionState = SessionsReducer(defaultSession, action);
expect(result).toEqual({
...defaultSession,
...payload
});
});
test('SET_COURSE_REGISTRATION works correctly', () => {
const payload = {
role: Role.Student,
group: '42D',
gameState: {
collectibles: {},
completed_quests: []
} as GameState,
courseId: 1,
grade: 1,
maxGrade: 10,
xp: 1,
story: {
story: '',
playStory: false
} as Story,
agreedToReseach: true
};
const action = {
type: SessionActions.setCourseRegistration.type,
payload
} as const;
const result: SessionState = SessionsReducer(defaultSession, action);
expect(result).toEqual({
...defaultSession,
...payload
});
});
test('SET_ASSESSMENT_CONFIGURATIONS works correctly', () => {
const payload = [
{
assessmentConfigId: 1,
type: 'Mission1',
buildHidden: false,
buildSolution: false,
isContest: false,
hoursBeforeEarlyXpDecay: 48,
earlySubmissionXp: 200,
isManuallyGraded: false,
isGradingAutoPublished: false,
displayInDashboard: true,
isMinigame: false,
hasTokenCounter: false,
hasVotingFeatures: false,
isAutosaveEnabled: true
},
{
assessmentConfigId: 1,
type: 'Mission1',
buildHidden: false,
buildSolution: false,
isContest: false,
hoursBeforeEarlyXpDecay: 48,
earlySubmissionXp: 200,
isManuallyGraded: false,
isGradingAutoPublished: false,
displayInDashboard: true,
isMinigame: false,
hasTokenCounter: false,
hasVotingFeatures: false,
isAutosaveEnabled: true
},
{
assessmentConfigId: 1,
type: 'Mission1',
buildHidden: false,
buildSolution: false,
isContest: false,
hoursBeforeEarlyXpDecay: 48,
earlySubmissionXp: 200,
isManuallyGraded: false,
isGradingAutoPublished: false,
displayInDashboard: true,
isMinigame: false,
hasTokenCounter: false,
hasVotingFeatures: false,
isAutosaveEnabled: true
}
];
const action = {
type: SessionActions.setAssessmentConfigurations.type,
payload
} as const;
const result: SessionState = SessionsReducer(defaultSession, action);
expect(result).toEqual({
...defaultSession,
assessmentConfigurations: payload
});
});
test('SET_ADMIN_PANEL_COURSE_REGISTRATIONS works correctly', () => {
const payload = [
{
courseRegId: 1,
courseId: 1,
name: 'Bob',
username: 'E1234567',
role: Role.Student
},
{
courseRegId: 2,
courseId: 1,
name: 'Avenger',
username: 'E7654321',
role: Role.Staff
}
];
const action = {
type: SessionActions.setAdminPanelCourseRegistrations.type,
payload
} as const;
const result: SessionState = SessionsReducer(defaultSession, action);
expect(result).toEqual({
...defaultSession,
userCourseRegistrations: payload
});
});
test('SET_GITHUB_ACCESS_TOKEN works correctly', () => {
const token = 'githubAccessToken';
const action = {
type: SessionActions.setGitHubAccessToken.type,
payload: token
} as const;
const result: SessionState = SessionsReducer(defaultSession, action);
expect(result).toEqual({
...defaultSession,
githubAccessToken: token
});
});
// Test Data for UPDATE_ASSESSMENT
const assessmentTest1: Assessment = {
type: 'Mission',
globalDeployment: undefined,
graderDeployment: undefined,
id: 1,
longSummary: 'long summary here',
missionPDF: 'www.google.com',
questions: [],
title: 'first assessment'
};
const assessmentTest2: Assessment = {
type: 'Contest',
globalDeployment: undefined,
graderDeployment: undefined,
id: 1,
longSummary: 'another long summary',
missionPDF: 'www.comp.nus.edu.sg',
questions: [],
title: 'updated first assessment'
};
const assessmentTest3: Assessment = {
type: 'Path',
globalDeployment: undefined,
graderDeployment: undefined,
id: 3,
longSummary: 'another long summary here',
missionPDF: 'www.yahoo.com',
questions: [],
title: 'path'
};
test('UPDATE_ASSESSMENT works correctly in inserting assessment', () => {
const action = {
type: SessionActions.updateAssessment.type,
payload: assessmentTest1
} as const;
const resultMap = SessionsReducer(defaultSession, action).assessments;
expect(resultMap[assessmentTest1.id]).toEqual(assessmentTest1);
});
test('UPDATE_ASSESSMENT works correctly in inserting assessment and retains old data', () => {
const assessments: { [id: number]: Assessment } = {};
assessments[assessmentTest3.id] = assessmentTest3;
const newDefaultSession: SessionState = {
...defaultSession,
assessments
};
const action = {
type: SessionActions.updateAssessment.type,
payload: assessmentTest2
} as const;
const resultMap = SessionsReducer(newDefaultSession, action).assessments;
expect(resultMap[assessmentTest2.id]).toEqual(assessmentTest2);
expect(resultMap[assessmentTest3.id]).toEqual(assessmentTest3);
});
test('UPDATE_ASSESSMENT works correctly in updating assessment', () => {
const assessments: { [id: number]: Assessment } = {};
assessments[assessmentTest1.id] = assessmentTest1;
const newDefaultSession = {
...defaultSession,
assessments
};
const action = {
type: SessionActions.updateAssessment.type,
payload: assessmentTest2
} as const;
const resultMap = SessionsReducer(newDefaultSession, action).assessments;
expect(resultMap[assessmentTest2.id]).toEqual(assessmentTest2);
});
// Test data for UPDATE_ASSESSMENT_OVERVIEWS
const assessmentOverviewsTest1: AssessmentOverview[] = [
{
type: 'Missions',
isManuallyGraded: true,
isPublished: false,
closeAt: 'test_string',
coverImage: 'test_string',
id: 0,
maxXp: 0,
earlySubmissionXp: 0,
openAt: 'test_string',
title: 'test_string',
shortSummary: 'test_string',
status: AssessmentStatuses.not_attempted,
story: null,
xp: 0,
isGradingPublished: false,
maxTeamSize: 5,
hasVotingFeatures: false,
hoursBeforeEarlyXpDecay: 0
}
];
const assessmentOverviewsTest2: AssessmentOverview[] = [
{
type: 'Contests',
isManuallyGraded: true,
isPublished: false,
closeAt: 'test_string_0',
coverImage: 'test_string_0',
fileName: 'test_sting_0',
id: 1,
maxXp: 1,
earlySubmissionXp: 0,
openAt: 'test_string_0',
title: 'test_string_0',
shortSummary: 'test_string_0',
status: AssessmentStatuses.attempted,
story: null,
xp: 1,
isGradingPublished: false,
maxTeamSize: 1,
hasVotingFeatures: false,
hoursBeforeEarlyXpDecay: 0
}
];
test('UPDATE_ASSESSMENT_OVERVIEWS works correctly in inserting assessment overviews', () => {
const action = {
type: SessionActions.updateAssessmentOverviews.type,
payload: assessmentOverviewsTest1
} as const;
const result: SessionState = SessionsReducer(defaultSession, action);
expect(result).toEqual({
...defaultSession,
assessmentOverviews: assessmentOverviewsTest1
});
});
test('UPDATE_ASSESSMENT_OVERVIEWS works correctly in updating assessment overviews', () => {
const newDefaultSession = {
...defaultSession,
assessmentOverviews: assessmentOverviewsTest1
};
const assessmentOverviewsPayload = [...assessmentOverviewsTest2, ...assessmentOverviewsTest1];
const action = {
type: SessionActions.updateAssessmentOverviews.type,
payload: assessmentOverviewsPayload
} as const;
const result: SessionState = SessionsReducer(newDefaultSession, action);
expect(result).toEqual({
...defaultSession,
assessmentOverviews: assessmentOverviewsPayload
});
});
// Test data for UPDATE_GRADING
const gradingTest1: GradingQuery = {
enable_llm_grading: false,
answers: [
{
id: 0,
prompts: [],
autogradingResults: [],
autoGradingStatus: 'N/A',
question: await vi.importMock('../../../../features/grading/GradingTypes'),
student: {
name: 'test student',
username: 'E0123456',
id: 234
},
grade: {
xp: 100,
xpAdjustment: 0,
comments: 'Well done. Please try the quest!'
}
}
],
assessment: {
coverPicture: 'test string',
id: 1,
number: 'M1A',
reading: 'test string',
story: 'test string',
summaryLong: 'test string',
summaryShort: 'test string',
title: 'test string'
}
};
const gradingTest2: GradingQuery = {
enable_llm_grading: false,
answers: [
{
id: 0,
prompts: [],
autogradingResults: [],
autoGradingStatus: 'N/A',
question: await vi.importMock('../../../../features/grading/GradingTypes'),
student: {
name: 'another test student',
username: 'E0000000',
id: 345
},
grade: {
xp: 500,
xpAdjustment: 20,
comments: 'Good job! All the best for the finals.'
}
}
],
assessment: {
coverPicture: 'another test string',
id: 2,
number: 'P2',
reading: 'another test string',
story: 'another test string',
summaryLong: 'another test string',
summaryShort: 'another test string',
title: 'another test string'
}
};
test('UPDATE_GRADING works correctly in inserting gradings', () => {
const submissionId = 23;
const action = {
type: SessionActions.updateGrading.type,
payload: {
submissionId,
grading: gradingTest1
}
} as const;
const gradingMap = SessionsReducer(defaultSession, action).gradings;
expect(gradingMap[submissionId]).toEqual(gradingTest1);
});
test('UPDATE_GRADING works correctly in inserting gradings and retains old data', () => {
const submissionId1 = 45;
const submissionId2 = 56;
const gradings: { [id: number]: GradingQuery } = {};
gradings[submissionId1] = gradingTest1;
const newDefaultSession = {
...defaultSession,
gradings
};
const action = {
type: SessionActions.updateGrading.type,
payload: {
submissionId: submissionId2,
grading: gradingTest2
}
} as const;
const gradingMap = SessionsReducer(newDefaultSession, action).gradings;
expect(gradingMap[submissionId1]).toEqual(gradingTest1);
expect(gradingMap[submissionId2]).toEqual(gradingTest2);
});
test('UPDATE_GRADING works correctly in updating gradings', () => {
const submissionId = 23;
const gradings: { [id: number]: GradingQuery } = {};
gradings[submissionId] = gradingTest1;
const newDefaultSession = {
...defaultSession,
gradings
};
const action = {
type: SessionActions.updateGrading.type,
payload: {
submissionId,
grading: gradingTest2
}
} as const;
const gradingMap = SessionsReducer(newDefaultSession, action).gradings;
expect(gradingMap[submissionId]).toEqual(gradingTest2);
});
// UPDATE_GRADING_OVERVIEWS test data
const gradingOverviewTest1: GradingOverview[] = [
{
assessmentId: 1,
assessmentNumber: 'M1A',
assessmentName: 'test assessment',
assessmentType: 'Contests',
initialXp: 0,
xpBonus: 100,
xpAdjustment: 50,
currentXp: 50,
maxXp: 500,
studentId: 100,
studentName: 'test student',
studentNames: [],
studentUsername: 'E0123456',
studentUsernames: [],
submissionId: 1,
submissionStatus: AssessmentStatuses.attempting,
progress: ProgressStatuses.attempting,
isGradingPublished: false,
groupName: 'group',
questionCount: 4,
gradedCount: 2
}
];
const gradingOverviewTest2: GradingOverview[] = [
{
assessmentId: 2,
assessmentNumber: 'P2',
assessmentName: 'another assessment',
assessmentType: 'Quests',
initialXp: 20,
xpBonus: 250,
xpAdjustment: 100,
currentXp: 300,
maxXp: 1000,
studentId: 20,
studentName: 'another student',
studentNames: [],
studentUsername: 'E0000000',
studentUsernames: [],
submissionId: 2,
submissionStatus: AssessmentStatuses.attempted,
progress: ProgressStatuses.graded,
isGradingPublished: false,
groupName: 'another group',
questionCount: 3,
gradedCount: 3
}
];
test('UPDATE_GRADING_OVERVIEWS works correctly in inserting grading overviews', () => {
const action = {
type: SessionActions.updateGradingOverviews.type,
payload: {
count: gradingOverviewTest1.length,
data: gradingOverviewTest1
}
} as const;
const result: SessionState = SessionsReducer(defaultSession, action);
expect(result.gradingOverviews).toEqual({
count: gradingOverviewTest1.length,
data: gradingOverviewTest1
});
});
test('UPDATE_GRADING_OVERVIEWS works correctly in updating grading overviews', () => {
const newDefaultSession = {
...defaultSession,
gradingOverviews: {
count: gradingOverviewTest1.length,
data: gradingOverviewTest1
}
};
const gradingOverviewsPayload = {
count: gradingOverviewTest1.length + gradingOverviewTest2.length,
data: [...gradingOverviewTest2, ...gradingOverviewTest1]
};
const action = {
type: SessionActions.updateGradingOverviews.type,
payload: gradingOverviewsPayload
} as const;
const result: SessionState = SessionsReducer(newDefaultSession, action);
expect(result.gradingOverviews).toEqual(gradingOverviewsPayload);
});
test('UPDATE_NOTIFICATIONS works correctly in updating notifications', () => {
const notifications: Notification[] = [
{
id: 1,
type: 'new',
assessment_id: 1,
assessment_type: 'Mission',
assessment_title: 'The Secret to Streams'
},
{
id: 2,
type: 'new',
assessment_id: 2,
assessment_type: 'Sidequest',
assessment_title: 'A sample Sidequest'
}
];
const action = {
type: SessionActions.updateNotifications.type,
payload: notifications
} as const;
const result: SessionState = SessionsReducer(defaultSession, action);
expect(result.notifications).toEqual(notifications);
});