@pie-lib/controller-utils 0.2.44 → 0.3.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.
- package/CHANGELOG.md +15 -32
- package/NEXT.CHANGELOG.json +1 -0
- package/lib/index.js.map +1 -1
- package/lib/partial-scoring.js +1 -5
- package/lib/partial-scoring.js.map +1 -1
- package/lib/persistence.js +7 -7
- package/lib/persistence.js.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/partial-scoring.test.js +25 -0
- package/src/__tests__/persistence.test.js +151 -0
- package/src/index.js +1 -0
- package/src/partial-scoring.js +1 -3
- package/src/persistence.js +3 -5
- package/README.md +0 -5
package/CHANGELOG.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
# [0.3.0-beta.1](https://github.com/pie-framework/pie-lib/compare/@pie-lib/controller-utils@0.2.44...@pie-lib/controller-utils@0.3.0-beta.1) (2025-07-20)
|
|
7
7
|
|
|
8
8
|
**Note:** Version bump only for package @pie-lib/controller-utils
|
|
9
9
|
|
|
@@ -11,81 +11,64 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
|
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
# [0.3.0-beta.0](https://github.com/pie-framework/pie-lib/compare/@pie-lib/controller-utils@0.2.44...@pie-lib/controller-utils@0.3.0-beta.0) (2025-07-20)
|
|
16
15
|
|
|
17
|
-
|
|
16
|
+
**Note:** Version bump only for package @pie-lib/controller-utils
|
|
18
17
|
|
|
19
|
-
* **eslint:** fix eslint problems-PD-2732 ([9eb9976](https://github.com/pie-framework/pie-lib/commit/9eb9976749753da86e1057a07bd2cfc65bf64ae4))
|
|
20
18
|
|
|
21
19
|
|
|
22
20
|
|
|
23
21
|
|
|
24
|
-
|
|
25
|
-
## [0.2.42](https://github.com/pie-framework/pie-lib/compare/@pie-lib/controller-utils@0.2.40...@pie-lib/controller-utils@0.2.42) (2023-01-31)
|
|
22
|
+
# [0.4.0-beta.0](https://github.com/pie-framework/pie-lib/compare/@pie-lib/controller-utils@0.2.44...@pie-lib/controller-utils@0.4.0-beta.0) (2025-07-15)
|
|
26
23
|
|
|
27
24
|
**Note:** Version bump only for package @pie-lib/controller-utils
|
|
28
25
|
|
|
26
|
+
# [0.3.0-beta.0](https://github.com/pie-framework/pie-lib/compare/@pie-lib/controller-utils@0.2.44...@pie-lib/controller-utils@0.3.0-beta.0) (2025-07-15)
|
|
29
27
|
|
|
28
|
+
**Note:** Version bump only for package @pie-lib/controller-utils
|
|
30
29
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
## [0.2.40](https://github.com/pie-framework/pie-lib/compare/@pie-lib/controller-utils@0.2.39...@pie-lib/controller-utils@0.2.40) (2023-01-31)
|
|
30
|
+
## [0.2.44](https://github.com/pie-framework/pie-lib/compare/@pie-lib/controller-utils@0.2.43...@pie-lib/controller-utils@0.2.44) (2023-06-24)
|
|
34
31
|
|
|
35
32
|
**Note:** Version bump only for package @pie-lib/controller-utils
|
|
36
33
|
|
|
34
|
+
## [0.2.43](https://github.com/pie-framework/pie-lib/compare/@pie-lib/controller-utils@0.2.42...@pie-lib/controller-utils@0.2.43) (2023-04-19)
|
|
37
35
|
|
|
36
|
+
### Bug Fixes
|
|
38
37
|
|
|
38
|
+
- **eslint:** fix eslint problems-PD-2732 ([9eb9976](https://github.com/pie-framework/pie-lib/commit/9eb9976749753da86e1057a07bd2cfc65bf64ae4))
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
## [0.2.39](https://github.com/pie-framework/pie-lib/compare/@pie-lib/controller-utils@0.2.38...@pie-lib/controller-utils@0.2.39) (2023-01-31)
|
|
40
|
+
## [0.2.42](https://github.com/pie-framework/pie-lib/compare/@pie-lib/controller-utils@0.2.40...@pie-lib/controller-utils@0.2.42) (2023-01-31)
|
|
42
41
|
|
|
43
42
|
**Note:** Version bump only for package @pie-lib/controller-utils
|
|
44
43
|
|
|
44
|
+
## [0.2.40](https://github.com/pie-framework/pie-lib/compare/@pie-lib/controller-utils@0.2.39...@pie-lib/controller-utils@0.2.40) (2023-01-31)
|
|
45
45
|
|
|
46
|
+
**Note:** Version bump only for package @pie-lib/controller-utils
|
|
46
47
|
|
|
48
|
+
## [0.2.39](https://github.com/pie-framework/pie-lib/compare/@pie-lib/controller-utils@0.2.38...@pie-lib/controller-utils@0.2.39) (2023-01-31)
|
|
47
49
|
|
|
50
|
+
**Note:** Version bump only for package @pie-lib/controller-utils
|
|
48
51
|
|
|
49
52
|
## [0.2.38](https://github.com/pie-framework/pie-lib/compare/@pie-lib/controller-utils@0.2.37...@pie-lib/controller-utils@0.2.38) (2023-01-31)
|
|
50
53
|
|
|
51
54
|
**Note:** Version bump only for package @pie-lib/controller-utils
|
|
52
55
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
56
|
## [0.2.37](https://github.com/pie-framework/pie-lib/compare/@pie-lib/controller-utils@0.2.31...@pie-lib/controller-utils@0.2.37) (2023-01-31)
|
|
58
57
|
|
|
59
58
|
**Note:** Version bump only for package @pie-lib/controller-utils
|
|
60
59
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
60
|
## [0.2.31](https://github.com/pie-framework/pie-lib/compare/@pie-lib/controller-utils@0.2.29...@pie-lib/controller-utils@0.2.31) (2023-01-31)
|
|
66
61
|
|
|
67
62
|
**Note:** Version bump only for package @pie-lib/controller-utils
|
|
68
63
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
64
|
## [0.2.29](https://github.com/pie-framework/pie-lib/compare/@pie-lib/controller-utils@0.2.27...@pie-lib/controller-utils@0.2.29) (2023-01-31)
|
|
74
65
|
|
|
75
66
|
**Note:** Version bump only for package @pie-lib/controller-utils
|
|
76
67
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
68
|
## [0.2.27](https://github.com/pie-framework/pie-lib/compare/@pie-lib/controller-utils@0.2.26...@pie-lib/controller-utils@0.2.27) (2022-11-23)
|
|
82
69
|
|
|
83
70
|
**Note:** Version bump only for package @pie-lib/controller-utils
|
|
84
71
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
72
|
## [0.2.26](https://github.com/pie-framework/pie-lib/compare/@pie-lib/controller-utils@0.2.24...@pie-lib/controller-utils@0.2.26) (2021-06-25)
|
|
90
73
|
|
|
91
74
|
**Note:** Version bump only for package @pie-lib/controller-utils
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
[]
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;
|
|
1
|
+
{"version":3,"sources":["../src/index.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;AAEA","sourcesContent":["import * as partialScoring from './partial-scoring';\n\nexport { lockChoices, getShuffledChoices } from './persistence';\nexport { partialScoring };\n"],"file":"index.js"}
|
package/lib/partial-scoring.js
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
3
|
Object.defineProperty(exports, "__esModule", {
|
|
6
4
|
value: true
|
|
7
5
|
});
|
|
8
6
|
exports.enabled = void 0;
|
|
9
7
|
|
|
10
|
-
var _lodash = _interopRequireDefault(require("lodash"));
|
|
11
|
-
|
|
12
8
|
var enabled = function enabled(config, env, defaultValue) {
|
|
13
9
|
// if model.partialScoring = false
|
|
14
10
|
// - if env.partialScoring = false || env.partialScoring = true => use dichotomous scoring
|
|
@@ -26,7 +22,7 @@ var enabled = function enabled(config, env, defaultValue) {
|
|
|
26
22
|
return false;
|
|
27
23
|
}
|
|
28
24
|
|
|
29
|
-
return
|
|
25
|
+
return typeof defaultValue === 'boolean' ? defaultValue : true;
|
|
30
26
|
};
|
|
31
27
|
|
|
32
28
|
exports.enabled = enabled;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/partial-scoring.js"],"names":["enabled","config","env","defaultValue","partialScoring"
|
|
1
|
+
{"version":3,"sources":["../src/partial-scoring.js"],"names":["enabled","config","env","defaultValue","partialScoring"],"mappings":";;;;;;;AAAO,IAAMA,OAAO,GAAG,SAAVA,OAAU,CAACC,MAAD,EAASC,GAAT,EAAcC,YAAd,EAA+B;AACpD;AACA;AACA;AACA;AACA;AACAF,EAAAA,MAAM,GAAGA,MAAM,IAAI,EAAnB;AACAC,EAAAA,GAAG,GAAGA,GAAG,IAAI,EAAb;;AAEA,MAAID,MAAM,CAACG,cAAP,KAA0B,KAA9B,EAAqC;AACnC,WAAO,KAAP;AACD;;AAED,MAAIF,GAAG,CAACE,cAAJ,KAAuB,KAA3B,EAAkC;AAChC,WAAO,KAAP;AACD;;AAED,SAAO,OAAOD,YAAP,KAAwB,SAAxB,GAAoCA,YAApC,GAAmD,IAA1D;AACD,CAlBM","sourcesContent":["export const enabled = (config, env, defaultValue) => {\n // if model.partialScoring = false\n // - if env.partialScoring = false || env.partialScoring = true => use dichotomous scoring\n // else if model.partialScoring = true || undefined\n // - if env.partialScoring = false, use dichotomous scoring\n // - else if env.partialScoring = true, use partial scoring\n config = config || {};\n env = env || {};\n\n if (config.partialScoring === false) {\n return false;\n }\n\n if (env.partialScoring === false) {\n return false;\n }\n\n return typeof defaultValue === 'boolean' ? defaultValue : true;\n};\n"],"file":"partial-scoring.js"}
|
package/lib/persistence.js
CHANGED
|
@@ -15,10 +15,6 @@ var _shuffle = _interopRequireDefault(require("lodash/shuffle"));
|
|
|
15
15
|
|
|
16
16
|
var _isEmpty = _interopRequireDefault(require("lodash/isEmpty"));
|
|
17
17
|
|
|
18
|
-
var _isNull = _interopRequireDefault(require("lodash/isNull"));
|
|
19
|
-
|
|
20
|
-
var _isUndefined = _interopRequireDefault(require("lodash/isUndefined"));
|
|
21
|
-
|
|
22
18
|
// eslint-disable-next-line no-console
|
|
23
19
|
var lg = function lg(n) {
|
|
24
20
|
return console[n].bind(console, 'controller-utils:');
|
|
@@ -32,7 +28,7 @@ var error = lg('error');
|
|
|
32
28
|
var compact = function compact(arr) {
|
|
33
29
|
if (Array.isArray(arr)) {
|
|
34
30
|
return arr.filter(function (v) {
|
|
35
|
-
return
|
|
31
|
+
return v !== null && v !== undefined;
|
|
36
32
|
});
|
|
37
33
|
}
|
|
38
34
|
|
|
@@ -43,16 +39,20 @@ exports.compact = compact;
|
|
|
43
39
|
|
|
44
40
|
var getShuffledChoices = function getShuffledChoices(choices, session, updateSession, choiceKey) {
|
|
45
41
|
return new Promise(function (resolve) {
|
|
42
|
+
var _session$data;
|
|
43
|
+
|
|
46
44
|
log('updateSession type: ', (0, _typeof2["default"])(updateSession));
|
|
47
45
|
log('session: ', session);
|
|
48
|
-
var currentShuffled = compact((session ||
|
|
46
|
+
var currentShuffled = compact((session === null || session === void 0 ? void 0 : (_session$data = session.data) === null || _session$data === void 0 ? void 0 : _session$data.shuffledValues) || (session === null || session === void 0 ? void 0 : session.shuffledValues) || []);
|
|
49
47
|
|
|
50
48
|
if (!session) {
|
|
51
49
|
// eslint-disable-next-line quotes
|
|
52
50
|
warn("unable to save shuffled choices because there's no session.");
|
|
53
51
|
resolve(undefined);
|
|
54
52
|
} else if (!(0, _isEmpty["default"])(currentShuffled)) {
|
|
55
|
-
|
|
53
|
+
var _session$data2;
|
|
54
|
+
|
|
55
|
+
debug('use shuffledValues to sort the choices...', (_session$data2 = session.data) === null || _session$data2 === void 0 ? void 0 : _session$data2.shuffledValues);
|
|
56
56
|
resolve(compact(currentShuffled.map(function (v) {
|
|
57
57
|
return choices.find(function (c) {
|
|
58
58
|
return c[choiceKey] === v;
|
package/lib/persistence.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/persistence.js"],"names":["lg","n","console","bind","debug","log","warn","error","compact","arr","Array","isArray","filter","v","getShuffledChoices","choices","session","updateSession","choiceKey","Promise","resolve","currentShuffled","
|
|
1
|
+
{"version":3,"sources":["../src/persistence.js"],"names":["lg","n","console","bind","debug","log","warn","error","compact","arr","Array","isArray","filter","v","undefined","getShuffledChoices","choices","session","updateSession","choiceKey","Promise","resolve","currentShuffled","data","shuffledValues","map","find","c","shuffledChoices","id","element","JSON","stringify","e","lockChoices","model","env","lockChoiceOrder","role"],"mappings":";;;;;;;;;;;AAAA;;AACA;;AACA;;AAEA;AACA,IAAMA,EAAE,GAAG,SAALA,EAAK,CAACC,CAAD;AAAA,SAAOC,OAAO,CAACD,CAAD,CAAP,CAAWE,IAAX,CAAgBD,OAAhB,EAAyB,mBAAzB,CAAP;AAAA,CAAX;;AACA,IAAME,KAAK,GAAGJ,EAAE,CAAC,OAAD,CAAhB;AACA,IAAMK,GAAG,GAAGL,EAAE,CAAC,KAAD,CAAd;AACA,IAAMM,IAAI,GAAGN,EAAE,CAAC,MAAD,CAAf;AACA,IAAMO,KAAK,GAAGP,EAAE,CAAC,OAAD,CAAhB;;AAEO,IAAMQ,OAAO,GAAG,SAAVA,OAAU,CAACC,GAAD,EAAS;AAC9B,MAAIC,KAAK,CAACC,OAAN,CAAcF,GAAd,CAAJ,EAAwB;AACtB,WAAOA,GAAG,CAACG,MAAJ,CAAW,UAACC,CAAD;AAAA,aAAOA,CAAC,KAAK,IAAN,IAAcA,CAAC,KAAKC,SAA3B;AAAA,KAAX,CAAP;AACD;;AACD,SAAOL,GAAP;AACD,CALM;;;;AAOA,IAAMM,kBAAkB,GAAG,SAArBA,kBAAqB,CAACC,OAAD,EAAUC,OAAV,EAAmBC,aAAnB,EAAkCC,SAAlC;AAAA,SAChC,IAAIC,OAAJ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J,EAAS,MAAT,EAAiB,SAAjB,CAAb;;AAEA,MAAIE,IAAI,KAAK,YAAb,EAA2B;AACzB;;AACA;AACJ;AACA;AACA;AACA;AAGI,WAAO,IAAP;AACD,GAvBiD,CAyBlD;;;AACA,SAAO,KAAP;AACD,CA3BM","sourcesContent":["import get from 'lodash/get';\nimport shuffle from 'lodash/shuffle';\nimport isEmpty from 'lodash/isEmpty';\n\n// eslint-disable-next-line no-console\nconst lg = (n) => console[n].bind(console, 'controller-utils:');\nconst debug = lg('debug');\nconst log = lg('log');\nconst warn = lg('warn');\nconst error = lg('error');\n\nexport const compact = (arr) => {\n if (Array.isArray(arr)) {\n return arr.filter((v) => v !== null && v !== undefined);\n }\n return arr;\n};\n\nexport const getShuffledChoices = (choices, session, updateSession, choiceKey) =>\n new Promise((resolve) => {\n log('updateSession type: ', typeof updateSession);\n log('session: ', session);\n\n const currentShuffled = compact(session?.data?.shuffledValues || session?.shuffledValues || []);\n\n if (!session) {\n // eslint-disable-next-line quotes\n warn(\"unable to save shuffled choices because there's no session.\");\n resolve(undefined);\n } else if (!isEmpty(currentShuffled)) {\n debug('use shuffledValues to sort the choices...', session.data?.shuffledValues);\n resolve(compact(currentShuffled.map((v) => choices.find((c) => c[choiceKey] === v))));\n } else {\n const shuffledChoices = shuffle(choices);\n\n if (updateSession && typeof updateSession === 'function') {\n try {\n //Note: session.id refers to the id of the element within a session\n const shuffledValues = compact(shuffledChoices.map((c) => c[choiceKey]));\n log('try to save shuffledValues to session...', shuffledValues);\n log('call updateSession... ', session.id, session.element);\n if (isEmpty(shuffledValues)) {\n error(\n `shuffledValues is an empty array? - refusing to call updateSession: shuffledChoices: ${JSON.stringify(\n shuffledChoices,\n )}, key: ${choiceKey}`,\n );\n } else {\n updateSession(session.id, session.element, { shuffledValues }).catch((e) =>\n // eslint-disable-next-line no-console\n console.error('update session failed for: ', session.id, e),\n );\n }\n } catch (e) {\n warn('unable to save shuffled order for choices');\n error(e);\n }\n } else {\n warn('unable to save shuffled choices, shuffle will happen every time.');\n }\n //save this shuffle to the session for later retrieval\n resolve(shuffledChoices);\n }\n });\n\n/**\n * If we return:\n * - true - that means that the order of the choices will be ordinal (as is created in the configure item)\n * - false - that means the getShuffledChoices above will be called and that in turn means that we either\n * return the shuffled values on the session (if any exists) or we shuffle the choices\n * @param model - model to check if we should lock order\n * @param session - session to check if we should lock order\n * @param env - env to check if we should lock order\n * @returns {boolean}\n */\nexport const lockChoices = (model, session, env) => {\n if (model.lockChoiceOrder) {\n return true;\n }\n\n log('lockChoiceOrder: ', get(env, ['@pie-element', 'lockChoiceOrder'], false));\n\n if (get(env, ['@pie-element', 'lockChoiceOrder'], false)) {\n return true;\n }\n\n const role = get(env, 'role', 'student');\n\n if (role === 'instructor') {\n // TODO: .. in the future the instructor can toggle between ordinal and shuffled here, so keeping this code until then\n /*const alreadyShuffled = hasShuffledValues(session);\n\n if (alreadyShuffled) {\n return false;\n }\n\n return true;*/\n return true;\n }\n\n // here it's a student, so don't lock and it will shuffle if needs be\n return false;\n};\n"],"file":"persistence.js"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pie-lib/controller-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0-beta.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "src/index.js",
|
|
@@ -14,5 +14,5 @@
|
|
|
14
14
|
"scripts": {},
|
|
15
15
|
"author": "",
|
|
16
16
|
"license": "ISC",
|
|
17
|
-
"gitHead": "
|
|
17
|
+
"gitHead": "e2aa3ddac60f49bcb8c2562370f496323642f453"
|
|
18
18
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { enabled } from '../partial-scoring';
|
|
2
|
+
|
|
3
|
+
describe('enabled', () => {
|
|
4
|
+
const assertEnabled = (config, env, expected) => {
|
|
5
|
+
it(`returns ${expected} for: config: ${config.partialScoring}, env:${env.partialScoring}`, () => {
|
|
6
|
+
const result = enabled(config, env);
|
|
7
|
+
expect(result).toEqual(expected);
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const config = (v) => ({ partialScoring: v });
|
|
12
|
+
const env = (v) => ({ partialScoring: v });
|
|
13
|
+
|
|
14
|
+
assertEnabled(config(undefined), env(undefined), true);
|
|
15
|
+
assertEnabled(config(undefined), env(true), true);
|
|
16
|
+
assertEnabled(config(undefined), env(false), false);
|
|
17
|
+
|
|
18
|
+
assertEnabled(config(false), env(undefined), false);
|
|
19
|
+
assertEnabled(config(false), env(true), false);
|
|
20
|
+
assertEnabled(config(false), env(false), false);
|
|
21
|
+
|
|
22
|
+
assertEnabled(config(true), env(undefined), true);
|
|
23
|
+
assertEnabled(config(true), env(true), true);
|
|
24
|
+
assertEnabled(config(true), env(false), false);
|
|
25
|
+
});
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { getShuffledChoices, lockChoices } from '../persistence';
|
|
2
|
+
|
|
3
|
+
describe('getShuffledChoices', () => {
|
|
4
|
+
let choices, session, updateSession, key;
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
updateSession = jest.fn().mockResolvedValue([]);
|
|
8
|
+
session = {
|
|
9
|
+
id: '1',
|
|
10
|
+
element: 'element',
|
|
11
|
+
};
|
|
12
|
+
choices = [
|
|
13
|
+
{
|
|
14
|
+
value: 1,
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
value: 2,
|
|
18
|
+
},
|
|
19
|
+
];
|
|
20
|
+
key = 'value';
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe('0 key', () => {
|
|
24
|
+
it('calls update session w/ 0 as a key', async () => {
|
|
25
|
+
const result = await getShuffledChoices([{ value: 0 }, { value: 1 }], session, updateSession, 'value');
|
|
26
|
+
expect(updateSession).toHaveBeenCalledWith(session.id, session.element, {
|
|
27
|
+
shuffledValues: expect.arrayContaining([0, 1]),
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('handles null values in session', () => {
|
|
33
|
+
beforeEach(async () => {
|
|
34
|
+
const result = await getShuffledChoices(choices, { ...session, shuffledValues: [null] }, updateSession, 'value');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('calls updateSession w/ new shuffle cos [null] is treated as empty', () => {
|
|
38
|
+
expect(updateSession).toHaveBeenCalledWith(session.id, session.element, {
|
|
39
|
+
shuffledValues: expect.arrayContaining([1, 2]),
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
describe('bad shuffle generation does not call updateSession', () => {
|
|
44
|
+
beforeEach(async () => {
|
|
45
|
+
const result = await getShuffledChoices(choices, {}, updateSession, 'foo');
|
|
46
|
+
});
|
|
47
|
+
it('does not call update session', () => {
|
|
48
|
+
expect(updateSession).not.toHaveBeenCalled();
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe('session does not exist', () => {
|
|
53
|
+
it('returns undefined for empty session', async () => {
|
|
54
|
+
const result = await getShuffledChoices(choices, undefined, updateSession, key);
|
|
55
|
+
expect(result).toEqual(undefined);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe('session exists', () => {
|
|
60
|
+
it('returns compact choices if session has shuffledValues', async () => {
|
|
61
|
+
session = { shuffledValues: [2, 1] };
|
|
62
|
+
const result = await getShuffledChoices(choices, session, updateSession, key);
|
|
63
|
+
expect(result).toEqual(expect.arrayContaining(choices));
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('returns shuffled choices if updateSession is a function', async () => {
|
|
67
|
+
session = {};
|
|
68
|
+
updateSession = jest.fn().mockResolvedValue();
|
|
69
|
+
const result = await getShuffledChoices(choices, session, updateSession, key);
|
|
70
|
+
expect(result).toEqual(expect.arrayContaining(choices));
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('calls updateSession as expected if updateSession is a function', async () => {
|
|
74
|
+
session = { id: '1', element: 'pie-element' };
|
|
75
|
+
await getShuffledChoices(choices, session, updateSession, key);
|
|
76
|
+
expect(updateSession).toHaveBeenCalledWith('1', 'pie-element', {
|
|
77
|
+
shuffledValues: expect.arrayContaining([1, 2]),
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe('lockChoices', () => {
|
|
84
|
+
const env = (lockChoiceOrder, role = 'student') => ({
|
|
85
|
+
'@pie-element': { lockChoiceOrder },
|
|
86
|
+
role,
|
|
87
|
+
});
|
|
88
|
+
const session = (shuffledValues) => ({ shuffledValues });
|
|
89
|
+
it.each`
|
|
90
|
+
modelLock | session | env | expected
|
|
91
|
+
${true} | ${session()} | ${env(true)} | ${true}
|
|
92
|
+
${true} | ${session()} | ${env(false)} | ${true}
|
|
93
|
+
${false} | ${session()} | ${env(true)} | ${true}
|
|
94
|
+
${false} | ${session()} | ${env(false)} | ${false}
|
|
95
|
+
${false} | ${session()} | ${undefined} | ${false}
|
|
96
|
+
${false} | ${undefined} | ${undefined} | ${false}
|
|
97
|
+
${undefined} | ${session()} | ${env(false)} | ${false}
|
|
98
|
+
${undefined} | ${session()} | ${env(undefined)} | ${false}
|
|
99
|
+
${false} | ${session()} | ${env(false, 'instructor')} | ${true}
|
|
100
|
+
${false} | ${session([0, 1])} | ${env(false, 'instructor')} | ${true}
|
|
101
|
+
${false} | ${undefined} | ${env(false, 'instructor')} | ${true}
|
|
102
|
+
`('1. model.lockChoiceOrder: $modelLock, $session, $env => $expected', ({ modelLock, session, env, expected }) => {
|
|
103
|
+
const model = { lockChoiceOrder: modelLock };
|
|
104
|
+
const result = lockChoices(model, session, env);
|
|
105
|
+
expect(result).toEqual(expected);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe('lockChoices mod', () => {
|
|
110
|
+
const env = (lockChoiceOrder, role = 'student') => ({
|
|
111
|
+
'@pie-element': { lockChoiceOrder },
|
|
112
|
+
role,
|
|
113
|
+
});
|
|
114
|
+
const session = (answers = ['foo', 'bar']) => ({ answers });
|
|
115
|
+
it.each`
|
|
116
|
+
modelLock | session | env | expected
|
|
117
|
+
${true} | ${session()} | ${env(true)} | ${true}
|
|
118
|
+
${true} | ${session()} | ${env(false)} | ${true}
|
|
119
|
+
${false} | ${session()} | ${env(true)} | ${true}
|
|
120
|
+
${false} | ${session()} | ${env(false)} | ${false}
|
|
121
|
+
${undefined} | ${session()} | ${env(true)} | ${true}
|
|
122
|
+
${undefined} | ${session()} | ${env(undefined)} | ${false}
|
|
123
|
+
${undefined} | ${session()} | ${env(undefined, 'instructor')} | ${true}
|
|
124
|
+
`('2. model.lockChoiceOrder: $modelLock, $session, $env => $expected', ({ modelLock, session, env, expected }) => {
|
|
125
|
+
const model = { lockChoiceOrder: modelLock };
|
|
126
|
+
const result = lockChoices(model, session, env);
|
|
127
|
+
expect(result).toEqual(expected);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe('lockChoices', () => {
|
|
132
|
+
const env = (lockChoiceOrder, role = 'student') => ({
|
|
133
|
+
'@pie-element': { lockChoiceOrder },
|
|
134
|
+
role,
|
|
135
|
+
});
|
|
136
|
+
const session = (shuffledValues) => ({ shuffledValues, answers: ['foo', 'bar'] });
|
|
137
|
+
it.each`
|
|
138
|
+
modelLock | session | env | expected
|
|
139
|
+
${true} | ${session()} | ${env(true)} | ${true}
|
|
140
|
+
${true} | ${session()} | ${env(false)} | ${true}
|
|
141
|
+
${false} | ${session()} | ${env(true)} | ${true}
|
|
142
|
+
${false} | ${session()} | ${env(false)} | ${false}
|
|
143
|
+
${undefined} | ${session()} | ${env(true)} | ${true}
|
|
144
|
+
${undefined} | ${session()} | ${env(undefined)} | ${false}
|
|
145
|
+
${undefined} | ${session()} | ${env(undefined, 'instructor')} | ${true}
|
|
146
|
+
`('3. model.lockChoiceOrder: $modelLock, $env => $expected', ({ modelLock, session, env, expected }) => {
|
|
147
|
+
const model = { lockChoiceOrder: modelLock };
|
|
148
|
+
const result = lockChoices(model, session, env);
|
|
149
|
+
expect(result).toEqual(expected);
|
|
150
|
+
});
|
|
151
|
+
});
|
package/src/index.js
CHANGED
package/src/partial-scoring.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
|
-
|
|
3
1
|
export const enabled = (config, env, defaultValue) => {
|
|
4
2
|
// if model.partialScoring = false
|
|
5
3
|
// - if env.partialScoring = false || env.partialScoring = true => use dichotomous scoring
|
|
@@ -17,5 +15,5 @@ export const enabled = (config, env, defaultValue) => {
|
|
|
17
15
|
return false;
|
|
18
16
|
}
|
|
19
17
|
|
|
20
|
-
return
|
|
18
|
+
return typeof defaultValue === 'boolean' ? defaultValue : true;
|
|
21
19
|
};
|
package/src/persistence.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import get from 'lodash/get';
|
|
2
2
|
import shuffle from 'lodash/shuffle';
|
|
3
3
|
import isEmpty from 'lodash/isEmpty';
|
|
4
|
-
import isNull from 'lodash/isNull';
|
|
5
|
-
import isUndefined from 'lodash/isUndefined';
|
|
6
4
|
|
|
7
5
|
// eslint-disable-next-line no-console
|
|
8
6
|
const lg = (n) => console[n].bind(console, 'controller-utils:');
|
|
@@ -13,7 +11,7 @@ const error = lg('error');
|
|
|
13
11
|
|
|
14
12
|
export const compact = (arr) => {
|
|
15
13
|
if (Array.isArray(arr)) {
|
|
16
|
-
return arr.filter((v) =>
|
|
14
|
+
return arr.filter((v) => v !== null && v !== undefined);
|
|
17
15
|
}
|
|
18
16
|
return arr;
|
|
19
17
|
};
|
|
@@ -23,14 +21,14 @@ export const getShuffledChoices = (choices, session, updateSession, choiceKey) =
|
|
|
23
21
|
log('updateSession type: ', typeof updateSession);
|
|
24
22
|
log('session: ', session);
|
|
25
23
|
|
|
26
|
-
const currentShuffled = compact(
|
|
24
|
+
const currentShuffled = compact(session?.data?.shuffledValues || session?.shuffledValues || []);
|
|
27
25
|
|
|
28
26
|
if (!session) {
|
|
29
27
|
// eslint-disable-next-line quotes
|
|
30
28
|
warn("unable to save shuffled choices because there's no session.");
|
|
31
29
|
resolve(undefined);
|
|
32
30
|
} else if (!isEmpty(currentShuffled)) {
|
|
33
|
-
debug('use shuffledValues to sort the choices...', session.shuffledValues);
|
|
31
|
+
debug('use shuffledValues to sort the choices...', session.data?.shuffledValues);
|
|
34
32
|
resolve(compact(currentShuffled.map((v) => choices.find((c) => c[choiceKey] === v))));
|
|
35
33
|
} else {
|
|
36
34
|
const shuffledChoices = shuffle(choices);
|