@pie-lib/categorize 0.8.17-next.0 → 0.9.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,953 @@
1
+ import {
2
+ countChosen,
3
+ buildState,
4
+ moveChoiceToCategory,
5
+ removeChoiceFromCategory,
6
+ ensureNoExtraChoicesInAnswer,
7
+ limitInArray,
8
+ limitInArrays,
9
+ removeAllChoices,
10
+ buildCategories,
11
+ buildChoices,
12
+ getAllPossibleResponses,
13
+ moveChoiceToAlternate,
14
+ } from '../index';
15
+ import range from 'lodash/range';
16
+ import util from 'util';
17
+ import debug from 'debug';
18
+ import { cat, cats, choice, answer } from './utils';
19
+
20
+ const log = debug('@pie-lib:drag:test');
21
+
22
+ const inspect = (o) => util.inspect(o, { color: true });
23
+
24
+ describe('categorize', () => {
25
+ describe('countChosen', () => {
26
+ const assert = (choice, categories, expected) => {
27
+ const l = (categories || []).map((c) => c.choices || []);
28
+
29
+ it(`${JSON.stringify(choice)} + ${JSON.stringify(l)} = ${expected}`, () => {
30
+ expect(countChosen(choice, categories)).toEqual(expected);
31
+ });
32
+ };
33
+
34
+ assert(null, null, 0);
35
+ assert({}, null, 0);
36
+ assert({ id: '1' }, [], 0);
37
+ assert({ id: '1' }, [{ choices: undefined }], 0);
38
+ assert({ id: '1' }, [cats(['1'])], 1);
39
+ assert({ id: '1' }, [cats(['1', '1'])], 2);
40
+ assert({ id: '1' }, [cats(['1', '2'])], 1);
41
+ assert({ id: '1' }, [cats(['1']), cats(['1'])], 2);
42
+ });
43
+
44
+ describe('limitInArrays', () => {
45
+ const assert = (id, arrays, limit, expected) => {
46
+ it(`${id}, ${inspect(arrays)} -> ${inspect(expected)}`, () => {
47
+ const result = limitInArrays(id, arrays, limit);
48
+ expect(result).toEqual(expected);
49
+ });
50
+ };
51
+ assert('1', [['1'], ['1']], 1, [['1'], []]);
52
+ assert('1', [['1'], ['1']], 2, [['1'], ['1']]);
53
+ assert('1', [['1'], ['1'], ['1', '2']], 2, [['1'], ['1'], ['2']]);
54
+ assert('1', [undefined, [], null], 2, [[], [], []]);
55
+ assert('1', undefined, 2, []);
56
+ assert('1', null, 2, []);
57
+ assert('1', [], 2, []);
58
+ });
59
+
60
+ describe('limitInArray', () => {
61
+ it('', () => {
62
+ expect(limitInArray('1', ['1', '1'], 1)).toEqual({
63
+ array: ['1'],
64
+ count: 1,
65
+ });
66
+ expect(limitInArray('1', ['1', '1'], 0)).toEqual({
67
+ array: [],
68
+ count: 2,
69
+ });
70
+ expect(limitInArray('1', ['1', '2', '1'], 1)).toEqual({
71
+ array: ['1', '2'],
72
+ count: 1,
73
+ });
74
+ expect(limitInArray('1', undefined, 1)).toEqual({
75
+ array: [],
76
+ count: 0,
77
+ });
78
+ expect(limitInArray('1', null, 1)).toEqual({
79
+ array: [],
80
+ count: 0,
81
+ });
82
+ expect(limitInArray('1', [], 1)).toEqual({
83
+ array: [],
84
+ count: 0,
85
+ });
86
+ });
87
+ });
88
+
89
+ describe('removeAllChoices', () => {
90
+ it('strips if there is no category id', () => {
91
+ const result = removeAllChoices('1', [{ choices: ['1'] }]);
92
+ expect(result).toEqual([{ choices: [] }]);
93
+ });
94
+
95
+ it('strips if the category id matches', () => {
96
+ const result = removeAllChoices('1', [{ category: '2', choices: ['1'] }], '2');
97
+ expect(result).toEqual([{ category: '2', choices: [] }]);
98
+ });
99
+
100
+ it('doesnt strip if the category id does not match', () => {
101
+ const result = removeAllChoices('1', [{ category: '3', choices: ['1'] }], '2');
102
+ expect(result).toEqual([{ category: '3', choices: ['1'] }]);
103
+ });
104
+ });
105
+
106
+ describe('ensureNoExtraChoicesInAnswer', () => {
107
+ const _assert = (only) => (answer, choices, expected) => {
108
+ const fn = only ? it.only : it;
109
+ fn(`${inspect(answer)} + ${inspect(choices)} == ${inspect(expected)}`, () => {
110
+ const result = ensureNoExtraChoicesInAnswer(answer, choices);
111
+ log('result: ', result);
112
+ expect(result).toEqual(expected);
113
+ });
114
+ };
115
+ const assert = _assert(false);
116
+ assert.only = _assert(true);
117
+
118
+ assert([answer('1', ['1', '2', '1'])], [{ id: '1' }, { id: '2' }], [answer('1', ['1', '2', '1'])]);
119
+
120
+ assert([answer('1', ['1', '2', '1'])], [{ id: '1', categoryCount: 1 }, { id: '2' }], [answer('1', ['1', '2'])]);
121
+
122
+ assert(
123
+ [answer('1', ['1', '2', '1', '2'])],
124
+ [
125
+ { id: '1', categoryCount: 0 },
126
+ { id: '2', categoryCount: 1 },
127
+ ],
128
+ [answer('1', ['1', '2', '1'])],
129
+ );
130
+ assert([answer('1', ['1', '2', '1', '2'])], [choice('1'), choice('2', 1)], [answer('1', ['1', '2', '1'])]);
131
+ assert([answer('1', ['1', '2', '1'])], [choice('1')], [answer('1', ['1', '2', '1'])]);
132
+ assert([answer('1', ['1', '2', '1'])], [choice('1', 1)], [answer('1', ['1', '2'])]);
133
+ assert(
134
+ [answer('1', ['3', '3', '1', '2', '1', '2', '3'])],
135
+ [choice('1'), choice('2', 1), choice('3', 2)],
136
+ [answer('1', ['3', '3', '1', '2', '1'])],
137
+ );
138
+
139
+ assert([answer('1', ['1', '1'])], [choice('1', 0)], [answer('1', ['1', '1'])]);
140
+
141
+ assert(
142
+ [
143
+ answer(
144
+ '1',
145
+ range(0, 10).map((r) => '1'),
146
+ ),
147
+ ],
148
+ [choice('1', 0)],
149
+ [
150
+ answer(
151
+ '1',
152
+ range(0, 10).map((r) => '1'),
153
+ ),
154
+ ],
155
+ );
156
+ assert([answer('1', ['1', '1'])], [choice('1', 1)], [answer('1', ['1'])]);
157
+
158
+ assert([answer('1', ['1']), answer('2', ['1'])], [choice('1', 1)], [answer('1', ['1']), answer('2', [])]);
159
+
160
+ assert([answer('1', ['1', '1', '1'])], [choice('1', 2)], [answer('1', ['1', '1'])]);
161
+
162
+ assert([answer('1', ['1']), answer('2', ['1', '1'])], [choice('1', 2)], [answer('1', ['1']), answer('2', ['1'])]);
163
+
164
+ assert(
165
+ [answer('1', ['1']), answer('2', ['1', '1']), answer('3', ['1'])],
166
+ [choice('1', 2)],
167
+ [answer('1', ['1']), answer('2', ['1']), answer('3', [])],
168
+ );
169
+
170
+ assert([answer('1', ['1', '1', '1'])], [], [answer('1', ['1', '1', '1'])]);
171
+
172
+ assert([answer('1', ['1', '1', '1'])], undefined, [answer('1', ['1', '1', '1'])]);
173
+
174
+ assert([answer('1', ['1', '1', '1'])], null, [answer('1', ['1', '1', '1'])]);
175
+ });
176
+
177
+ describe('countChosen', () => {
178
+ const assert = (choice, categories, expected) => {
179
+ const l = (categories || []).map((c) => c.choices || []);
180
+
181
+ it(`${JSON.stringify(choice)} + ${JSON.stringify(l)} = ${expected}`, () => {
182
+ expect(countChosen(choice, categories)).toEqual(expected);
183
+ });
184
+ };
185
+
186
+ assert(null, undefined, 0);
187
+ assert(null, [], 0);
188
+ assert(null, null, 0);
189
+ assert({}, null, 0);
190
+ assert({ id: '1' }, [], 0);
191
+ assert({ id: '1' }, [{ choices: undefined }], 0);
192
+ assert({ id: '1' }, [cats(['1'])], 1);
193
+ assert({ id: '1' }, [cats(['1', '1'])], 2);
194
+ assert({ id: '1' }, [cats(['1', '2'])], 1);
195
+ assert({ id: '1' }, [cats(['1']), cats(['1'])], 2);
196
+ });
197
+
198
+ describe('removeChoiceFromCategory', () => {
199
+ const assert = (choiceId, categoryId, choiceIndex, answers, expected) => {
200
+ it(`remove choice:${choiceId} from category:${categoryId} == ${util.inspect(expected, {
201
+ colors: true,
202
+ })}`, () => {
203
+ const result = removeChoiceFromCategory(choiceId, categoryId, choiceIndex, answers);
204
+ expect(result).toEqual(expected);
205
+ });
206
+ };
207
+ assert('1', '1', 0, [answer('1', ['1'])], [answer('1', [])]);
208
+
209
+ assert('1', '1', 0, [answer('1', ['1']), answer('2', ['1', '1'])], [answer('1', []), answer('2', ['1', '1'])]);
210
+
211
+ assert('1', '1', 3, [answer('1', ['1', '2', '3', '1'])], [answer('1', ['1', '2', '3'])]);
212
+ });
213
+
214
+ describe('moveChoiceToCategory', () => {
215
+ const assert = (choiceId, from, to, choiceIndex, answers, expected) => {
216
+ it(`move choice:${choiceId} from category:${from} -> category:${to} == ${util.inspect(expected, {
217
+ colors: true,
218
+ })}`, () => {
219
+ const result = moveChoiceToCategory(choiceId, from, to, choiceIndex, answers);
220
+ expect(result).toEqual(expected);
221
+ });
222
+ };
223
+ assert('1', '1', '2', 0, [answer('1', ['1'])], [answer('1', []), answer('2', ['1'])]);
224
+ assert('1', undefined, '2', 0, [answer('1', ['1'])], [answer('1', ['1']), answer('2', ['1'])]);
225
+ });
226
+
227
+ describe('moveChoiceToAlternate', () => {
228
+ const assert = (choiceId, from, to, choiceIndex, answers, alternateIndex, categoryCount, expected) => {
229
+ it(`move choice:${choiceId} from category:${from} -> category:${to} == ${util.inspect(expected, {
230
+ colors: true,
231
+ })}`, () => {
232
+ const result = moveChoiceToAlternate(choiceId, from, to, choiceIndex, answers, alternateIndex, categoryCount);
233
+ expect(result).toEqual(expected);
234
+ });
235
+ };
236
+ assert(
237
+ '2',
238
+ '1',
239
+ '2',
240
+ 0,
241
+ [
242
+ answer(
243
+ '1',
244
+ ['1'],
245
+ [
246
+ ['1', '2'],
247
+ ['1', '1', '2'],
248
+ ],
249
+ ),
250
+ answer(
251
+ '2',
252
+ ['1'],
253
+ [
254
+ ['1', '2', '2'],
255
+ ['1', '1'],
256
+ ],
257
+ ),
258
+ ],
259
+ 1,
260
+ 0,
261
+ [
262
+ answer(
263
+ '1',
264
+ ['1'],
265
+ [
266
+ ['1', '2'],
267
+ ['1', '1', '2'],
268
+ ],
269
+ ),
270
+ answer(
271
+ '2',
272
+ ['1'],
273
+ [
274
+ ['1', '2', '2'],
275
+ ['1', '1', '2'],
276
+ ],
277
+ ),
278
+ ],
279
+ );
280
+ assert(
281
+ '2',
282
+ '1',
283
+ '2',
284
+ 0,
285
+ [
286
+ answer(
287
+ '1',
288
+ ['1'],
289
+ [
290
+ ['1', '2'],
291
+ ['1', '1', '2'],
292
+ ],
293
+ ),
294
+ answer(
295
+ '2',
296
+ ['1'],
297
+ [
298
+ ['1', '2', '2'],
299
+ ['1', '1'],
300
+ ],
301
+ ),
302
+ ],
303
+ 1,
304
+ 1,
305
+ [
306
+ answer(
307
+ '1',
308
+ ['1'],
309
+ [
310
+ ['1', '2'],
311
+ ['1', '1'],
312
+ ],
313
+ ),
314
+ answer(
315
+ '2',
316
+ ['1'],
317
+ [
318
+ ['1', '2', '2'],
319
+ ['1', '1', '2'],
320
+ ],
321
+ ),
322
+ ],
323
+ );
324
+ });
325
+
326
+ describe('buildState', () => {
327
+ const _a = (only, label, categories, choices, answers, correctResponse, expected) => {
328
+ const fn = only ? it.only : it;
329
+ fn(label, () => {
330
+ const result = buildState(categories, choices, answers, correctResponse);
331
+ expect(result).toMatchObject(expected);
332
+ });
333
+ };
334
+ const assert = _a.bind(_a, false);
335
+ assert.only = _a.bind(_a, true);
336
+
337
+ assert('1 category, 1 choice, 1 answer, no correct response', [cat('1')], [choice('1')], [answer('1', '1')], [], {
338
+ categories: [{ ...cat('1'), choices: [choice('1')] }],
339
+ choices: [choice('1')],
340
+ });
341
+
342
+ assert('1 category, 1 unlimited choice, no answer, no correct response', [cat('1')], [choice('2')], [], [], {
343
+ categories: [{ ...cat('1'), choices: [] }],
344
+ choices: [choice('2')],
345
+ });
346
+
347
+ assert(
348
+ '2 categories, 1 unlimited choice, 2 answers, no correct response',
349
+ [cat('1'), cat('2')],
350
+ [choice('1', 0)],
351
+ [answer('1', ['1']), answer('2', ['1'])],
352
+ [],
353
+ {
354
+ categories: [
355
+ { ...cat('1'), choices: [choice('1', 0)] },
356
+ { ...cat('2'), choices: [choice('1', 0)] },
357
+ ],
358
+ choices: [choice('1', 0)],
359
+ },
360
+ );
361
+
362
+ assert(
363
+ '2 categories, 1 choice, 1 answer, no correct response',
364
+ [cat('1'), cat('2')],
365
+ [choice('1', 0)],
366
+ [answer('1', ['1'])],
367
+ [],
368
+ {
369
+ categories: [
370
+ { ...cat('1'), choices: [choice('1', 0)] },
371
+ {
372
+ ...cat('2'),
373
+ choices: [],
374
+ },
375
+ ],
376
+ choices: [{ ...choice('1', 0) }],
377
+ },
378
+ );
379
+
380
+ assert(
381
+ '2 categories, 1 choices, 1 incorrect answer, 1 correct response',
382
+ [cat('1'), cat('2')],
383
+ [choice('1', 0)],
384
+ [answer('1', ['1'])],
385
+ [answer('2', ['1'])],
386
+ {
387
+ correct: false,
388
+ categories: [
389
+ { ...cat('1'), choices: [choice('1', 0, false)], correct: false },
390
+ {
391
+ ...cat('2'),
392
+ choices: [],
393
+ },
394
+ ],
395
+ choices: [{ ...choice('1', 0) }],
396
+ },
397
+ );
398
+
399
+ assert(
400
+ '1 cat, 1 choice, 1 answer, 1 correct response',
401
+ [cat('1')],
402
+ [choice('1', 0)],
403
+ [answer('1', ['1'])],
404
+ [answer('1', ['1'])],
405
+ {
406
+ correct: true,
407
+ categories: [
408
+ {
409
+ ...cat('1'),
410
+ choices: [choice('1', 0, true)],
411
+ correct: true,
412
+ },
413
+ ],
414
+ choices: [{ ...choice('1', 0) }],
415
+ },
416
+ );
417
+
418
+ assert(
419
+ '1 cat, 1 choice, 2 same answers (correct), 2 correct responses',
420
+ [cat('1')],
421
+ [choice('1', 0)],
422
+ [answer('1', ['1', '1'])],
423
+ [answer('1', ['1', '1'])],
424
+ {
425
+ correct: true,
426
+ categories: [
427
+ {
428
+ ...cat('1'),
429
+ choices: [choice('1', 0, true), choice('1', 0, true)],
430
+ correct: true,
431
+ },
432
+ ],
433
+ choices: [{ ...choice('1', 0) }],
434
+ },
435
+ );
436
+
437
+ assert(
438
+ '1 cat, 1 choice, 2 same answers (1 correct, 1 incorrect), correct response',
439
+ [cat('1')],
440
+ [choice('1', 0)],
441
+ [answer('1', ['1', '1'])],
442
+ [answer('1', ['1'])],
443
+ {
444
+ correct: false,
445
+ categories: [
446
+ {
447
+ ...cat('1'),
448
+ choices: [choice('1', 0, true), choice('1', 0, false)],
449
+ correct: false,
450
+ },
451
+ ],
452
+ choices: [{ ...choice('1', 0) }],
453
+ },
454
+ );
455
+
456
+ assert(
457
+ '2 categories, 1 limited choice , 1 correct answer, correct response',
458
+ [cat('1'), cat('2')],
459
+ [choice('1', 1)],
460
+ [answer('1', ['1'])],
461
+ [answer('1', ['1'])],
462
+ {
463
+ correct: true,
464
+ categories: [
465
+ { ...cat('1'), choices: [choice('1', 1, true)], correct: true },
466
+ { ...cat('2'), correct: true },
467
+ ],
468
+ choices: [{ empty: true }],
469
+ },
470
+ );
471
+
472
+ assert(
473
+ '2 categories, 1 limited choice , empty answer, correct response',
474
+ [cat('1'), cat('2')],
475
+ [choice('1', 1)],
476
+ [],
477
+ [answer('1', ['1'])],
478
+ {
479
+ correct: false,
480
+ categories: [
481
+ { ...cat('1'), choices: [], correct: false },
482
+ { ...cat('2'), choices: [], correct: true },
483
+ ],
484
+ choices: [choice('1', 1)],
485
+ },
486
+ );
487
+
488
+ assert(
489
+ '1 cat, 2 choices, 1 answer, 1 correct response, 1 alternate response',
490
+ [cat('1')],
491
+ [choice('1', 0), choice('2', 0)],
492
+ [answer('1', ['2'])],
493
+ [answer('1', ['1'], [['2']])],
494
+ {
495
+ correct: true,
496
+ categories: [
497
+ {
498
+ ...cat('1'),
499
+ choices: [choice('2', 0, true)],
500
+ correct: true,
501
+ },
502
+ ],
503
+ choices: [{ ...choice('1', 0) }, { ...choice('2', 0) }],
504
+ },
505
+ );
506
+ });
507
+
508
+ // new tests
509
+
510
+ const baseCategories = [
511
+ { id: 's10', label: 'SUM=10' },
512
+ { id: 's11', label: 'SUM=11' },
513
+ ];
514
+ const baseChoices = [
515
+ { id: '3', content: '3' },
516
+ { id: '4', content: '4' },
517
+ { id: '5', content: '5' },
518
+ { id: '6', content: '6', categoryCount: 1 },
519
+ { id: '7', content: '7', categoryCount: 2 },
520
+ ];
521
+ const correctResponseNoAlternates = [
522
+ { category: 's10', choices: ['3', '7'] },
523
+ { category: 's11', choices: ['4', '7'] },
524
+ ];
525
+ const correctResponseWithAlternates = [
526
+ {
527
+ category: 's10',
528
+ choices: ['3', '7'],
529
+ alternateResponses: [
530
+ ['4', '6'],
531
+ ['5', '5'],
532
+ ],
533
+ },
534
+ {
535
+ category: 's11',
536
+ choices: ['4', '7'],
537
+ alternateResponses: [
538
+ ['3', '3', '5'],
539
+ ['5', '6'],
540
+ ],
541
+ },
542
+ ];
543
+
544
+ describe('buildState', () => {
545
+ // main response selections
546
+ const aC1 = [
547
+ { category: 's10', choices: ['3', '7'] },
548
+ { category: 's11', choices: ['4', '7'] },
549
+ ];
550
+ const aC1BestResponse = { s10: ['3', '7'], s11: ['4', '7'] };
551
+ const aC1BuiltCategories = [
552
+ {
553
+ id: 's10',
554
+ label: 'SUM=10',
555
+ correct: true,
556
+ choices: [
557
+ { ...baseChoices[0], correct: true },
558
+ { ...baseChoices[4], correct: true },
559
+ ],
560
+ },
561
+ {
562
+ id: 's11',
563
+ label: 'SUM=11',
564
+ correct: true,
565
+ choices: [
566
+ { ...baseChoices[1], correct: true },
567
+ { ...baseChoices[4], correct: true },
568
+ ],
569
+ },
570
+ ];
571
+
572
+ // alternate 1 response
573
+ const aC2 = [
574
+ { category: 's10', choices: ['4', '6'] },
575
+ { category: 's11', choices: ['3', '3', '5'] },
576
+ ];
577
+ const aC2BestResponse = { s10: ['4', '6'], s11: ['3', '3', '5'] };
578
+ const aC2BuiltCategories = [
579
+ {
580
+ id: 's10',
581
+ label: 'SUM=10',
582
+ correct: true,
583
+ choices: [
584
+ { ...baseChoices[1], correct: true },
585
+ { ...baseChoices[3], correct: true },
586
+ ],
587
+ },
588
+ {
589
+ id: 's11',
590
+ label: 'SUM=11',
591
+ correct: true,
592
+ choices: [
593
+ { ...baseChoices[0], correct: true },
594
+ { ...baseChoices[0], correct: true },
595
+ {
596
+ ...baseChoices[2],
597
+ correct: true,
598
+ },
599
+ ],
600
+ },
601
+ ];
602
+ const aC2BuiltCategoriesNoAlt = [
603
+ {
604
+ id: 's10',
605
+ label: 'SUM=10',
606
+ correct: false,
607
+ choices: [
608
+ { ...baseChoices[1], correct: false },
609
+ { ...baseChoices[3], correct: false },
610
+ ],
611
+ },
612
+ {
613
+ id: 's11',
614
+ label: 'SUM=11',
615
+ correct: false,
616
+ choices: [
617
+ { ...baseChoices[0], correct: false },
618
+ { ...baseChoices[0], correct: false },
619
+ {
620
+ ...baseChoices[2],
621
+ correct: false,
622
+ },
623
+ ],
624
+ },
625
+ ];
626
+
627
+ // alternate 2 response
628
+ const aC3 = [
629
+ { category: 's10', choices: ['5', '5'] },
630
+ { category: 's11', choices: ['5', '6'] },
631
+ ];
632
+ const aC3BestResponse = { s10: ['5', '5'], s11: ['5', '6'] };
633
+ const aC3BuiltCategories = [
634
+ {
635
+ id: 's10',
636
+ label: 'SUM=10',
637
+ correct: true,
638
+ choices: [
639
+ { ...baseChoices[2], correct: true },
640
+ { ...baseChoices[2], correct: true },
641
+ ],
642
+ },
643
+ {
644
+ id: 's11',
645
+ label: 'SUM=11',
646
+ correct: true,
647
+ choices: [
648
+ { ...baseChoices[2], correct: true },
649
+ { ...baseChoices[3], correct: true },
650
+ ],
651
+ },
652
+ ];
653
+ const aC3BuiltCategoriesNoAlt = [
654
+ {
655
+ id: 's10',
656
+ label: 'SUM=10',
657
+ correct: false,
658
+ choices: [
659
+ { ...baseChoices[2], correct: false },
660
+ { ...baseChoices[2], correct: false },
661
+ ],
662
+ },
663
+ {
664
+ id: 's11',
665
+ label: 'SUM=11',
666
+ correct: false,
667
+ choices: [
668
+ { ...baseChoices[2], correct: false },
669
+ { ...baseChoices[3], correct: false },
670
+ ],
671
+ },
672
+ ];
673
+
674
+ // alternate 1 response mixed with main response
675
+ const aM1 = [
676
+ { category: 's10', choices: ['4', '6'] },
677
+ { category: 's11', choices: ['4', '7'] },
678
+ ];
679
+ const aM1BestResponse = { s10: ['3', '7'], s11: ['4', '7'] };
680
+ const aM1BuiltCategories = [
681
+ {
682
+ id: 's10',
683
+ label: 'SUM=10',
684
+ correct: false,
685
+ choices: [
686
+ { ...baseChoices[1], correct: false },
687
+ { ...baseChoices[3], correct: false },
688
+ ],
689
+ },
690
+ {
691
+ id: 's11',
692
+ label: 'SUM=11',
693
+ correct: true,
694
+ choices: [
695
+ { ...baseChoices[1], correct: true },
696
+ { ...baseChoices[4], correct: true },
697
+ ],
698
+ },
699
+ ];
700
+ const aM1BuiltCategoriesNoAlt = [
701
+ {
702
+ id: 's10',
703
+ label: 'SUM=10',
704
+ correct: false,
705
+ choices: [
706
+ { ...baseChoices[1], correct: false },
707
+ { ...baseChoices[3], correct: false },
708
+ ],
709
+ },
710
+ {
711
+ id: 's11',
712
+ label: 'SUM=11',
713
+ correct: true,
714
+ choices: [
715
+ { ...baseChoices[1], correct: true },
716
+ { ...baseChoices[4], correct: true },
717
+ ],
718
+ },
719
+ ];
720
+
721
+ // alternate 1 response mixed with alternate 2 response
722
+ const aM2 = [
723
+ { category: 's10', choices: ['4', '6'] },
724
+ { category: 's11', choices: ['5', '6'] },
725
+ ];
726
+ const aM2BestResponse = { s10: ['3', '7'], s11: ['4', '7'] };
727
+ const aM2BuiltCategories = [
728
+ {
729
+ id: 's10',
730
+ label: 'SUM=10',
731
+ correct: false,
732
+ choices: [
733
+ { ...baseChoices[1], correct: false },
734
+ { ...baseChoices[3], correct: false },
735
+ ],
736
+ },
737
+ {
738
+ id: 's11',
739
+ label: 'SUM=11',
740
+ correct: false,
741
+ choices: [
742
+ { ...baseChoices[2], correct: false },
743
+ { ...baseChoices[3], correct: false },
744
+ ],
745
+ },
746
+ ];
747
+ const aM2BuiltCategoriesNoAlt = [
748
+ {
749
+ id: 's10',
750
+ label: 'SUM=10',
751
+ correct: false,
752
+ choices: [
753
+ { ...baseChoices[1], correct: false },
754
+ { ...baseChoices[3], correct: false },
755
+ ],
756
+ },
757
+ {
758
+ id: 's11',
759
+ label: 'SUM=11',
760
+ correct: false,
761
+ choices: [
762
+ { ...baseChoices[2], correct: false },
763
+ { ...baseChoices[3], correct: false },
764
+ ],
765
+ },
766
+ ];
767
+
768
+ // main response selections Incorrect
769
+ const aI1 = [
770
+ { category: 's10', choices: ['3', '6'] },
771
+ { category: 's11', choices: ['4', '4'] },
772
+ ];
773
+ const aI1BestResponse = { s10: ['3', '7'], s11: ['4', '7'] };
774
+ const aI1BuiltCategories = [
775
+ {
776
+ id: 's10',
777
+ label: 'SUM=10',
778
+ correct: false,
779
+ choices: [
780
+ { ...baseChoices[0], correct: true },
781
+ { ...baseChoices[3], correct: false },
782
+ ],
783
+ },
784
+ {
785
+ id: 's11',
786
+ label: 'SUM=11',
787
+ correct: false,
788
+ choices: [
789
+ { ...baseChoices[1], correct: true },
790
+ { ...baseChoices[1], correct: false },
791
+ ],
792
+ },
793
+ ];
794
+
795
+ // if there are alternates, looks for the best response
796
+ it.each`
797
+ label | answers | correctExpected | bestResponseExpected | builtCategories | builtChoices
798
+ ${'correct'} | ${aC1} | ${true} | ${aC1BestResponse} | ${aC1BuiltCategories} | ${[...baseChoices.slice(0, 4), { empty: true }]}
799
+ ${'correct'} | ${aC2} | ${true} | ${aC2BestResponse} | ${aC2BuiltCategories} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
800
+ ${'correct'} | ${aC3} | ${true} | ${aC3BestResponse} | ${aC3BuiltCategories} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
801
+ ${'incorrect'} | ${aI1} | ${false} | ${aI1BestResponse} | ${aI1BuiltCategories} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
802
+ ${'mixed'} | ${aM1} | ${false} | ${aM1BestResponse} | ${aM1BuiltCategories} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
803
+ ${'mixed'} | ${aM2} | ${false} | ${aM2BestResponse} | ${aM2BuiltCategories} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
804
+ `(
805
+ 'With Alternates, $label -> dychotomous: correct = correctExpected',
806
+ ({ answers, correctExpected, bestResponseExpected, builtCategories, builtChoices }) => {
807
+ const { choices, categories, correct, bestResponse } = buildState(
808
+ baseCategories,
809
+ baseChoices,
810
+ answers,
811
+ correctResponseWithAlternates,
812
+ );
813
+
814
+ expect(choices).toEqual(builtChoices);
815
+ expect(categories).toEqual(builtCategories);
816
+ expect(correctExpected).toEqual(correct);
817
+ expect(bestResponseExpected).toEqual(bestResponse);
818
+ },
819
+ );
820
+
821
+ // if there are no alternates, the best response is always the main one
822
+ it.each`
823
+ label | answers | correctExpected | bestResponseExpected | builtCategories | builtChoices
824
+ ${'correct'} | ${aC1} | ${true} | ${aC1BestResponse} | ${aC1BuiltCategories} | ${[...baseChoices.slice(0, 4), { empty: true }]}
825
+ ${'correct'} | ${aC2} | ${false} | ${aC1BestResponse} | ${aC2BuiltCategoriesNoAlt} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
826
+ ${'correct'} | ${aC3} | ${false} | ${aC1BestResponse} | ${aC3BuiltCategoriesNoAlt} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
827
+ ${'correct'} | ${aI1} | ${false} | ${aI1BestResponse} | ${aI1BuiltCategories} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
828
+ ${'correct'} | ${aM1} | ${false} | ${aC1BestResponse} | ${aM1BuiltCategoriesNoAlt} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
829
+ ${'correct'} | ${aM2} | ${false} | ${aC1BestResponse} | ${aM2BuiltCategoriesNoAlt} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
830
+ `(
831
+ 'Without Alternates, $label -> dychotomous: correct = correctExpected',
832
+ ({ answers, correctExpected, bestResponseExpected, builtCategories, builtChoices }) => {
833
+ const { choices, categories, correct, bestResponse } = buildState(
834
+ baseCategories,
835
+ baseChoices,
836
+ answers,
837
+ correctResponseNoAlternates,
838
+ );
839
+
840
+ expect(choices).toEqual(builtChoices);
841
+ expect(categories).toEqual(builtCategories);
842
+ expect(correctExpected).toEqual(correct);
843
+ expect(bestResponseExpected).toEqual(bestResponse);
844
+ },
845
+ );
846
+ });
847
+
848
+ describe('buildCategories', () => {
849
+ it.each`
850
+ categories | choices | answers | builtCategories
851
+ ${null} | ${null} | ${null} | ${[]}
852
+ ${undefined} | ${undefined} | ${undefined} | ${[]}
853
+ ${[]} | ${[]} | ${[]} | ${[]}
854
+ ${baseCategories} | ${baseChoices} | ${[]} | ${[
855
+ {
856
+ id: 's10',
857
+ label: 'SUM=10',
858
+ choices: [],
859
+ },
860
+ { id: 's11', label: 'SUM=11', choices: [] },
861
+ ]}
862
+ `('builtCategories = $builtCategories', ({ categories, choices, answers, builtCategories }) => {
863
+ expect(buildCategories(categories, choices, answers)).toEqual(builtCategories);
864
+ });
865
+ });
866
+
867
+ describe('buildChoices', () => {
868
+ const pRCA = ['3', '7'];
869
+ const pRCB = ['3', '3', '7'];
870
+
871
+ const bCC1 = [baseChoices[0], baseChoices[0]];
872
+ const bC1A = [
873
+ { ...baseChoices[0], correct: true },
874
+ { ...baseChoices[0], correct: false },
875
+ ];
876
+ const bC1B = [
877
+ { ...baseChoices[0], correct: true },
878
+ { ...baseChoices[0], correct: true },
879
+ ];
880
+
881
+ const bCC2 = [baseChoices[0], baseChoices[0], baseChoices[4]];
882
+ const bC2A = [
883
+ { ...baseChoices[0], correct: true },
884
+ { ...baseChoices[0], correct: false },
885
+ { ...baseChoices[4], correct: true },
886
+ ];
887
+ const bC2B = [
888
+ { ...baseChoices[0], correct: true },
889
+ { ...baseChoices[0], correct: true },
890
+ { ...baseChoices[4], correct: true },
891
+ ];
892
+
893
+ const bCC3 = baseChoices;
894
+ const bC3A = [
895
+ { ...baseChoices[0], correct: true },
896
+ { ...baseChoices[1], correct: false },
897
+ { ...baseChoices[2], correct: false },
898
+ { ...baseChoices[3], correct: false },
899
+ { ...baseChoices[4], correct: true },
900
+ ];
901
+
902
+ const bCC4 = [baseChoices[0], baseChoices[0], baseChoices[4], baseChoices[4]];
903
+ const bC4A = [
904
+ { ...baseChoices[0], correct: true },
905
+ { ...baseChoices[0], correct: false },
906
+ { ...baseChoices[4], correct: true },
907
+ { ...baseChoices[4], correct: false },
908
+ ];
909
+ const bC4B = [
910
+ { ...baseChoices[0], correct: true },
911
+ { ...baseChoices[0], correct: true },
912
+ { ...baseChoices[4], correct: true },
913
+ { ...baseChoices[4], correct: false },
914
+ ];
915
+
916
+ it.each`
917
+ possibleResponseChoices | builtCategoryChoices | builtChoices
918
+ ${pRCA} | ${[]} | ${[]}
919
+ ${pRCA} | ${bCC1} | ${bC1A}
920
+ ${pRCB} | ${bCC1} | ${bC1B}
921
+ ${pRCA} | ${bCC2} | ${bC2A}
922
+ ${pRCB} | ${bCC2} | ${bC2B}
923
+ ${pRCA} | ${bCC3} | ${bC3A}
924
+ ${pRCB} | ${bCC3} | ${bC3A}
925
+ ${pRCA} | ${bCC4} | ${bC4A}
926
+ ${pRCB} | ${bCC4} | ${bC4B}
927
+ `(
928
+ 'builtChoices = builtChoices, correct = $correct',
929
+ ({ possibleResponseChoices, builtCategoryChoices, builtChoices }) => {
930
+ const { builtChoices: builtChoicesResult } = buildChoices(possibleResponseChoices, builtCategoryChoices);
931
+
932
+ expect(builtChoicesResult).toEqual(builtChoices);
933
+ },
934
+ );
935
+ });
936
+
937
+ describe('getAllPossibleResponses', () => {
938
+ it.each`
939
+ correctResponse | allResponses
940
+ ${correctResponseNoAlternates} | ${[{ s10: ['3', '7'], s11: ['4', '7'] }]}
941
+ ${correctResponseWithAlternates} | ${[
942
+ { s10: ['3', '7'], s11: ['4', '7'] },
943
+ {
944
+ s10: ['4', '6'],
945
+ s11: ['3', '3', '5'],
946
+ },
947
+ { s10: ['5', '5'], s11: ['5', '6'] },
948
+ ]}
949
+ `('allResponses = allResponses', ({ correctResponse, allResponses }) => {
950
+ expect(getAllPossibleResponses(correctResponse)).toEqual(allResponses);
951
+ });
952
+ });
953
+ });