@pie-lib/categorize 2.0.2-next.1 → 2.0.3-next.0

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.
@@ -1,1262 +0,0 @@
1
- import {
2
- buildCategories,
3
- buildChoices,
4
- buildState,
5
- countChosen,
6
- countInAnswer,
7
- countInChoices,
8
- ensureNoExtraChoicesInAlternate,
9
- ensureNoExtraChoicesInAnswer,
10
- getAllPossibleResponses,
11
- getBuiltCategories,
12
- limitChoices,
13
- limitInArray,
14
- limitInArrays,
15
- moveChoiceToAlternate,
16
- moveChoiceToCategory,
17
- rearrangeChoices,
18
- removeAllChoices,
19
- removeCategory,
20
- removeChoiceFromAlternate,
21
- removeChoiceFromCategory,
22
- stillSelectable,
23
- verifyAllowMultiplePlacements,
24
- } from '../index';
25
- import { range } from 'lodash-es';
26
- import util from 'util';
27
- import debug from 'debug';
28
- import { answer, cat, cats, choice } from './utils';
29
-
30
- const log = debug('@pie-lib:drag:test');
31
-
32
- const inspect = (o) => util.inspect(o, { color: true });
33
-
34
- describe('categorize', () => {
35
- describe('countChosen', () => {
36
- const assert = (choice, categories, expected) => {
37
- const l = (categories || []).map((c) => c.choices || []);
38
-
39
- it(`${JSON.stringify(choice)} + ${JSON.stringify(l)} = ${expected}`, () => {
40
- expect(countChosen(choice, categories)).toEqual(expected);
41
- });
42
- };
43
-
44
- assert(null, null, 0);
45
- assert({}, null, 0);
46
- assert({ id: '1' }, [], 0);
47
- assert({ id: '1' }, [{ choices: undefined }], 0);
48
- assert({ id: '1' }, [cats(['1'])], 1);
49
- assert({ id: '1' }, [cats(['1', '1'])], 2);
50
- assert({ id: '1' }, [cats(['1', '2'])], 1);
51
- assert({ id: '1' }, [cats(['1']), cats(['1'])], 2);
52
- });
53
-
54
- describe('limitInArrays', () => {
55
- const assert = (id, arrays, limit, expected) => {
56
- it(`${id}, ${inspect(arrays)} -> ${inspect(expected)}`, () => {
57
- const result = limitInArrays(id, arrays, limit);
58
- expect(result).toEqual(expected);
59
- });
60
- };
61
- assert('1', [['1'], ['1']], 1, [['1'], []]);
62
- assert('1', [['1'], ['1']], 2, [['1'], ['1']]);
63
- assert('1', [['1'], ['1'], ['1', '2']], 2, [['1'], ['1'], ['2']]);
64
- assert('1', [undefined, [], null], 2, [[], [], []]);
65
- assert('1', undefined, 2, []);
66
- assert('1', null, 2, []);
67
- assert('1', [], 2, []);
68
- });
69
-
70
- describe('limitInArray', () => {
71
- it('', () => {
72
- expect(limitInArray('1', ['1', '1'], 1)).toEqual({
73
- array: ['1'],
74
- count: 1,
75
- });
76
- expect(limitInArray('1', ['1', '1'], 0)).toEqual({
77
- array: [],
78
- count: 2,
79
- });
80
- expect(limitInArray('1', ['1', '2', '1'], 1)).toEqual({
81
- array: ['1', '2'],
82
- count: 1,
83
- });
84
- expect(limitInArray('1', undefined, 1)).toEqual({
85
- array: [],
86
- count: 0,
87
- });
88
- expect(limitInArray('1', null, 1)).toEqual({
89
- array: [],
90
- count: 0,
91
- });
92
- expect(limitInArray('1', [], 1)).toEqual({
93
- array: [],
94
- count: 0,
95
- });
96
- });
97
- });
98
-
99
- describe('removeAllChoices', () => {
100
- it('strips if there is no category id', () => {
101
- const result = removeAllChoices('1', [{ choices: ['1'] }]);
102
- expect(result).toEqual([{ choices: [] }]);
103
- });
104
-
105
- it('strips if the category id matches', () => {
106
- const result = removeAllChoices('1', [{ category: '2', choices: ['1'] }], '2');
107
- expect(result).toEqual([{ category: '2', choices: [] }]);
108
- });
109
-
110
- it('doesnt strip if the category id does not match', () => {
111
- const result = removeAllChoices('1', [{ category: '3', choices: ['1'] }], '2');
112
- expect(result).toEqual([{ category: '3', choices: ['1'] }]);
113
- });
114
- });
115
-
116
- describe('ensureNoExtraChoicesInAnswer', () => {
117
- const _assert = (only) => (answer, choices, expected) => {
118
- const fn = only ? it.only : it;
119
- fn(`${inspect(answer)} + ${inspect(choices)} == ${inspect(expected)}`, () => {
120
- const result = ensureNoExtraChoicesInAnswer(answer, choices);
121
- log('result: ', result);
122
- expect(result).toEqual(expected);
123
- });
124
- };
125
- const assert = _assert(false);
126
- assert.only = _assert(true);
127
-
128
- assert([answer('1', ['1', '2', '1'])], [{ id: '1' }, { id: '2' }], [answer('1', ['1', '2', '1'])]);
129
-
130
- assert([answer('1', ['1', '2', '1'])], [{ id: '1', categoryCount: 1 }, { id: '2' }], [answer('1', ['1', '2'])]);
131
-
132
- assert(
133
- [answer('1', ['1', '2', '1', '2'])],
134
- [
135
- { id: '1', categoryCount: 0 },
136
- { id: '2', categoryCount: 1 },
137
- ],
138
- [answer('1', ['1', '2', '1'])],
139
- );
140
- assert([answer('1', ['1', '2', '1', '2'])], [choice('1'), choice('2', 1)], [answer('1', ['1', '2', '1'])]);
141
- assert([answer('1', ['1', '2', '1'])], [choice('1')], [answer('1', ['1', '2', '1'])]);
142
- assert([answer('1', ['1', '2', '1'])], [choice('1', 1)], [answer('1', ['1', '2'])]);
143
- assert(
144
- [answer('1', ['3', '3', '1', '2', '1', '2', '3'])],
145
- [choice('1'), choice('2', 1), choice('3', 2)],
146
- [answer('1', ['3', '3', '1', '2', '1'])],
147
- );
148
-
149
- assert([answer('1', ['1', '1'])], [choice('1', 0)], [answer('1', ['1', '1'])]);
150
-
151
- assert(
152
- [
153
- answer(
154
- '1',
155
- range(0, 10).map((r) => '1'),
156
- ),
157
- ],
158
- [choice('1', 0)],
159
- [
160
- answer(
161
- '1',
162
- range(0, 10).map((r) => '1'),
163
- ),
164
- ],
165
- );
166
- assert([answer('1', ['1', '1'])], [choice('1', 1)], [answer('1', ['1'])]);
167
-
168
- assert([answer('1', ['1']), answer('2', ['1'])], [choice('1', 1)], [answer('1', ['1']), answer('2', [])]);
169
-
170
- assert([answer('1', ['1', '1', '1'])], [choice('1', 2)], [answer('1', ['1', '1'])]);
171
-
172
- assert([answer('1', ['1']), answer('2', ['1', '1'])], [choice('1', 2)], [answer('1', ['1']), answer('2', ['1'])]);
173
-
174
- assert(
175
- [answer('1', ['1']), answer('2', ['1', '1']), answer('3', ['1'])],
176
- [choice('1', 2)],
177
- [answer('1', ['1']), answer('2', ['1']), answer('3', [])],
178
- );
179
-
180
- assert([answer('1', ['1', '1', '1'])], [], [answer('1', ['1', '1', '1'])]);
181
-
182
- assert([answer('1', ['1', '1', '1'])], undefined, [answer('1', ['1', '1', '1'])]);
183
-
184
- assert([answer('1', ['1', '1', '1'])], null, [answer('1', ['1', '1', '1'])]);
185
- });
186
-
187
- describe('countChosen', () => {
188
- const assert = (choice, categories, expected) => {
189
- const l = (categories || []).map((c) => c.choices || []);
190
-
191
- it(`${JSON.stringify(choice)} + ${JSON.stringify(l)} = ${expected}`, () => {
192
- expect(countChosen(choice, categories)).toEqual(expected);
193
- });
194
- };
195
-
196
- assert(null, undefined, 0);
197
- assert(null, [], 0);
198
- assert(null, null, 0);
199
- assert({}, null, 0);
200
- assert({ id: '1' }, [], 0);
201
- assert({ id: '1' }, [{ choices: undefined }], 0);
202
- assert({ id: '1' }, [cats(['1'])], 1);
203
- assert({ id: '1' }, [cats(['1', '1'])], 2);
204
- assert({ id: '1' }, [cats(['1', '2'])], 1);
205
- assert({ id: '1' }, [cats(['1']), cats(['1'])], 2);
206
- });
207
-
208
- describe('removeChoiceFromCategory', () => {
209
- const assert = (choiceId, categoryId, choiceIndex, answers, expected) => {
210
- it(`remove choice:${choiceId} from category:${categoryId} == ${util.inspect(expected, {
211
- colors: true,
212
- })}`, () => {
213
- const result = removeChoiceFromCategory(choiceId, categoryId, choiceIndex, answers);
214
- expect(result).toEqual(expected);
215
- });
216
- };
217
- assert('1', '1', 0, [answer('1', ['1'])], [answer('1', [])]);
218
-
219
- assert('1', '1', 0, [answer('1', ['1']), answer('2', ['1', '1'])], [answer('1', []), answer('2', ['1', '1'])]);
220
-
221
- assert('1', '1', 3, [answer('1', ['1', '2', '3', '1'])], [answer('1', ['1', '2', '3'])]);
222
- });
223
-
224
- describe('moveChoiceToCategory', () => {
225
- const assert = (choiceId, from, to, choiceIndex, answers, expected) => {
226
- it(`move choice:${choiceId} from category:${from} -> category:${to} == ${util.inspect(expected, {
227
- colors: true,
228
- })}`, () => {
229
- const result = moveChoiceToCategory(choiceId, from, to, choiceIndex, answers);
230
- expect(result).toEqual(expected);
231
- });
232
- };
233
- assert('1', '1', '2', 0, [answer('1', ['1'])], [answer('1', []), answer('2', ['1'])]);
234
- assert('1', undefined, '2', 0, [answer('1', ['1'])], [answer('1', ['1']), answer('2', ['1'])]);
235
- });
236
-
237
- describe('moveChoiceToAlternate', () => {
238
- const assert = (choiceId, from, to, choiceIndex, answers, alternateIndex, categoryCount, expected) => {
239
- it(`move choice:${choiceId} from category:${from} -> category:${to} == ${util.inspect(expected, {
240
- colors: true,
241
- })}`, () => {
242
- const result = moveChoiceToAlternate(choiceId, from, to, choiceIndex, answers, alternateIndex, categoryCount);
243
- expect(result).toEqual(expected);
244
- });
245
- };
246
- assert(
247
- '2',
248
- '1',
249
- '2',
250
- 0,
251
- [
252
- answer(
253
- '1',
254
- ['1'],
255
- [
256
- ['1', '2'],
257
- ['1', '1', '2'],
258
- ],
259
- ),
260
- answer(
261
- '2',
262
- ['1'],
263
- [
264
- ['1', '2', '2'],
265
- ['1', '1'],
266
- ],
267
- ),
268
- ],
269
- 1,
270
- 0,
271
- [
272
- answer(
273
- '1',
274
- ['1'],
275
- [
276
- ['1', '2'],
277
- ['1', '1', '2'],
278
- ],
279
- ),
280
- answer(
281
- '2',
282
- ['1'],
283
- [
284
- ['1', '2', '2'],
285
- ['1', '1', '2'],
286
- ],
287
- ),
288
- ],
289
- );
290
- assert(
291
- '2',
292
- '1',
293
- '2',
294
- 0,
295
- [
296
- answer(
297
- '1',
298
- ['1'],
299
- [
300
- ['1', '2'],
301
- ['1', '1', '2'],
302
- ],
303
- ),
304
- answer(
305
- '2',
306
- ['1'],
307
- [
308
- ['1', '2', '2'],
309
- ['1', '1'],
310
- ],
311
- ),
312
- ],
313
- 1,
314
- 1,
315
- [
316
- answer(
317
- '1',
318
- ['1'],
319
- [
320
- ['1', '2'],
321
- ['1', '1'],
322
- ],
323
- ),
324
- answer(
325
- '2',
326
- ['1'],
327
- [
328
- ['1', '2', '2'],
329
- ['1', '1', '2'],
330
- ],
331
- ),
332
- ],
333
- );
334
- });
335
-
336
- describe('buildState', () => {
337
- const _a = (only, label, categories, choices, answers, correctResponse, expected) => {
338
- const fn = only ? it.only : it;
339
- fn(label, () => {
340
- const result = buildState(categories, choices, answers, correctResponse);
341
- expect(result).toMatchObject(expected);
342
- });
343
- };
344
- const assert = _a.bind(_a, false);
345
- assert.only = _a.bind(_a, true);
346
-
347
- assert('1 category, 1 choice, 1 answer, no correct response', [cat('1')], [choice('1')], [answer('1', '1')], [], {
348
- categories: [{ ...cat('1'), choices: [choice('1')] }],
349
- choices: [choice('1')],
350
- });
351
-
352
- assert('1 category, 1 unlimited choice, no answer, no correct response', [cat('1')], [choice('2')], [], [], {
353
- categories: [{ ...cat('1'), choices: [] }],
354
- choices: [choice('2')],
355
- });
356
-
357
- assert(
358
- '2 categories, 1 unlimited choice, 2 answers, no correct response',
359
- [cat('1'), cat('2')],
360
- [choice('1', 0)],
361
- [answer('1', ['1']), answer('2', ['1'])],
362
- [],
363
- {
364
- categories: [
365
- { ...cat('1'), choices: [choice('1', 0)] },
366
- { ...cat('2'), choices: [choice('1', 0)] },
367
- ],
368
- choices: [choice('1', 0)],
369
- },
370
- );
371
-
372
- assert(
373
- '2 categories, 1 choice, 1 answer, no correct response',
374
- [cat('1'), cat('2')],
375
- [choice('1', 0)],
376
- [answer('1', ['1'])],
377
- [],
378
- {
379
- categories: [
380
- { ...cat('1'), choices: [choice('1', 0)] },
381
- {
382
- ...cat('2'),
383
- choices: [],
384
- },
385
- ],
386
- choices: [{ ...choice('1', 0) }],
387
- },
388
- );
389
-
390
- assert(
391
- '2 categories, 1 choices, 1 incorrect answer, 1 correct response',
392
- [cat('1'), cat('2')],
393
- [choice('1', 0)],
394
- [answer('1', ['1'])],
395
- [answer('2', ['1'])],
396
- {
397
- correct: false,
398
- categories: [
399
- { ...cat('1'), choices: [choice('1', 0, false)], correct: false },
400
- {
401
- ...cat('2'),
402
- choices: [],
403
- },
404
- ],
405
- choices: [{ ...choice('1', 0) }],
406
- },
407
- );
408
-
409
- assert(
410
- '1 cat, 1 choice, 1 answer, 1 correct response',
411
- [cat('1')],
412
- [choice('1', 0)],
413
- [answer('1', ['1'])],
414
- [answer('1', ['1'])],
415
- {
416
- correct: true,
417
- categories: [
418
- {
419
- ...cat('1'),
420
- choices: [choice('1', 0, true)],
421
- correct: true,
422
- },
423
- ],
424
- choices: [{ ...choice('1', 0) }],
425
- },
426
- );
427
-
428
- assert(
429
- '1 cat, 1 choice, 2 same answers (correct), 2 correct responses',
430
- [cat('1')],
431
- [choice('1', 0)],
432
- [answer('1', ['1', '1'])],
433
- [answer('1', ['1', '1'])],
434
- {
435
- correct: true,
436
- categories: [
437
- {
438
- ...cat('1'),
439
- choices: [choice('1', 0, true), choice('1', 0, true)],
440
- correct: true,
441
- },
442
- ],
443
- choices: [{ ...choice('1', 0) }],
444
- },
445
- );
446
-
447
- assert(
448
- '1 cat, 1 choice, 2 same answers (1 correct, 1 incorrect), correct response',
449
- [cat('1')],
450
- [choice('1', 0)],
451
- [answer('1', ['1', '1'])],
452
- [answer('1', ['1'])],
453
- {
454
- correct: false,
455
- categories: [
456
- {
457
- ...cat('1'),
458
- choices: [choice('1', 0, true), choice('1', 0, false)],
459
- correct: false,
460
- },
461
- ],
462
- choices: [{ ...choice('1', 0) }],
463
- },
464
- );
465
-
466
- assert(
467
- '2 categories, 1 limited choice , 1 correct answer, correct response',
468
- [cat('1'), cat('2')],
469
- [choice('1', 1)],
470
- [answer('1', ['1'])],
471
- [answer('1', ['1'])],
472
- {
473
- correct: true,
474
- categories: [
475
- { ...cat('1'), choices: [choice('1', 1, true)], correct: true },
476
- { ...cat('2'), correct: true },
477
- ],
478
- choices: [{ empty: true }],
479
- },
480
- );
481
-
482
- assert(
483
- '2 categories, 1 limited choice , empty answer, correct response',
484
- [cat('1'), cat('2')],
485
- [choice('1', 1)],
486
- [],
487
- [answer('1', ['1'])],
488
- {
489
- correct: false,
490
- categories: [
491
- { ...cat('1'), choices: [], correct: false },
492
- { ...cat('2'), choices: [], correct: true },
493
- ],
494
- choices: [choice('1', 1)],
495
- },
496
- );
497
-
498
- assert(
499
- '1 cat, 2 choices, 1 answer, 1 correct response, 1 alternate response',
500
- [cat('1')],
501
- [choice('1', 0), choice('2', 0)],
502
- [answer('1', ['2'])],
503
- [answer('1', ['1'], [['2']])],
504
- {
505
- correct: true,
506
- categories: [
507
- {
508
- ...cat('1'),
509
- choices: [choice('2', 0, true)],
510
- correct: true,
511
- },
512
- ],
513
- choices: [{ ...choice('1', 0) }, { ...choice('2', 0) }],
514
- },
515
- );
516
- });
517
-
518
- // new tests
519
-
520
- const baseCategories = [
521
- { id: 's10', label: 'SUM=10' },
522
- { id: 's11', label: 'SUM=11' },
523
- ];
524
- const baseChoices = [
525
- { id: '3', content: '3' },
526
- { id: '4', content: '4' },
527
- { id: '5', content: '5' },
528
- { id: '6', content: '6', categoryCount: 1 },
529
- { id: '7', content: '7', categoryCount: 2 },
530
- ];
531
- const correctResponseNoAlternates = [
532
- { category: 's10', choices: ['3', '7'] },
533
- { category: 's11', choices: ['4', '7'] },
534
- ];
535
- const correctResponseWithAlternates = [
536
- {
537
- category: 's10',
538
- choices: ['3', '7'],
539
- alternateResponses: [
540
- ['4', '6'],
541
- ['5', '5'],
542
- ],
543
- },
544
- {
545
- category: 's11',
546
- choices: ['4', '7'],
547
- alternateResponses: [
548
- ['3', '3', '5'],
549
- ['5', '6'],
550
- ],
551
- },
552
- ];
553
-
554
- describe('buildState', () => {
555
- // main response selections
556
- const aC1 = [
557
- { category: 's10', choices: ['3', '7'] },
558
- { category: 's11', choices: ['4', '7'] },
559
- ];
560
- const aC1BestResponse = { s10: ['3', '7'], s11: ['4', '7'] };
561
- const aC1BuiltCategories = [
562
- {
563
- id: 's10',
564
- label: 'SUM=10',
565
- correct: true,
566
- choices: [
567
- { ...baseChoices[0], correct: true },
568
- { ...baseChoices[4], correct: true },
569
- ],
570
- },
571
- {
572
- id: 's11',
573
- label: 'SUM=11',
574
- correct: true,
575
- choices: [
576
- { ...baseChoices[1], correct: true },
577
- { ...baseChoices[4], correct: true },
578
- ],
579
- },
580
- ];
581
-
582
- // alternate 1 response
583
- const aC2 = [
584
- { category: 's10', choices: ['4', '6'] },
585
- { category: 's11', choices: ['3', '3', '5'] },
586
- ];
587
- const aC2BestResponse = { s10: ['4', '6'], s11: ['3', '3', '5'] };
588
- const aC2BuiltCategories = [
589
- {
590
- id: 's10',
591
- label: 'SUM=10',
592
- correct: true,
593
- choices: [
594
- { ...baseChoices[1], correct: true },
595
- { ...baseChoices[3], correct: true },
596
- ],
597
- },
598
- {
599
- id: 's11',
600
- label: 'SUM=11',
601
- correct: true,
602
- choices: [
603
- { ...baseChoices[0], correct: true },
604
- { ...baseChoices[0], correct: true },
605
- {
606
- ...baseChoices[2],
607
- correct: true,
608
- },
609
- ],
610
- },
611
- ];
612
- const aC2BuiltCategoriesNoAlt = [
613
- {
614
- id: 's10',
615
- label: 'SUM=10',
616
- correct: false,
617
- choices: [
618
- { ...baseChoices[1], correct: false },
619
- { ...baseChoices[3], correct: false },
620
- ],
621
- },
622
- {
623
- id: 's11',
624
- label: 'SUM=11',
625
- correct: false,
626
- choices: [
627
- { ...baseChoices[0], correct: false },
628
- { ...baseChoices[0], correct: false },
629
- {
630
- ...baseChoices[2],
631
- correct: false,
632
- },
633
- ],
634
- },
635
- ];
636
-
637
- // alternate 2 response
638
- const aC3 = [
639
- { category: 's10', choices: ['5', '5'] },
640
- { category: 's11', choices: ['5', '6'] },
641
- ];
642
- const aC3BestResponse = { s10: ['5', '5'], s11: ['5', '6'] };
643
- const aC3BuiltCategories = [
644
- {
645
- id: 's10',
646
- label: 'SUM=10',
647
- correct: true,
648
- choices: [
649
- { ...baseChoices[2], correct: true },
650
- { ...baseChoices[2], correct: true },
651
- ],
652
- },
653
- {
654
- id: 's11',
655
- label: 'SUM=11',
656
- correct: true,
657
- choices: [
658
- { ...baseChoices[2], correct: true },
659
- { ...baseChoices[3], correct: true },
660
- ],
661
- },
662
- ];
663
- const aC3BuiltCategoriesNoAlt = [
664
- {
665
- id: 's10',
666
- label: 'SUM=10',
667
- correct: false,
668
- choices: [
669
- { ...baseChoices[2], correct: false },
670
- { ...baseChoices[2], correct: false },
671
- ],
672
- },
673
- {
674
- id: 's11',
675
- label: 'SUM=11',
676
- correct: false,
677
- choices: [
678
- { ...baseChoices[2], correct: false },
679
- { ...baseChoices[3], correct: false },
680
- ],
681
- },
682
- ];
683
-
684
- // alternate 1 response mixed with main response
685
- const aM1 = [
686
- { category: 's10', choices: ['4', '6'] },
687
- { category: 's11', choices: ['4', '7'] },
688
- ];
689
- const aM1BestResponse = { s10: ['3', '7'], s11: ['4', '7'] };
690
- const aM1BuiltCategories = [
691
- {
692
- id: 's10',
693
- label: 'SUM=10',
694
- correct: false,
695
- choices: [
696
- { ...baseChoices[1], correct: false },
697
- { ...baseChoices[3], correct: false },
698
- ],
699
- },
700
- {
701
- id: 's11',
702
- label: 'SUM=11',
703
- correct: true,
704
- choices: [
705
- { ...baseChoices[1], correct: true },
706
- { ...baseChoices[4], correct: true },
707
- ],
708
- },
709
- ];
710
- const aM1BuiltCategoriesNoAlt = [
711
- {
712
- id: 's10',
713
- label: 'SUM=10',
714
- correct: false,
715
- choices: [
716
- { ...baseChoices[1], correct: false },
717
- { ...baseChoices[3], correct: false },
718
- ],
719
- },
720
- {
721
- id: 's11',
722
- label: 'SUM=11',
723
- correct: true,
724
- choices: [
725
- { ...baseChoices[1], correct: true },
726
- { ...baseChoices[4], correct: true },
727
- ],
728
- },
729
- ];
730
-
731
- // alternate 1 response mixed with alternate 2 response
732
- const aM2 = [
733
- { category: 's10', choices: ['4', '6'] },
734
- { category: 's11', choices: ['5', '6'] },
735
- ];
736
- const aM2BestResponse = { s10: ['3', '7'], s11: ['4', '7'] };
737
- const aM2BuiltCategories = [
738
- {
739
- id: 's10',
740
- label: 'SUM=10',
741
- correct: false,
742
- choices: [
743
- { ...baseChoices[1], correct: false },
744
- { ...baseChoices[3], correct: false },
745
- ],
746
- },
747
- {
748
- id: 's11',
749
- label: 'SUM=11',
750
- correct: false,
751
- choices: [
752
- { ...baseChoices[2], correct: false },
753
- { ...baseChoices[3], correct: false },
754
- ],
755
- },
756
- ];
757
- const aM2BuiltCategoriesNoAlt = [
758
- {
759
- id: 's10',
760
- label: 'SUM=10',
761
- correct: false,
762
- choices: [
763
- { ...baseChoices[1], correct: false },
764
- { ...baseChoices[3], correct: false },
765
- ],
766
- },
767
- {
768
- id: 's11',
769
- label: 'SUM=11',
770
- correct: false,
771
- choices: [
772
- { ...baseChoices[2], correct: false },
773
- { ...baseChoices[3], correct: false },
774
- ],
775
- },
776
- ];
777
-
778
- // main response selections Incorrect
779
- const aI1 = [
780
- { category: 's10', choices: ['3', '6'] },
781
- { category: 's11', choices: ['4', '4'] },
782
- ];
783
- const aI1BestResponse = { s10: ['3', '7'], s11: ['4', '7'] };
784
- const aI1BuiltCategories = [
785
- {
786
- id: 's10',
787
- label: 'SUM=10',
788
- correct: false,
789
- choices: [
790
- { ...baseChoices[0], correct: true },
791
- { ...baseChoices[3], correct: false },
792
- ],
793
- },
794
- {
795
- id: 's11',
796
- label: 'SUM=11',
797
- correct: false,
798
- choices: [
799
- { ...baseChoices[1], correct: true },
800
- { ...baseChoices[1], correct: false },
801
- ],
802
- },
803
- ];
804
-
805
- // if there are alternates, looks for the best response
806
- it.each`
807
- label | answers | correctExpected | bestResponseExpected | builtCategories | builtChoices
808
- ${'correct'} | ${aC1} | ${true} | ${aC1BestResponse} | ${aC1BuiltCategories} | ${[...baseChoices.slice(0, 4), { empty: true }]}
809
- ${'correct'} | ${aC2} | ${true} | ${aC2BestResponse} | ${aC2BuiltCategories} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
810
- ${'correct'} | ${aC3} | ${true} | ${aC3BestResponse} | ${aC3BuiltCategories} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
811
- ${'incorrect'} | ${aI1} | ${false} | ${aI1BestResponse} | ${aI1BuiltCategories} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
812
- ${'mixed'} | ${aM1} | ${false} | ${aM1BestResponse} | ${aM1BuiltCategories} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
813
- ${'mixed'} | ${aM2} | ${false} | ${aM2BestResponse} | ${aM2BuiltCategories} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
814
- `(
815
- 'With Alternates, $label -> dychotomous: correct = correctExpected',
816
- ({ answers, correctExpected, bestResponseExpected, builtCategories, builtChoices }) => {
817
- const { choices, categories, correct, bestResponse } = buildState(
818
- baseCategories,
819
- baseChoices,
820
- answers,
821
- correctResponseWithAlternates,
822
- );
823
-
824
- expect(choices).toEqual(builtChoices);
825
- expect(categories).toEqual(builtCategories);
826
- expect(correctExpected).toEqual(correct);
827
- expect(bestResponseExpected).toEqual(bestResponse);
828
- },
829
- );
830
-
831
- // if there are no alternates, the best response is always the main one
832
- it.each`
833
- label | answers | correctExpected | bestResponseExpected | builtCategories | builtChoices
834
- ${'correct'} | ${aC1} | ${true} | ${aC1BestResponse} | ${aC1BuiltCategories} | ${[...baseChoices.slice(0, 4), { empty: true }]}
835
- ${'correct'} | ${aC2} | ${false} | ${aC1BestResponse} | ${aC2BuiltCategoriesNoAlt} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
836
- ${'correct'} | ${aC3} | ${false} | ${aC1BestResponse} | ${aC3BuiltCategoriesNoAlt} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
837
- ${'correct'} | ${aI1} | ${false} | ${aI1BestResponse} | ${aI1BuiltCategories} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
838
- ${'correct'} | ${aM1} | ${false} | ${aC1BestResponse} | ${aM1BuiltCategoriesNoAlt} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
839
- ${'correct'} | ${aM2} | ${false} | ${aC1BestResponse} | ${aM2BuiltCategoriesNoAlt} | ${[...baseChoices.slice(0, 3), { empty: true }, baseChoices[4]]}
840
- `(
841
- 'Without Alternates, $label -> dychotomous: correct = correctExpected',
842
- ({ answers, correctExpected, bestResponseExpected, builtCategories, builtChoices }) => {
843
- const { choices, categories, correct, bestResponse } = buildState(
844
- baseCategories,
845
- baseChoices,
846
- answers,
847
- correctResponseNoAlternates,
848
- );
849
-
850
- expect(choices).toEqual(builtChoices);
851
- expect(categories).toEqual(builtCategories);
852
- expect(correctExpected).toEqual(correct);
853
- expect(bestResponseExpected).toEqual(bestResponse);
854
- },
855
- );
856
- });
857
-
858
- describe('buildCategories', () => {
859
- it.each`
860
- categories | choices | answers | builtCategories
861
- ${null} | ${null} | ${null} | ${[]}
862
- ${undefined} | ${undefined} | ${undefined} | ${[]}
863
- ${[]} | ${[]} | ${[]} | ${[]}
864
- ${baseCategories} | ${baseChoices} | ${[]} | ${[
865
- {
866
- id: 's10',
867
- label: 'SUM=10',
868
- choices: [],
869
- },
870
- { id: 's11', label: 'SUM=11', choices: [] },
871
- ]}
872
- `('builtCategories = $builtCategories', ({ categories, choices, answers, builtCategories }) => {
873
- expect(buildCategories(categories, choices, answers)).toEqual(builtCategories);
874
- });
875
- });
876
-
877
- describe('buildChoices', () => {
878
- const pRCA = ['3', '7'];
879
- const pRCB = ['3', '3', '7'];
880
-
881
- const bCC1 = [baseChoices[0], baseChoices[0]];
882
- const bC1A = [
883
- { ...baseChoices[0], correct: true },
884
- { ...baseChoices[0], correct: false },
885
- ];
886
- const bC1B = [
887
- { ...baseChoices[0], correct: true },
888
- { ...baseChoices[0], correct: true },
889
- ];
890
-
891
- const bCC2 = [baseChoices[0], baseChoices[0], baseChoices[4]];
892
- const bC2A = [
893
- { ...baseChoices[0], correct: true },
894
- { ...baseChoices[0], correct: false },
895
- { ...baseChoices[4], correct: true },
896
- ];
897
- const bC2B = [
898
- { ...baseChoices[0], correct: true },
899
- { ...baseChoices[0], correct: true },
900
- { ...baseChoices[4], correct: true },
901
- ];
902
-
903
- const bCC3 = baseChoices;
904
- const bC3A = [
905
- { ...baseChoices[0], correct: true },
906
- { ...baseChoices[1], correct: false },
907
- { ...baseChoices[2], correct: false },
908
- { ...baseChoices[3], correct: false },
909
- { ...baseChoices[4], correct: true },
910
- ];
911
-
912
- const bCC4 = [baseChoices[0], baseChoices[0], baseChoices[4], baseChoices[4]];
913
- const bC4A = [
914
- { ...baseChoices[0], correct: true },
915
- { ...baseChoices[0], correct: false },
916
- { ...baseChoices[4], correct: true },
917
- { ...baseChoices[4], correct: false },
918
- ];
919
- const bC4B = [
920
- { ...baseChoices[0], correct: true },
921
- { ...baseChoices[0], correct: true },
922
- { ...baseChoices[4], correct: true },
923
- { ...baseChoices[4], correct: false },
924
- ];
925
-
926
- it.each`
927
- possibleResponseChoices | builtCategoryChoices | builtChoices
928
- ${pRCA} | ${[]} | ${[]}
929
- ${pRCA} | ${bCC1} | ${bC1A}
930
- ${pRCB} | ${bCC1} | ${bC1B}
931
- ${pRCA} | ${bCC2} | ${bC2A}
932
- ${pRCB} | ${bCC2} | ${bC2B}
933
- ${pRCA} | ${bCC3} | ${bC3A}
934
- ${pRCB} | ${bCC3} | ${bC3A}
935
- ${pRCA} | ${bCC4} | ${bC4A}
936
- ${pRCB} | ${bCC4} | ${bC4B}
937
- `(
938
- 'builtChoices = builtChoices, correct = $correct',
939
- ({ possibleResponseChoices, builtCategoryChoices, builtChoices }) => {
940
- const { builtChoices: builtChoicesResult } = buildChoices(possibleResponseChoices, builtCategoryChoices);
941
-
942
- expect(builtChoicesResult).toEqual(builtChoices);
943
- },
944
- );
945
- });
946
-
947
- describe('getAllPossibleResponses', () => {
948
- it.each`
949
- correctResponse | allResponses
950
- ${correctResponseNoAlternates} | ${[{ s10: ['3', '7'], s11: ['4', '7'] }]}
951
- ${correctResponseWithAlternates} | ${[
952
- { s10: ['3', '7'], s11: ['4', '7'] },
953
- {
954
- s10: ['4', '6'],
955
- s11: ['3', '3', '5'],
956
- },
957
- { s10: ['5', '5'], s11: ['5', '6'] },
958
- ]}
959
- `('allResponses = allResponses', ({ correctResponse, allResponses }) => {
960
- expect(getAllPossibleResponses(correctResponse)).toEqual(allResponses);
961
- });
962
- });
963
-
964
- describe('limitChoices', () => {
965
- it('returns all choices when count is not exceeded', () => {
966
- const result = limitChoices('1', 2, ['1', '2', '1']);
967
- expect(result).toEqual(['1', '2', '1']);
968
- });
969
-
970
- it('limits choice when count is exceeded', () => {
971
- const result = limitChoices('1', 2, ['1', '2', '1', '1']);
972
- expect(result).toEqual(['1', '2', '1']);
973
- });
974
-
975
- it('handles empty choices array', () => {
976
- const result = limitChoices('1', 2, []);
977
- expect(result).toEqual([]);
978
- });
979
-
980
- it('handles different choice IDs', () => {
981
- const result = limitChoices('2', 1, ['1', '2', '3', '2']);
982
- expect(result).toEqual(['1', '2', '3']);
983
- });
984
- });
985
-
986
- describe('ensureNoExtraChoicesInAlternate', () => {
987
- it('handles null choices', () => {
988
- const result = ensureNoExtraChoicesInAlternate([answer('1', ['1'])], null);
989
- expect(result).toEqual([answer('1', ['1'])]);
990
- });
991
-
992
- it('handles undefined choices', () => {
993
- const result = ensureNoExtraChoicesInAlternate([answer('1', ['1'])], undefined);
994
- expect(result).toEqual([answer('1', ['1'])]);
995
- });
996
-
997
- it('handles empty choices array', () => {
998
- const result = ensureNoExtraChoicesInAlternate([answer('1', ['1'])], []);
999
- expect(result).toEqual([answer('1', ['1'])]);
1000
- });
1001
-
1002
- it('limits alternates when categoryCount is set', () => {
1003
- const answers = [answer('1', ['1'], [['1', '1'], ['1']]), answer('2', ['2'], [['2'], ['2', '2']])];
1004
- const choices = [choice('1', 2), choice('2', 1)];
1005
- const result = ensureNoExtraChoicesInAlternate(answers, choices);
1006
-
1007
- expect(result[0].alternateResponses).toEqual([['1', '1'], ['1']]);
1008
- expect(result[1].alternateResponses).toEqual([['2'], ['2']]);
1009
- });
1010
-
1011
- it('does not limit when categoryCount is 0', () => {
1012
- const answers = [answer('1', ['1'], [['1', '1', '1']])];
1013
- const choices = [choice('1', 0)];
1014
- const result = ensureNoExtraChoicesInAlternate(answers, choices);
1015
-
1016
- expect(result).toEqual(answers);
1017
- });
1018
- });
1019
-
1020
- describe('countInAnswer', () => {
1021
- it('counts choice occurrences in answer', () => {
1022
- const result = countInAnswer('1', [answer('1', ['1', '2', '1'])]);
1023
- expect(result).toBe(2);
1024
- });
1025
-
1026
- it('counts across multiple categories', () => {
1027
- const result = countInAnswer('1', [answer('1', ['1', '1']), answer('2', ['1', '2'])]);
1028
- expect(result).toBe(3);
1029
- });
1030
-
1031
- it('returns 0 when choice not found', () => {
1032
- const result = countInAnswer('3', [answer('1', ['1', '2'])]);
1033
- expect(result).toBe(0);
1034
- });
1035
-
1036
- it('handles empty answer array', () => {
1037
- const result = countInAnswer('1', []);
1038
- expect(result).toBe(0);
1039
- });
1040
- });
1041
-
1042
- describe('countInChoices', () => {
1043
- it('counts choice in array', () => {
1044
- expect(countInChoices('1', ['1', '2', '1'])).toBe(2);
1045
- });
1046
-
1047
- it('returns 0 for empty array', () => {
1048
- expect(countInChoices('1', [])).toBe(0);
1049
- });
1050
-
1051
- it('returns 0 for null', () => {
1052
- expect(countInChoices('1', null)).toBe(0);
1053
- });
1054
-
1055
- it('returns 0 for undefined', () => {
1056
- expect(countInChoices('1', undefined)).toBe(0);
1057
- });
1058
-
1059
- it('returns 0 when choice not found', () => {
1060
- expect(countInChoices('3', ['1', '2'])).toBe(0);
1061
- });
1062
- });
1063
-
1064
- describe('removeCategory', () => {
1065
- it('removes category by id', () => {
1066
- const result = removeCategory('1', [answer('1', ['1']), answer('2', ['2'])]);
1067
- expect(result).toEqual([answer('2', ['2'])]);
1068
- });
1069
-
1070
- it('removes multiple occurrences', () => {
1071
- const result = removeCategory('1', [answer('1', ['1']), answer('1', ['2']), answer('2', ['3'])]);
1072
- expect(result).toEqual([answer('2', ['3'])]);
1073
- });
1074
-
1075
- it('returns all answers when category not found', () => {
1076
- const answers = [answer('1', ['1']), answer('2', ['2'])];
1077
- const result = removeCategory('3', answers);
1078
- expect(result).toEqual(answers);
1079
- });
1080
-
1081
- it('handles empty answers array', () => {
1082
- const result = removeCategory('1', []);
1083
- expect(result).toEqual([]);
1084
- });
1085
- });
1086
-
1087
- describe('rearrangeChoices', () => {
1088
- it('swaps elements at given indices', () => {
1089
- const choices = ['1', '2', '3', '4'];
1090
- const result = rearrangeChoices(choices, 0, 2);
1091
- expect(result).toEqual(['3', '2', '1', '4']);
1092
- });
1093
-
1094
- it('handles empty array', () => {
1095
- const result = rearrangeChoices([], 0, 1);
1096
- expect(result).toEqual([]);
1097
- });
1098
-
1099
- it('handles single element array', () => {
1100
- const choices = ['1'];
1101
- const result = rearrangeChoices(choices, 0, 0);
1102
- expect(result).toEqual(['1']);
1103
- });
1104
-
1105
- it('rearranges when moving forward', () => {
1106
- const choices = ['a', 'b', 'c'];
1107
- const result = rearrangeChoices(choices, 0, 2);
1108
- expect(result).toEqual(['c', 'b', 'a']);
1109
- });
1110
- });
1111
-
1112
- describe('verifyAllowMultiplePlacements', () => {
1113
- it('removes duplicates from other categories', () => {
1114
- const answers = [
1115
- { category: '1', choices: ['a', 'b'] },
1116
- { category: '2', choices: ['a', 'c'] },
1117
- ];
1118
- const result = verifyAllowMultiplePlacements({ id: 'a' }, '1', answers);
1119
-
1120
- expect(result[0].choices).toEqual(['a', 'b']);
1121
- expect(result[1].choices).toEqual(['c']);
1122
- });
1123
-
1124
- it('removes duplicate choices in target category', () => {
1125
- const answers = [{ category: '1', choices: [{ id: 'a' }, { id: 'a' }, { id: 'b' }] }];
1126
- const result = verifyAllowMultiplePlacements({ id: 'a' }, '1', answers);
1127
-
1128
- expect(result[0].choices).toEqual([{ id: 'a' }, { id: 'b' }]);
1129
- });
1130
-
1131
- it('handles empty choices arrays', () => {
1132
- const answers = [
1133
- { category: '1', choices: [] },
1134
- { category: '2', choices: [] },
1135
- ];
1136
- const result = verifyAllowMultiplePlacements({ id: 'a' }, '1', answers);
1137
-
1138
- expect(result).toEqual(answers);
1139
- });
1140
- });
1141
-
1142
- describe('removeChoiceFromAlternate', () => {
1143
- it('removes choice from alternate at specified index', () => {
1144
- const answers = [
1145
- answer(
1146
- '1',
1147
- ['1'],
1148
- [
1149
- ['a', 'b'],
1150
- ['c', 'd'],
1151
- ],
1152
- ),
1153
- ];
1154
- const result = removeChoiceFromAlternate('b', '1', 1, 0, answers);
1155
-
1156
- expect(result[0].alternateResponses[0]).toEqual(['a']);
1157
- });
1158
-
1159
- it('does not affect other categories', () => {
1160
- const answers = [answer('1', ['1'], [['a', 'b']]), answer('2', ['2'], [['c', 'd']])];
1161
- const result = removeChoiceFromAlternate('b', '1', 1, 0, answers);
1162
-
1163
- expect(result[1].alternateResponses[0]).toEqual(['c', 'd']);
1164
- });
1165
-
1166
- it('handles null answers', () => {
1167
- const result = removeChoiceFromAlternate('a', '1', 0, 0, null);
1168
- expect(result).toEqual([]);
1169
- });
1170
-
1171
- it('handles undefined answers', () => {
1172
- const result = removeChoiceFromAlternate('a', '1', 0, 0, undefined);
1173
- expect(result).toEqual([]);
1174
- });
1175
- });
1176
-
1177
- describe('stillSelectable', () => {
1178
- it('returns true when categoryCount is 0', () => {
1179
- const result = stillSelectable({ id: '1', categoryCount: 0 }, []);
1180
- expect(result).toBe(true);
1181
- });
1182
-
1183
- it('returns true when categoryCount is undefined', () => {
1184
- const result = stillSelectable({ id: '1' }, []);
1185
- expect(result).toBe(true);
1186
- });
1187
-
1188
- it('returns true when count is below categoryCount', () => {
1189
- const builtCategories = [cats(['1'])];
1190
- const result = stillSelectable({ id: '1', categoryCount: 2 }, builtCategories);
1191
- expect(result).toBe(true);
1192
- });
1193
-
1194
- it('returns false when count equals categoryCount', () => {
1195
- const builtCategories = [cats(['1', '1'])];
1196
- const result = stillSelectable({ id: '1', categoryCount: 2 }, builtCategories);
1197
- expect(result).toBe(false);
1198
- });
1199
-
1200
- it('returns false when count exceeds categoryCount', () => {
1201
- const builtCategories = [cats(['1', '1', '1'])];
1202
- const result = stillSelectable({ id: '1', categoryCount: 2 }, builtCategories);
1203
- expect(result).toBe(false);
1204
- });
1205
- });
1206
-
1207
- describe('getBuiltCategories', () => {
1208
- it('returns built categories with correctness', () => {
1209
- const possibleResponse = { 1: ['a', 'b'] };
1210
- const builtCategories = [
1211
- {
1212
- id: '1',
1213
- label: 'Cat 1',
1214
- choices: [
1215
- { id: 'a', content: 'A' },
1216
- { id: 'b', content: 'B' },
1217
- ],
1218
- },
1219
- ];
1220
-
1221
- const result = getBuiltCategories(possibleResponse, builtCategories);
1222
-
1223
- expect(result.correct).toBe(true);
1224
- expect(result.builtCategories[0].correct).toBe(true);
1225
- expect(result.builtCategories[0].choices[0].correct).toBe(true);
1226
- expect(result.builtCategories[0].choices[1].correct).toBe(true);
1227
- });
1228
-
1229
- it('marks as incorrect when choices do not match', () => {
1230
- const possibleResponse = { 1: ['a'] };
1231
- const builtCategories = [
1232
- {
1233
- id: '1',
1234
- label: 'Cat 1',
1235
- choices: [{ id: 'b', content: 'B' }],
1236
- },
1237
- ];
1238
-
1239
- const result = getBuiltCategories(possibleResponse, builtCategories);
1240
-
1241
- expect(result.correct).toBe(false);
1242
- expect(result.builtCategories[0].correct).toBe(false);
1243
- expect(result.builtCategories[0].choices[0].correct).toBe(false);
1244
- });
1245
-
1246
- it('handles empty possibleResponse for category', () => {
1247
- const possibleResponse = {};
1248
- const builtCategories = [
1249
- {
1250
- id: '1',
1251
- label: 'Cat 1',
1252
- choices: [],
1253
- },
1254
- ];
1255
-
1256
- const result = getBuiltCategories(possibleResponse, builtCategories);
1257
-
1258
- expect(result.correct).toBe(true);
1259
- expect(result.builtCategories[0].correct).toBe(true);
1260
- });
1261
- });
1262
- });