@media-quest/builder 0.0.28 → 0.0.30
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/dist/public-api.d.ts +3 -0
- package/dist/public-api.js +13 -2
- package/dist/public-api.js.map +1 -1
- package/package.json +2 -2
- package/src/code-book/codebook.ts +10 -2
- package/src/sum-score/sum-score-variable.spec.ts +308 -308
- package/src/sum-score/sum-score-variable.ts +102 -102
- package/src/sum-score/sum-score.ts +6 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@media-quest/builder",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.30",
|
|
4
4
|
"description": "Builder library for Media-quest schemas",
|
|
5
5
|
"main": "dist/public-api.js",
|
|
6
6
|
"types": "dist/public-api.d.js",
|
|
@@ -24,6 +24,6 @@
|
|
|
24
24
|
"dts": true
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
|
-
"@media-quest/engine": "0.0.
|
|
27
|
+
"@media-quest/engine": "0.0.30"
|
|
28
28
|
}
|
|
29
29
|
}
|
|
@@ -26,8 +26,16 @@ const fromPage = (
|
|
|
26
26
|
});
|
|
27
27
|
|
|
28
28
|
const varId = modulePrefix + "_" + page.prefix;
|
|
29
|
+
const pageIncludedInSumScoresArray = page.includedInSumScores ?? [];
|
|
29
30
|
|
|
30
|
-
const includedInSumScores =
|
|
31
|
+
const includedInSumScores: SumScoreVariableDto[] = [];
|
|
32
|
+
pageIncludedInSumScoresArray.forEach((p) => {
|
|
33
|
+
sumScoreVariables.forEach((ssv) => {
|
|
34
|
+
if (p.sumScoreVariableId === ssv.id) {
|
|
35
|
+
includedInSumScores.push(ssv);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
});
|
|
31
39
|
|
|
32
40
|
const variable: CodeBookQuestionVariable = {
|
|
33
41
|
kind: "codebook-question-variable",
|
|
@@ -38,7 +46,7 @@ const fromPage = (
|
|
|
38
46
|
modulePrefix,
|
|
39
47
|
pagePosition,
|
|
40
48
|
varId,
|
|
41
|
-
includedInSumScores
|
|
49
|
+
includedInSumScores,
|
|
42
50
|
};
|
|
43
51
|
|
|
44
52
|
variables.push(variable);
|
|
@@ -1,308 +1,308 @@
|
|
|
1
|
-
import { SumScore } from "./sum-score";
|
|
2
|
-
import { SumScoreVariable, SumScoreVariableDto } from "./sum-score-variable";
|
|
3
|
-
import { SchemaID, SumScoreVariableID } from "../primitives/ID";
|
|
4
|
-
import { SumScoreAnswer } from "./sum-score-answer";
|
|
5
|
-
import { BuilderSchema } from "../Builder-schema";
|
|
6
|
-
import { SchemaPrefix } from "../primitives/schema-prefix";
|
|
7
|
-
import { CodeBook } from "../code-book/codebook";
|
|
8
|
-
import { BuilderPage } from "../page/Builder-page";
|
|
9
|
-
import { VarID } from "../primitives/varID";
|
|
10
|
-
|
|
11
|
-
const createAnswer = (value: number): SumScoreAnswer => {
|
|
12
|
-
const varId = "v" + value;
|
|
13
|
-
const varLabel = "label for " + varId;
|
|
14
|
-
const valueLabel = "label for value " + value;
|
|
15
|
-
return {
|
|
16
|
-
varId,
|
|
17
|
-
varLabel,
|
|
18
|
-
value,
|
|
19
|
-
valueLabel,
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const createSumScore = () => {
|
|
24
|
-
const id = SumScoreVariableID.create();
|
|
25
|
-
const sumVar1: SumScoreVariableDto = {
|
|
26
|
-
id,
|
|
27
|
-
name: "",
|
|
28
|
-
description: "",
|
|
29
|
-
useAvg: true,
|
|
30
|
-
};
|
|
31
|
-
return sumVar1;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
const a0 = createAnswer(0);
|
|
35
|
-
const a1 = createAnswer(1);
|
|
36
|
-
const a2 = createAnswer(2);
|
|
37
|
-
const a3 = createAnswer(3);
|
|
38
|
-
const a4 = createAnswer(4);
|
|
39
|
-
const a5 = createAnswer(5);
|
|
40
|
-
const a6 = createAnswer(6);
|
|
41
|
-
const a7 = createAnswer(7);
|
|
42
|
-
const a8 = createAnswer(8);
|
|
43
|
-
const a9 = createAnswer(9);
|
|
44
|
-
|
|
45
|
-
const all = [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9];
|
|
46
|
-
|
|
47
|
-
describe("Sum score sum-score.", () => {
|
|
48
|
-
test("Is allowed SumScoreValue", () => {
|
|
49
|
-
expect(SumScore.isAllowedValue(-1)).toBeFalsy();
|
|
50
|
-
expect(SumScore.isAllowedValue(0)).toBeTruthy();
|
|
51
|
-
expect(SumScore.isAllowedValue(1)).toBeTruthy();
|
|
52
|
-
expect(SumScore.isAllowedValue(2)).toBeTruthy();
|
|
53
|
-
expect(SumScore.isAllowedValue(3)).toBeTruthy();
|
|
54
|
-
expect(SumScore.isAllowedValue(4)).toBeTruthy();
|
|
55
|
-
expect(SumScore.isAllowedValue(5)).toBeTruthy();
|
|
56
|
-
expect(SumScore.isAllowedValue(6)).toBeTruthy();
|
|
57
|
-
expect(SumScore.isAllowedValue(7)).toBeTruthy();
|
|
58
|
-
expect(SumScore.isAllowedValue(8)).toBeTruthy();
|
|
59
|
-
expect(SumScore.isAllowedValue(9)).toBeFalsy();
|
|
60
|
-
expect(SumScore.isAllowedValue(999)).toBeFalsy();
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
test("Will calculate 2", () => {
|
|
64
|
-
const ss1 = createSumScore();
|
|
65
|
-
const basedOn = [
|
|
66
|
-
{ varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
|
|
67
|
-
{ varId: a2.varId, varLabel: a2.varLabel, weight: 1 },
|
|
68
|
-
{ varId: a9.varId, varLabel: a9.varLabel, weight: 1 },
|
|
69
|
-
];
|
|
70
|
-
|
|
71
|
-
const score = SumScore.calculate(ss1, basedOn, all);
|
|
72
|
-
expect(score.sumScore).toBe(3);
|
|
73
|
-
expect(score.basedOn.length).toBe(3);
|
|
74
|
-
expect(score.avg).toBe(1.5);
|
|
75
|
-
expect(score.skippedBy9Count).toBe(1);
|
|
76
|
-
});
|
|
77
|
-
test("Will calculate 5 values", () => {
|
|
78
|
-
const ss1 = createSumScore();
|
|
79
|
-
const basedOn = [
|
|
80
|
-
{ varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
|
|
81
|
-
{ varId: a2.varId, varLabel: a2.varLabel, weight: 1 },
|
|
82
|
-
{ varId: a4.varId, varLabel: a4.varLabel, weight: 1 },
|
|
83
|
-
{ varId: a5.varId, varLabel: a5.varLabel, weight: 1 },
|
|
84
|
-
{ varId: a6.varId, varLabel: a6.varLabel, weight: 1 },
|
|
85
|
-
{ varId: a9.varId, varLabel: a9.varLabel, weight: 0.5 },
|
|
86
|
-
];
|
|
87
|
-
const score = SumScore.calculate(ss1, basedOn, all);
|
|
88
|
-
expect(score.sumScore).toBe(18);
|
|
89
|
-
expect(score.avg).toBe(3.6);
|
|
90
|
-
const basedOn1 = score.basedOn[0];
|
|
91
|
-
expect(basedOn1.value).toBe(a1.value);
|
|
92
|
-
expect(basedOn1.weight).toBe(1);
|
|
93
|
-
expect(basedOn1.varId).toBe(a1.varId);
|
|
94
|
-
expect(basedOn1.varLabel).toBe(a1.varLabel);
|
|
95
|
-
});
|
|
96
|
-
test("Will not include 9 in includedAnswersCount", () => {
|
|
97
|
-
const ss1 = createSumScore();
|
|
98
|
-
const basedOn = [
|
|
99
|
-
{ varId: a0.varId, varLabel: a0.varLabel, weight: 1 },
|
|
100
|
-
{ varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
|
|
101
|
-
{ varId: a5.varId, varLabel: a5.varLabel, weight: 1 },
|
|
102
|
-
{ varId: a9.varId, varLabel: a9.varLabel, weight: 1 },
|
|
103
|
-
];
|
|
104
|
-
const score = SumScore.calculate(ss1, basedOn, all);
|
|
105
|
-
expect(score.sumScore).toBe(6);
|
|
106
|
-
expect(score.avg).toBe(2);
|
|
107
|
-
expect(score.includedAnswerCount).toBe(3);
|
|
108
|
-
});
|
|
109
|
-
test("Will calculate weight", () => {
|
|
110
|
-
const ss1 = createSumScore();
|
|
111
|
-
const basedOn = [
|
|
112
|
-
{ varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
|
|
113
|
-
{ varId: a8.varId, varLabel: a8.varLabel, weight: 0.5 },
|
|
114
|
-
];
|
|
115
|
-
const score = SumScore.calculate(ss1, basedOn, all);
|
|
116
|
-
expect(score.sumScore).toBe(5);
|
|
117
|
-
});
|
|
118
|
-
test("Will calculate many weighted variables.", () => {
|
|
119
|
-
const ss1 = createSumScore();
|
|
120
|
-
const basedOn = [
|
|
121
|
-
{ varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
|
|
122
|
-
{ varId: a2.varId, varLabel: a2.varLabel, weight: 0.5 },
|
|
123
|
-
{ varId: a5.varId, varLabel: a5.varLabel, weight: 1.2 },
|
|
124
|
-
{ varId: a8.varId, varLabel: a8.varLabel, weight: 2 },
|
|
125
|
-
];
|
|
126
|
-
const score = SumScore.calculate(ss1, basedOn, all);
|
|
127
|
-
expect(score.sumScore).toBe(24);
|
|
128
|
-
});
|
|
129
|
-
test("Will count missing answers (missingAnswersCount)", () => {
|
|
130
|
-
const ss1 = createSumScore();
|
|
131
|
-
const basedOn = [
|
|
132
|
-
{ varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
|
|
133
|
-
{ varId: a2.varId, varLabel: a2.varLabel, weight: 1 },
|
|
134
|
-
{ varId: a3.varId, varLabel: a3.varLabel, weight: 1 },
|
|
135
|
-
{ varId: a5.varId, varLabel: a5.varLabel, weight: 1 },
|
|
136
|
-
{ varId: a9.varId, varLabel: a9.varLabel, weight: 1 },
|
|
137
|
-
];
|
|
138
|
-
const missing5InDataSet = [a1, a2, a8];
|
|
139
|
-
const score = SumScore.calculate(ss1, basedOn, missing5InDataSet);
|
|
140
|
-
expect(score.sumScore).toBe(3);
|
|
141
|
-
expect(score.includedAnswerCount).toBe(2);
|
|
142
|
-
expect(score.missingAnswerCount).toBe(3);
|
|
143
|
-
expect(score.avg).toBe(1.5);
|
|
144
|
-
expect(score.basedOn.length).toBe(2);
|
|
145
|
-
expect(score.avg).toBe(1.5);
|
|
146
|
-
expect(score.errorMessages.length).toBe(0);
|
|
147
|
-
});
|
|
148
|
-
test("includedAnswerCount + missingAnswerCount + skippedBy9Count = SumScoreVariable.basedOn.length", () => {
|
|
149
|
-
const ss1 = createSumScore();
|
|
150
|
-
const basedOn = [
|
|
151
|
-
{ varId: a0.varId, varLabel: a0.varLabel, weight: 1 },
|
|
152
|
-
{ varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
|
|
153
|
-
{ varId: a2.varId, varLabel: a2.varLabel, weight: 1 },
|
|
154
|
-
{ varId: a9.varId, varLabel: a9.varLabel, weight: 1 },
|
|
155
|
-
];
|
|
156
|
-
const answers = [a1, a7, a8, a9];
|
|
157
|
-
const score = SumScore.calculate(ss1, basedOn, answers);
|
|
158
|
-
|
|
159
|
-
expect(score.includedAnswerCount + score.missingAnswerCount + score.skippedBy9Count).toBe(
|
|
160
|
-
basedOn.length,
|
|
161
|
-
);
|
|
162
|
-
});
|
|
163
|
-
test("variable will also have copy of all questions it used in.", () => {
|
|
164
|
-
const prefix = SchemaPrefix.fromValueOrThrow("dep4");
|
|
165
|
-
const schema = BuilderSchema.create(SchemaID.create(), "testing-dep4", prefix.value);
|
|
166
|
-
const p1 = schema.addPage("question");
|
|
167
|
-
const p2 = schema.addPage("question");
|
|
168
|
-
const p3 = schema.addPage("question");
|
|
169
|
-
|
|
170
|
-
// Setting question text
|
|
171
|
-
p1.mainText.text = "q1 text";
|
|
172
|
-
p2.mainText.text = "q2 text";
|
|
173
|
-
p3.mainText.text = "q3 text";
|
|
174
|
-
const v1 = schema.sumScoreVariableCreate({
|
|
175
|
-
name: "ss1",
|
|
176
|
-
description: "ss1-desc",
|
|
177
|
-
useAvg: true,
|
|
178
|
-
});
|
|
179
|
-
const v2 = schema.sumScoreVariableCreate({
|
|
180
|
-
name: "ss2",
|
|
181
|
-
description: "ss2-desc",
|
|
182
|
-
useAvg: true,
|
|
183
|
-
});
|
|
184
|
-
const v3 = schema.sumScoreVariableCreate({
|
|
185
|
-
name: "ss3",
|
|
186
|
-
description: "ss3-desc",
|
|
187
|
-
useAvg: true,
|
|
188
|
-
});
|
|
189
|
-
schema.sumScoreVariableAddToPage(v1, p1, 1);
|
|
190
|
-
schema.sumScoreVariableAddToPage(v1, p2, 1);
|
|
191
|
-
schema.sumScoreVariableAddToPage(v2, p2, 1);
|
|
192
|
-
schema.sumScoreVariableAddToPage(v3, p1, 1);
|
|
193
|
-
schema.sumScoreVariableAddToPage(v3, p2, 1);
|
|
194
|
-
schema.sumScoreVariableAddToPage(v3, p3, 1);
|
|
195
|
-
|
|
196
|
-
expect(v1.usedIn.length).toBe(2);
|
|
197
|
-
expect(v2.usedIn.length).toBe(1);
|
|
198
|
-
expect(v3.usedIn.length).toBe(3);
|
|
199
|
-
schema.sumScoreVariableDeleteFromPage(p3.id, v3.id);
|
|
200
|
-
expect(v3.usedIn.length).toBe(2);
|
|
201
|
-
|
|
202
|
-
schema.sumScoreVariableDeleteFromPage(p2.id, v3.id);
|
|
203
|
-
expect(v3.usedIn.length).toBe(1);
|
|
204
|
-
|
|
205
|
-
const json = schema.toJson();
|
|
206
|
-
const clone = BuilderSchema.fromJson(json);
|
|
207
|
-
const clonedVariables = clone.sumScoreVariables;
|
|
208
|
-
expect(clonedVariables.length).toBe(3);
|
|
209
|
-
|
|
210
|
-
const v1Clone = clonedVariables.find((v) => v.id === v1.id) as SumScoreVariable;
|
|
211
|
-
const v2Clone = clonedVariables.find((v) => v.id === v2.id) as SumScoreVariable;
|
|
212
|
-
const v3Clone = clonedVariables.find((v) => v.id === v3.id) as SumScoreVariable;
|
|
213
|
-
expect(v1Clone.usedIn.length).toBe(2);
|
|
214
|
-
expect(v2Clone.usedIn.length).toBe(1);
|
|
215
|
-
expect(v3Clone.usedIn.length).toBe(1);
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
test("calculateAll", () => {
|
|
219
|
-
const prefix = SchemaPrefix.fromValueOrThrow("angst");
|
|
220
|
-
const schema = BuilderSchema.create(SchemaID.create(), "testing", prefix.value);
|
|
221
|
-
const p1 = schema.addPage("question");
|
|
222
|
-
const p2 = schema.addPage("question");
|
|
223
|
-
const p3 = schema.addPage("question");
|
|
224
|
-
|
|
225
|
-
// Setting question text
|
|
226
|
-
p1.mainText.text = "q1";
|
|
227
|
-
p2.mainText.text = "q2";
|
|
228
|
-
p3.mainText.text = "q3";
|
|
229
|
-
const v1 = schema.sumScoreVariableCreate({
|
|
230
|
-
name: "ss1",
|
|
231
|
-
description: "ss1-desc",
|
|
232
|
-
useAvg: true,
|
|
233
|
-
});
|
|
234
|
-
const v2 = schema.sumScoreVariableCreate({
|
|
235
|
-
name: "ss2",
|
|
236
|
-
description: "ss2-desc",
|
|
237
|
-
useAvg: true,
|
|
238
|
-
});
|
|
239
|
-
const v3 = schema.sumScoreVariableCreate({
|
|
240
|
-
name: "ss3",
|
|
241
|
-
description: "ss3-desc",
|
|
242
|
-
useAvg: true,
|
|
243
|
-
});
|
|
244
|
-
const success_v1_p1 = schema.sumScoreVariableAddToPage(v1, p1, 1);
|
|
245
|
-
const success_v1_p2 = schema.sumScoreVariableAddToPage(v1, p2, 1);
|
|
246
|
-
const success_v2_p2 = schema.sumScoreVariableAddToPage(v2, p2, 1);
|
|
247
|
-
const success_v3_p1 = schema.sumScoreVariableAddToPage(v3, p1, 1);
|
|
248
|
-
const success_v3_p2 = schema.sumScoreVariableAddToPage(v3, p2, 1);
|
|
249
|
-
const success_v3_p3 = schema.sumScoreVariableAddToPage(v3, p3, 1);
|
|
250
|
-
expect(success_v1_p1).toBeTruthy();
|
|
251
|
-
expect(success_v1_p2).toBeTruthy();
|
|
252
|
-
expect(success_v2_p2).toBeTruthy();
|
|
253
|
-
expect(success_v3_p1).toBeTruthy();
|
|
254
|
-
expect(success_v3_p2).toBeTruthy();
|
|
255
|
-
expect(success_v3_p3).toBeTruthy();
|
|
256
|
-
|
|
257
|
-
// const v = schema.
|
|
258
|
-
const p1_questionVariable = p1.getQuestionVariables(prefix, 0)[0];
|
|
259
|
-
const p2_questionVariable = p2.getQuestionVariables(prefix, 1)[0];
|
|
260
|
-
const p3_questionVariable = p3.getQuestionVariables(prefix, 2)[0];
|
|
261
|
-
|
|
262
|
-
const ans1: SumScoreAnswer = {
|
|
263
|
-
varId: p1_questionVariable.varId,
|
|
264
|
-
value: 1,
|
|
265
|
-
varLabel: p1_questionVariable.label,
|
|
266
|
-
valueLabel: "Ja",
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
const ans2: SumScoreAnswer = {
|
|
270
|
-
varId: p2_questionVariable.varId,
|
|
271
|
-
value: 2,
|
|
272
|
-
varLabel: p2_questionVariable.label,
|
|
273
|
-
valueLabel: "Ja",
|
|
274
|
-
};
|
|
275
|
-
const ans3: SumScoreAnswer = {
|
|
276
|
-
varId: p3_questionVariable.varId,
|
|
277
|
-
value: 3,
|
|
278
|
-
varLabel: p3_questionVariable.label,
|
|
279
|
-
valueLabel: "Ja",
|
|
280
|
-
};
|
|
281
|
-
|
|
282
|
-
const sumScores = SumScore.calculateAll(schema.toJson(), [ans1, ans2, ans3]);
|
|
283
|
-
const ss1 = sumScores[0] as SumScore;
|
|
284
|
-
const ss2 = sumScores[1] as SumScore;
|
|
285
|
-
const ss3 = sumScores[2] as SumScore;
|
|
286
|
-
|
|
287
|
-
expect(sumScores.length).toBe(3);
|
|
288
|
-
// BASED ON IS WORKING.
|
|
289
|
-
expect(ss1.basedOn.length).toBe(2);
|
|
290
|
-
expect(ss2.basedOn.length).toBe(1);
|
|
291
|
-
expect(ss3.basedOn.length).toBe(3);
|
|
292
|
-
const isBasedOn = (sumScore: SumScore, variableId: string, variableLabel: string) => {
|
|
293
|
-
const found = sumScore.basedOn.find((basedOnItem) => basedOnItem.varId === variableId);
|
|
294
|
-
|
|
295
|
-
expect(found?.varId).toBe(variableId);
|
|
296
|
-
expect(found?.varLabel).toBe(variableLabel);
|
|
297
|
-
};
|
|
298
|
-
isBasedOn(ss1, ans1.varId, p1_questionVariable.label);
|
|
299
|
-
isBasedOn(ss1, ans2.varId, p2_questionVariable.label);
|
|
300
|
-
isBasedOn(ss2, ans2.varId, p2_questionVariable.label);
|
|
301
|
-
isBasedOn(ss3, ans1.varId, p1_questionVariable.label);
|
|
302
|
-
isBasedOn(ss3, ans2.varId, p2_questionVariable.label);
|
|
303
|
-
isBasedOn(ss3, ans3.varId, p3_questionVariable.label);
|
|
304
|
-
expect(ss1.sumScore).toBe(3);
|
|
305
|
-
expect(ss2.sumScore).toBe(2);
|
|
306
|
-
expect(ss3.sumScore).toBe(6);
|
|
307
|
-
});
|
|
308
|
-
});
|
|
1
|
+
import { SumScore } from "./sum-score";
|
|
2
|
+
import { SumScoreVariable, SumScoreVariableDto } from "./sum-score-variable";
|
|
3
|
+
import { SchemaID, SumScoreVariableID } from "../primitives/ID";
|
|
4
|
+
import { SumScoreAnswer } from "./sum-score-answer";
|
|
5
|
+
import { BuilderSchema } from "../Builder-schema";
|
|
6
|
+
import { SchemaPrefix } from "../primitives/schema-prefix";
|
|
7
|
+
import { CodeBook } from "../code-book/codebook";
|
|
8
|
+
import { BuilderPage } from "../page/Builder-page";
|
|
9
|
+
import { VarID } from "../primitives/varID";
|
|
10
|
+
|
|
11
|
+
const createAnswer = (value: number): SumScoreAnswer => {
|
|
12
|
+
const varId = "v" + value;
|
|
13
|
+
const varLabel = "label for " + varId;
|
|
14
|
+
const valueLabel = "label for value " + value;
|
|
15
|
+
return {
|
|
16
|
+
varId,
|
|
17
|
+
varLabel,
|
|
18
|
+
value,
|
|
19
|
+
valueLabel,
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const createSumScore = () => {
|
|
24
|
+
const id = SumScoreVariableID.create();
|
|
25
|
+
const sumVar1: SumScoreVariableDto = {
|
|
26
|
+
id,
|
|
27
|
+
name: "",
|
|
28
|
+
description: "",
|
|
29
|
+
useAvg: true,
|
|
30
|
+
};
|
|
31
|
+
return sumVar1;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const a0 = createAnswer(0);
|
|
35
|
+
const a1 = createAnswer(1);
|
|
36
|
+
const a2 = createAnswer(2);
|
|
37
|
+
const a3 = createAnswer(3);
|
|
38
|
+
const a4 = createAnswer(4);
|
|
39
|
+
const a5 = createAnswer(5);
|
|
40
|
+
const a6 = createAnswer(6);
|
|
41
|
+
const a7 = createAnswer(7);
|
|
42
|
+
const a8 = createAnswer(8);
|
|
43
|
+
const a9 = createAnswer(9);
|
|
44
|
+
|
|
45
|
+
const all = [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9];
|
|
46
|
+
|
|
47
|
+
describe("Sum score sum-score.", () => {
|
|
48
|
+
test("Is allowed SumScoreValue", () => {
|
|
49
|
+
expect(SumScore.isAllowedValue(-1)).toBeFalsy();
|
|
50
|
+
expect(SumScore.isAllowedValue(0)).toBeTruthy();
|
|
51
|
+
expect(SumScore.isAllowedValue(1)).toBeTruthy();
|
|
52
|
+
expect(SumScore.isAllowedValue(2)).toBeTruthy();
|
|
53
|
+
expect(SumScore.isAllowedValue(3)).toBeTruthy();
|
|
54
|
+
expect(SumScore.isAllowedValue(4)).toBeTruthy();
|
|
55
|
+
expect(SumScore.isAllowedValue(5)).toBeTruthy();
|
|
56
|
+
expect(SumScore.isAllowedValue(6)).toBeTruthy();
|
|
57
|
+
expect(SumScore.isAllowedValue(7)).toBeTruthy();
|
|
58
|
+
expect(SumScore.isAllowedValue(8)).toBeTruthy();
|
|
59
|
+
expect(SumScore.isAllowedValue(9)).toBeFalsy();
|
|
60
|
+
expect(SumScore.isAllowedValue(999)).toBeFalsy();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("Will calculate 2", () => {
|
|
64
|
+
const ss1 = createSumScore();
|
|
65
|
+
const basedOn = [
|
|
66
|
+
{ varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
|
|
67
|
+
{ varId: a2.varId, varLabel: a2.varLabel, weight: 1 },
|
|
68
|
+
{ varId: a9.varId, varLabel: a9.varLabel, weight: 1 },
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
const score = SumScore.calculate(ss1, basedOn, all);
|
|
72
|
+
expect(score.sumScore).toBe(3);
|
|
73
|
+
expect(score.basedOn.length).toBe(3);
|
|
74
|
+
expect(score.avg).toBe(1.5);
|
|
75
|
+
expect(score.skippedBy9Count).toBe(1);
|
|
76
|
+
});
|
|
77
|
+
test("Will calculate 5 values", () => {
|
|
78
|
+
const ss1 = createSumScore();
|
|
79
|
+
const basedOn = [
|
|
80
|
+
{ varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
|
|
81
|
+
{ varId: a2.varId, varLabel: a2.varLabel, weight: 1 },
|
|
82
|
+
{ varId: a4.varId, varLabel: a4.varLabel, weight: 1 },
|
|
83
|
+
{ varId: a5.varId, varLabel: a5.varLabel, weight: 1 },
|
|
84
|
+
{ varId: a6.varId, varLabel: a6.varLabel, weight: 1 },
|
|
85
|
+
{ varId: a9.varId, varLabel: a9.varLabel, weight: 0.5 },
|
|
86
|
+
];
|
|
87
|
+
const score = SumScore.calculate(ss1, basedOn, all);
|
|
88
|
+
expect(score.sumScore).toBe(18);
|
|
89
|
+
expect(score.avg).toBe(3.6);
|
|
90
|
+
const basedOn1 = score.basedOn[0];
|
|
91
|
+
expect(basedOn1.value).toBe(a1.value);
|
|
92
|
+
expect(basedOn1.weight).toBe(1);
|
|
93
|
+
expect(basedOn1.varId).toBe(a1.varId);
|
|
94
|
+
expect(basedOn1.varLabel).toBe(a1.varLabel);
|
|
95
|
+
});
|
|
96
|
+
test("Will not include 9 in includedAnswersCount", () => {
|
|
97
|
+
const ss1 = createSumScore();
|
|
98
|
+
const basedOn = [
|
|
99
|
+
{ varId: a0.varId, varLabel: a0.varLabel, weight: 1 },
|
|
100
|
+
{ varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
|
|
101
|
+
{ varId: a5.varId, varLabel: a5.varLabel, weight: 1 },
|
|
102
|
+
{ varId: a9.varId, varLabel: a9.varLabel, weight: 1 },
|
|
103
|
+
];
|
|
104
|
+
const score = SumScore.calculate(ss1, basedOn, all);
|
|
105
|
+
expect(score.sumScore).toBe(6);
|
|
106
|
+
expect(score.avg).toBe(2);
|
|
107
|
+
expect(score.includedAnswerCount).toBe(3);
|
|
108
|
+
});
|
|
109
|
+
test("Will calculate weight", () => {
|
|
110
|
+
const ss1 = createSumScore();
|
|
111
|
+
const basedOn = [
|
|
112
|
+
{ varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
|
|
113
|
+
{ varId: a8.varId, varLabel: a8.varLabel, weight: 0.5 },
|
|
114
|
+
];
|
|
115
|
+
const score = SumScore.calculate(ss1, basedOn, all);
|
|
116
|
+
expect(score.sumScore).toBe(5);
|
|
117
|
+
});
|
|
118
|
+
test("Will calculate many weighted variables.", () => {
|
|
119
|
+
const ss1 = createSumScore();
|
|
120
|
+
const basedOn = [
|
|
121
|
+
{ varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
|
|
122
|
+
{ varId: a2.varId, varLabel: a2.varLabel, weight: 0.5 },
|
|
123
|
+
{ varId: a5.varId, varLabel: a5.varLabel, weight: 1.2 },
|
|
124
|
+
{ varId: a8.varId, varLabel: a8.varLabel, weight: 2 },
|
|
125
|
+
];
|
|
126
|
+
const score = SumScore.calculate(ss1, basedOn, all);
|
|
127
|
+
expect(score.sumScore).toBe(24);
|
|
128
|
+
});
|
|
129
|
+
test("Will count missing answers (missingAnswersCount)", () => {
|
|
130
|
+
const ss1 = createSumScore();
|
|
131
|
+
const basedOn = [
|
|
132
|
+
{ varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
|
|
133
|
+
{ varId: a2.varId, varLabel: a2.varLabel, weight: 1 },
|
|
134
|
+
{ varId: a3.varId, varLabel: a3.varLabel, weight: 1 },
|
|
135
|
+
{ varId: a5.varId, varLabel: a5.varLabel, weight: 1 },
|
|
136
|
+
{ varId: a9.varId, varLabel: a9.varLabel, weight: 1 },
|
|
137
|
+
];
|
|
138
|
+
const missing5InDataSet = [a1, a2, a8];
|
|
139
|
+
const score = SumScore.calculate(ss1, basedOn, missing5InDataSet);
|
|
140
|
+
expect(score.sumScore).toBe(3);
|
|
141
|
+
expect(score.includedAnswerCount).toBe(2);
|
|
142
|
+
expect(score.missingAnswerCount).toBe(3);
|
|
143
|
+
expect(score.avg).toBe(1.5);
|
|
144
|
+
expect(score.basedOn.length).toBe(2);
|
|
145
|
+
expect(score.avg).toBe(1.5);
|
|
146
|
+
expect(score.errorMessages.length).toBe(0);
|
|
147
|
+
});
|
|
148
|
+
test("includedAnswerCount + missingAnswerCount + skippedBy9Count = SumScoreVariable.basedOn.length", () => {
|
|
149
|
+
const ss1 = createSumScore();
|
|
150
|
+
const basedOn = [
|
|
151
|
+
{ varId: a0.varId, varLabel: a0.varLabel, weight: 1 },
|
|
152
|
+
{ varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
|
|
153
|
+
{ varId: a2.varId, varLabel: a2.varLabel, weight: 1 },
|
|
154
|
+
{ varId: a9.varId, varLabel: a9.varLabel, weight: 1 },
|
|
155
|
+
];
|
|
156
|
+
const answers = [a1, a7, a8, a9];
|
|
157
|
+
const score = SumScore.calculate(ss1, basedOn, answers);
|
|
158
|
+
|
|
159
|
+
expect(score.includedAnswerCount + score.missingAnswerCount + score.skippedBy9Count).toBe(
|
|
160
|
+
basedOn.length,
|
|
161
|
+
);
|
|
162
|
+
});
|
|
163
|
+
test("variable will also have copy of all questions it used in.", () => {
|
|
164
|
+
const prefix = SchemaPrefix.fromValueOrThrow("dep4");
|
|
165
|
+
const schema = BuilderSchema.create(SchemaID.create(), "testing-dep4", prefix.value);
|
|
166
|
+
const p1 = schema.addPage("question");
|
|
167
|
+
const p2 = schema.addPage("question");
|
|
168
|
+
const p3 = schema.addPage("question");
|
|
169
|
+
|
|
170
|
+
// Setting question text
|
|
171
|
+
p1.mainText.text = "q1 text";
|
|
172
|
+
p2.mainText.text = "q2 text";
|
|
173
|
+
p3.mainText.text = "q3 text";
|
|
174
|
+
const v1 = schema.sumScoreVariableCreate({
|
|
175
|
+
name: "ss1",
|
|
176
|
+
description: "ss1-desc",
|
|
177
|
+
useAvg: true,
|
|
178
|
+
});
|
|
179
|
+
const v2 = schema.sumScoreVariableCreate({
|
|
180
|
+
name: "ss2",
|
|
181
|
+
description: "ss2-desc",
|
|
182
|
+
useAvg: true,
|
|
183
|
+
});
|
|
184
|
+
const v3 = schema.sumScoreVariableCreate({
|
|
185
|
+
name: "ss3",
|
|
186
|
+
description: "ss3-desc",
|
|
187
|
+
useAvg: true,
|
|
188
|
+
});
|
|
189
|
+
schema.sumScoreVariableAddToPage(v1, p1, 1);
|
|
190
|
+
schema.sumScoreVariableAddToPage(v1, p2, 1);
|
|
191
|
+
schema.sumScoreVariableAddToPage(v2, p2, 1);
|
|
192
|
+
schema.sumScoreVariableAddToPage(v3, p1, 1);
|
|
193
|
+
schema.sumScoreVariableAddToPage(v3, p2, 1);
|
|
194
|
+
schema.sumScoreVariableAddToPage(v3, p3, 1);
|
|
195
|
+
|
|
196
|
+
expect(v1.usedIn.length).toBe(2);
|
|
197
|
+
expect(v2.usedIn.length).toBe(1);
|
|
198
|
+
expect(v3.usedIn.length).toBe(3);
|
|
199
|
+
schema.sumScoreVariableDeleteFromPage(p3.id, v3.id);
|
|
200
|
+
expect(v3.usedIn.length).toBe(2);
|
|
201
|
+
|
|
202
|
+
schema.sumScoreVariableDeleteFromPage(p2.id, v3.id);
|
|
203
|
+
expect(v3.usedIn.length).toBe(1);
|
|
204
|
+
|
|
205
|
+
const json = schema.toJson();
|
|
206
|
+
const clone = BuilderSchema.fromJson(json);
|
|
207
|
+
const clonedVariables = clone.sumScoreVariables;
|
|
208
|
+
expect(clonedVariables.length).toBe(3);
|
|
209
|
+
|
|
210
|
+
const v1Clone = clonedVariables.find((v) => v.id === v1.id) as SumScoreVariable;
|
|
211
|
+
const v2Clone = clonedVariables.find((v) => v.id === v2.id) as SumScoreVariable;
|
|
212
|
+
const v3Clone = clonedVariables.find((v) => v.id === v3.id) as SumScoreVariable;
|
|
213
|
+
expect(v1Clone.usedIn.length).toBe(2);
|
|
214
|
+
expect(v2Clone.usedIn.length).toBe(1);
|
|
215
|
+
expect(v3Clone.usedIn.length).toBe(1);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test("calculateAll", () => {
|
|
219
|
+
const prefix = SchemaPrefix.fromValueOrThrow("angst");
|
|
220
|
+
const schema = BuilderSchema.create(SchemaID.create(), "testing", prefix.value);
|
|
221
|
+
const p1 = schema.addPage("question");
|
|
222
|
+
const p2 = schema.addPage("question");
|
|
223
|
+
const p3 = schema.addPage("question");
|
|
224
|
+
|
|
225
|
+
// Setting question text
|
|
226
|
+
p1.mainText.text = "q1";
|
|
227
|
+
p2.mainText.text = "q2";
|
|
228
|
+
p3.mainText.text = "q3";
|
|
229
|
+
const v1 = schema.sumScoreVariableCreate({
|
|
230
|
+
name: "ss1",
|
|
231
|
+
description: "ss1-desc",
|
|
232
|
+
useAvg: true,
|
|
233
|
+
});
|
|
234
|
+
const v2 = schema.sumScoreVariableCreate({
|
|
235
|
+
name: "ss2",
|
|
236
|
+
description: "ss2-desc",
|
|
237
|
+
useAvg: true,
|
|
238
|
+
});
|
|
239
|
+
const v3 = schema.sumScoreVariableCreate({
|
|
240
|
+
name: "ss3",
|
|
241
|
+
description: "ss3-desc",
|
|
242
|
+
useAvg: true,
|
|
243
|
+
});
|
|
244
|
+
const success_v1_p1 = schema.sumScoreVariableAddToPage(v1, p1, 1);
|
|
245
|
+
const success_v1_p2 = schema.sumScoreVariableAddToPage(v1, p2, 1);
|
|
246
|
+
const success_v2_p2 = schema.sumScoreVariableAddToPage(v2, p2, 1);
|
|
247
|
+
const success_v3_p1 = schema.sumScoreVariableAddToPage(v3, p1, 1);
|
|
248
|
+
const success_v3_p2 = schema.sumScoreVariableAddToPage(v3, p2, 1);
|
|
249
|
+
const success_v3_p3 = schema.sumScoreVariableAddToPage(v3, p3, 1);
|
|
250
|
+
expect(success_v1_p1).toBeTruthy();
|
|
251
|
+
expect(success_v1_p2).toBeTruthy();
|
|
252
|
+
expect(success_v2_p2).toBeTruthy();
|
|
253
|
+
expect(success_v3_p1).toBeTruthy();
|
|
254
|
+
expect(success_v3_p2).toBeTruthy();
|
|
255
|
+
expect(success_v3_p3).toBeTruthy();
|
|
256
|
+
|
|
257
|
+
// const v = schema.
|
|
258
|
+
const p1_questionVariable = p1.getQuestionVariables(prefix, 0)[0];
|
|
259
|
+
const p2_questionVariable = p2.getQuestionVariables(prefix, 1)[0];
|
|
260
|
+
const p3_questionVariable = p3.getQuestionVariables(prefix, 2)[0];
|
|
261
|
+
|
|
262
|
+
const ans1: SumScoreAnswer = {
|
|
263
|
+
varId: p1_questionVariable.varId,
|
|
264
|
+
value: 1,
|
|
265
|
+
varLabel: p1_questionVariable.label,
|
|
266
|
+
valueLabel: "Ja",
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const ans2: SumScoreAnswer = {
|
|
270
|
+
varId: p2_questionVariable.varId,
|
|
271
|
+
value: 2,
|
|
272
|
+
varLabel: p2_questionVariable.label,
|
|
273
|
+
valueLabel: "Ja",
|
|
274
|
+
};
|
|
275
|
+
const ans3: SumScoreAnswer = {
|
|
276
|
+
varId: p3_questionVariable.varId,
|
|
277
|
+
value: 3,
|
|
278
|
+
varLabel: p3_questionVariable.label,
|
|
279
|
+
valueLabel: "Ja",
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
const sumScores = SumScore.calculateAll(schema.toJson(), [ans1, ans2, ans3]);
|
|
283
|
+
const ss1 = sumScores[0] as SumScore;
|
|
284
|
+
const ss2 = sumScores[1] as SumScore;
|
|
285
|
+
const ss3 = sumScores[2] as SumScore;
|
|
286
|
+
|
|
287
|
+
expect(sumScores.length).toBe(3);
|
|
288
|
+
// BASED ON IS WORKING.
|
|
289
|
+
expect(ss1.basedOn.length).toBe(2);
|
|
290
|
+
expect(ss2.basedOn.length).toBe(1);
|
|
291
|
+
expect(ss3.basedOn.length).toBe(3);
|
|
292
|
+
const isBasedOn = (sumScore: SumScore, variableId: string, variableLabel: string) => {
|
|
293
|
+
const found = sumScore.basedOn.find((basedOnItem) => basedOnItem.varId === variableId);
|
|
294
|
+
|
|
295
|
+
expect(found?.varId).toBe(variableId);
|
|
296
|
+
expect(found?.varLabel).toBe(variableLabel);
|
|
297
|
+
};
|
|
298
|
+
isBasedOn(ss1, ans1.varId, p1_questionVariable.label);
|
|
299
|
+
isBasedOn(ss1, ans2.varId, p2_questionVariable.label);
|
|
300
|
+
isBasedOn(ss2, ans2.varId, p2_questionVariable.label);
|
|
301
|
+
isBasedOn(ss3, ans1.varId, p1_questionVariable.label);
|
|
302
|
+
isBasedOn(ss3, ans2.varId, p2_questionVariable.label);
|
|
303
|
+
isBasedOn(ss3, ans3.varId, p3_questionVariable.label);
|
|
304
|
+
expect(ss1.sumScore).toBe(3);
|
|
305
|
+
expect(ss2.sumScore).toBe(2);
|
|
306
|
+
expect(ss3.sumScore).toBe(6);
|
|
307
|
+
});
|
|
308
|
+
});
|