@media-quest/builder 0.0.25 → 0.0.26

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.
Files changed (44) hide show
  1. package/package.json +15 -15
  2. package/src/Builder-option.ts +64 -64
  3. package/src/Builder-page.spec.ts +2 -160
  4. package/src/Builder-page.ts +6 -83
  5. package/src/Builder-question.spec.ts +68 -68
  6. package/src/Builder-question.ts +102 -102
  7. package/src/Builder-schema.spec.ts +86 -114
  8. package/src/Builder-schema.ts +13 -9
  9. package/src/Builder-text.spec.ts +24 -24
  10. package/src/Builder-text.ts +57 -57
  11. package/src/BuilderObject.ts +30 -29
  12. package/src/BuilderTag.ts +96 -96
  13. package/src/builder-compiler.ts +1 -1
  14. package/src/code-book/codebook-variable.ts +29 -0
  15. package/src/{codebook.ts → code-book/codebook.ts} +72 -72
  16. package/src/primitives/ID.spec.ts +39 -39
  17. package/src/primitives/ID.ts +138 -119
  18. package/src/primitives/page-prefix.ts +59 -59
  19. package/src/primitives/varID.ts +12 -12
  20. package/src/public-api.ts +28 -26
  21. package/src/rulebuilder/Builder-rule.spec.ts +323 -323
  22. package/src/rulebuilder/Builder-rule.ts +191 -191
  23. package/src/rulebuilder/RuleBuilder-test-utils.ts +320 -320
  24. package/src/rulebuilder/RuleInput.ts +30 -30
  25. package/src/rulebuilder/RuleVariable.ts +34 -48
  26. package/src/rulebuilder/SingleSelectItem.ts +9 -8
  27. package/src/rulebuilder/condition/Builder-condition-group.ts +14 -6
  28. package/src/rulebuilder/condition/Builder-condition.spec.ts +12 -12
  29. package/src/rulebuilder/condition/Builder-condition.ts +17 -13
  30. package/src/rulebuilder/index.ts +16 -3
  31. package/src/rulebuilder/page-action-manager.ts +33 -33
  32. package/src/rulebuilder/rule2/Rule2.ts +211 -215
  33. package/src/schema-config.ts +25 -25
  34. package/src/sum-score/sum-score-answer.ts +6 -0
  35. package/src/sum-score/sum-score-manager.spec.ts +189 -0
  36. package/src/sum-score/sum-score-manager.ts +154 -0
  37. package/src/sum-score/sum-score-membership.ts +45 -0
  38. package/src/sum-score/sum-score-variable.spec.ts +151 -0
  39. package/src/sum-score/sum-score-variable.ts +36 -0
  40. package/src/{variable → sum-score}/sum-score.ts +122 -130
  41. package/tsconfig.tsbuildinfo +1 -1
  42. package/src/variable/b-variable.ts +0 -68
  43. package/src/variable/mq-variable.spec.ts +0 -147
  44. package/src/variable/sum-score-variable.ts +0 -50
@@ -1,215 +1,211 @@
1
- import { BuilderOperator } from "../condition/Builder-operator";
2
- import { BuilderConditionDto } from "../condition/Builder-condition";
3
- import { BuilderConditionGroupDto } from "../condition/Builder-condition-group";
4
- import { BuilderRuleDto } from "../Builder-rule";
5
-
6
- type SolveErrorReason =
7
- | "INVALID_FACT_TYPE"
8
- | "INVALID_OPERATOR"
9
- | "INVALID_VALUE"
10
- | "INVALID_VARIABLE"
11
- | "UNIMPLEMENTED_VARIABLE_TYPE"
12
- | "UNIMPLEMENTED_OPERATOR";
13
-
14
- type TrueResult = { readonly type: "IS_TRUE" };
15
- type FalseResult = { readonly type: "IS_FALSE" };
16
- type MissingFactsResult = {
17
- readonly type: "MISSING_FACTS";
18
- readonly missingVariables: ReadonlyArray<string>;
19
- };
20
- type ErrorResult = {
21
- readonly type: "HAS_ERROR";
22
- readonly reason: SolveErrorReason;
23
- readonly data: Record<string, string>;
24
- };
25
-
26
- export type EvalResult = FalseResult | TrueResult | MissingFactsResult | ErrorResult;
27
-
28
- export interface Fact2 {
29
- readonly variableType: "numeric";
30
- readonly value: number;
31
- readonly valueLabel: string;
32
- readonly variableId: string;
33
- readonly variableLabel: string;
34
- }
35
-
36
- export class FactCollection {
37
- public static isFact = (value: unknown): value is Fact2 => {
38
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
39
- return false;
40
- }
41
- const fact = value as Partial<Fact2>;
42
- if (typeof fact.variableId !== "string" || fact.variableId.length === 0) {
43
- return false;
44
- }
45
- if (typeof fact.variableLabel !== "string") {
46
- return false;
47
- }
48
- if (typeof fact.value !== "number") {
49
- return false;
50
- }
51
- if (typeof fact.valueLabel !== "string") {
52
- return false;
53
- }
54
- // NB: This is a temporary check until we have more variable types.
55
- if (typeof fact.variableType !== "string" || fact.variableType !== "numeric") {
56
- return false;
57
- }
58
-
59
- return true;
60
- };
61
- public static create = (facts: ReadonlyArray<Fact2>): FactCollection => {
62
- return new FactCollection(facts);
63
- };
64
-
65
- private readonly _facts: ReadonlyArray<Fact2>;
66
-
67
- private constructor(facts: ReadonlyArray<Fact2>) {
68
- if (!Array.isArray(facts)) {
69
- console.log("Invalid facts", facts);
70
- this._facts = [];
71
- } else {
72
- this._facts = [...facts];
73
- }
74
- }
75
- byId(variableId: string): Fact2 | false {
76
- const result = this._facts.find((fact) => fact.variableId === variableId);
77
- if (!result) {
78
- return false;
79
- }
80
- return { ...result };
81
- }
82
- }
83
- interface FactEvaluator {
84
- isTrue(facts: FactCollection): boolean;
85
- evaluate(facts: FactCollection): EvalResult;
86
- }
87
- interface IsValid {
88
- isValid(): boolean;
89
- }
90
- export class Condition implements FactEvaluator, IsValid {
91
- public static create = (dto: BuilderConditionDto): Condition => {
92
- return new Condition(dto);
93
- };
94
- private constructor(private readonly dto: BuilderConditionDto) {}
95
- isTrue(facts: FactCollection): boolean {
96
- const dto = this.dto;
97
- const op = dto.operator;
98
- const value = dto.value;
99
- const varId = dto.variableId;
100
- if (!BuilderOperator.is(op)) {
101
- return false;
102
- }
103
-
104
- if (typeof value !== "number") {
105
- return false;
106
- }
107
-
108
- const fact = facts.byId(this.dto.variableId);
109
-
110
- if (!FactCollection.isFact(fact)) {
111
- return false;
112
- }
113
-
114
- if (fact.variableType !== "numeric" && typeof fact.value !== "number") {
115
- return false;
116
- }
117
-
118
- if (op === "equal") {
119
- return fact.value === value;
120
- }
121
-
122
- if (op === "notEqual") {
123
- return fact.value !== value;
124
- }
125
-
126
- return false;
127
- }
128
- isValid(): boolean {
129
- return false;
130
- }
131
-
132
- evaluate(facts: FactCollection): EvalResult {
133
- return { type: "HAS_ERROR", reason: "UNIMPLEMENTED_VARIABLE_TYPE", data: {} }; // TODO
134
- }
135
- }
136
- export class ConditionGroup implements FactEvaluator, IsValid {
137
- public static readonly create = (dto: BuilderConditionGroupDto): ConditionGroup => {
138
- return new ConditionGroup(dto);
139
- };
140
-
141
- private readonly _conditions: ReadonlyArray<Condition>;
142
- private constructor(private readonly dto: BuilderConditionGroupDto) {
143
- this._conditions = dto.conditions.map(Condition.create);
144
- }
145
-
146
- isTrue(facts: FactCollection): boolean {
147
- const results = this._conditions.map((condition) => condition.isTrue(facts));
148
- let trueCount = 0;
149
- let falseCount = 0;
150
- results.forEach((results) => {
151
- if (results) {
152
- trueCount++;
153
- } else {
154
- falseCount++;
155
- }
156
- });
157
- if (trueCount === 0 || falseCount === 0) {
158
- return false;
159
- }
160
- const type = this.dto.type;
161
-
162
- if (type === "all" && trueCount === this._conditions.length) {
163
- return true;
164
- }
165
-
166
- if (type === "any" && trueCount > 0) {
167
- return true;
168
- }
169
-
170
- const minLimit = this.dto.count;
171
- if (type === "count" && typeof minLimit === "number" && trueCount >= minLimit) {
172
- return true;
173
- }
174
-
175
- return false;
176
- }
177
-
178
- isValid(): boolean {
179
- return true;
180
- }
181
-
182
- evaluate(facts: FactCollection): EvalResult {
183
- return { type: "HAS_ERROR", reason: "UNIMPLEMENTED_VARIABLE_TYPE", data: {} }; // TODO
184
- }
185
- }
186
- export class Rule2 implements FactEvaluator {
187
- readonly name: string;
188
- private readonly _conditions: ReadonlyArray<Condition | ConditionGroup>;
189
- public static readonly create = (dto: BuilderRuleDto): Rule2 => {
190
- return new Rule2(dto);
191
- };
192
- private _count = -1;
193
- constructor(private readonly dto: BuilderRuleDto) {
194
- this.name = dto.name;
195
- const conditions: Array<Condition | ConditionGroup> = [];
196
- dto.conditions.forEach((condition) => {
197
- if (condition.kind === "condition-group") {
198
- conditions.push(ConditionGroup.create(condition));
199
- } else if (condition.kind === "condition") {
200
- conditions.push(Condition.create(condition));
201
- } else {
202
- console.log("Unknown condition", condition);
203
- }
204
- });
205
- this._conditions = conditions;
206
- }
207
-
208
- isTrue(facts: FactCollection): boolean {
209
- return false;
210
- }
211
-
212
- evaluate(facts: FactCollection): EvalResult {
213
- return { type: "HAS_ERROR", reason: "UNIMPLEMENTED_VARIABLE_TYPE", data: {} }; // TODO
214
- }
215
- }
1
+ import { BuilderOperator } from "../condition/Builder-operator";
2
+ import { BuilderConditionDto } from "../condition/Builder-condition";
3
+ import { BuilderConditionGroupDto } from "../condition/Builder-condition-group";
4
+ import { BuilderRuleDto } from "../Builder-rule";
5
+
6
+ type SolveErrorReason =
7
+ | "INVALID_FACT_TYPE"
8
+ | "INVALID_OPERATOR"
9
+ | "INVALID_VALUE"
10
+ | "INVALID_VARIABLE"
11
+ | "UNIMPLEMENTED_VARIABLE_TYPE"
12
+ | "UNIMPLEMENTED_OPERATOR";
13
+
14
+ type TrueResult = { readonly type: "IS_TRUE" };
15
+ type FalseResult = { readonly type: "IS_FALSE" };
16
+ type MissingFactsResult = {
17
+ readonly type: "MISSING_FACTS";
18
+ readonly missingVariables: ReadonlyArray<string>;
19
+ };
20
+ type ErrorResult = {
21
+ readonly type: "HAS_ERROR";
22
+ readonly reason: SolveErrorReason;
23
+ readonly data: Record<string, string>;
24
+ };
25
+
26
+ export type EvalResult = FalseResult | TrueResult | MissingFactsResult | ErrorResult;
27
+
28
+ export interface Fact2 {
29
+ readonly variableType: "numeric";
30
+ readonly value: number;
31
+ readonly valueLabel: string;
32
+ readonly variableId: string;
33
+ readonly variableLabel: string;
34
+ }
35
+
36
+ export class FactCollection {
37
+ public static isFact = (value: unknown): value is Fact2 => {
38
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
39
+ return false;
40
+ }
41
+ const fact = value as Partial<Fact2>;
42
+ if (typeof fact.variableId !== "string" || fact.variableId.length === 0) {
43
+ return false;
44
+ }
45
+ if (typeof fact.variableLabel !== "string") {
46
+ return false;
47
+ }
48
+ if (typeof fact.value !== "number") {
49
+ return false;
50
+ }
51
+ if (typeof fact.valueLabel !== "string") {
52
+ return false;
53
+ }
54
+ // NB: This is a temporary check until we have more sum-score types.
55
+ if (typeof fact.variableType !== "string" || fact.variableType !== "numeric") {
56
+ return false;
57
+ }
58
+
59
+ return true;
60
+ };
61
+ public static create = (facts: ReadonlyArray<Fact2>): FactCollection => {
62
+ return new FactCollection(facts);
63
+ };
64
+
65
+ private readonly _facts: ReadonlyArray<Fact2>;
66
+
67
+ private constructor(facts: ReadonlyArray<Fact2>) {
68
+ if (!Array.isArray(facts)) {
69
+ console.log("Invalid facts", facts);
70
+ this._facts = [];
71
+ } else {
72
+ this._facts = [...facts];
73
+ }
74
+ }
75
+ byId(variableId: string): Fact2 | false {
76
+ const result = this._facts.find((fact) => fact.variableId === variableId);
77
+ if (!result) {
78
+ return false;
79
+ }
80
+ return { ...result };
81
+ }
82
+ }
83
+ interface FactEvaluator {
84
+ isTrue(facts: FactCollection): boolean;
85
+ evaluate(facts: FactCollection): EvalResult;
86
+ }
87
+ interface IsValid {
88
+ isValid(): boolean;
89
+ }
90
+ export class Condition implements FactEvaluator, IsValid {
91
+ public static create = (dto: BuilderConditionDto): Condition => {
92
+ return new Condition(dto);
93
+ };
94
+ private constructor(private readonly dto: BuilderConditionDto) {}
95
+ isTrue(facts: FactCollection): boolean {
96
+ const dto = this.dto;
97
+ const op = dto.operator;
98
+ const value = dto.value;
99
+ const varId = dto.variableId;
100
+ if (!BuilderOperator.is(op)) {
101
+ return false;
102
+ }
103
+
104
+ if (typeof value !== "number") {
105
+ return false;
106
+ }
107
+
108
+ const fact = facts.byId(this.dto.variableId);
109
+
110
+ if (!FactCollection.isFact(fact)) {
111
+ return false;
112
+ }
113
+
114
+ if (fact.variableType !== "numeric" && typeof fact.value !== "number") {
115
+ return false;
116
+ }
117
+
118
+ if (op === "equal") {
119
+ return fact.value === value;
120
+ }
121
+
122
+ if (op === "notEqual") {
123
+ return fact.value !== value;
124
+ }
125
+
126
+ return false;
127
+ }
128
+ isValid(): boolean {
129
+ return false;
130
+ }
131
+
132
+ evaluate(facts: FactCollection): EvalResult {
133
+ return { type: "HAS_ERROR", reason: "UNIMPLEMENTED_VARIABLE_TYPE", data: {} }; // TODO
134
+ }
135
+ }
136
+ export class ConditionGroup implements FactEvaluator, IsValid {
137
+ public static readonly create = (dto: BuilderConditionGroupDto): ConditionGroup => {
138
+ return new ConditionGroup(dto);
139
+ };
140
+
141
+ private readonly _conditions: ReadonlyArray<Condition>;
142
+ private constructor(private readonly dto: BuilderConditionGroupDto) {
143
+ this._conditions = dto.conditions.map(Condition.create);
144
+ }
145
+
146
+ isTrue(facts: FactCollection): boolean {
147
+ const results = this._conditions.map((condition) => condition.isTrue(facts));
148
+ let trueCount = 0;
149
+ let falseCount = 0;
150
+ results.forEach((results) => {
151
+ if (results) {
152
+ trueCount++;
153
+ } else {
154
+ falseCount++;
155
+ }
156
+ });
157
+ if (trueCount === 0 || falseCount === 0) {
158
+ return false;
159
+ }
160
+ const type = this.dto.type;
161
+
162
+ if (type === "all" && trueCount === this._conditions.length) {
163
+ return true;
164
+ }
165
+
166
+ if (type === "any" && trueCount > 0) {
167
+ return true;
168
+ }
169
+
170
+ const minLimit = this.dto.count;
171
+ return type === "count" && typeof minLimit === "number" && trueCount >= minLimit;
172
+ }
173
+
174
+ isValid(): boolean {
175
+ return true;
176
+ }
177
+
178
+ evaluate(facts: FactCollection): EvalResult {
179
+ return { type: "HAS_ERROR", reason: "UNIMPLEMENTED_VARIABLE_TYPE", data: {} }; // TODO
180
+ }
181
+ }
182
+ export class Rule2 implements FactEvaluator {
183
+ readonly name: string;
184
+ private readonly _conditions: ReadonlyArray<Condition | ConditionGroup>;
185
+ public static readonly create = (dto: BuilderRuleDto): Rule2 => {
186
+ return new Rule2(dto);
187
+ };
188
+ private _count = -1;
189
+ constructor(private readonly dto: BuilderRuleDto) {
190
+ this.name = dto.name;
191
+ const conditions: Array<Condition | ConditionGroup> = [];
192
+ dto.conditions.forEach((condition) => {
193
+ if (condition.kind === "condition-group") {
194
+ conditions.push(ConditionGroup.create(condition));
195
+ } else if (condition.kind === "condition") {
196
+ conditions.push(Condition.create(condition));
197
+ } else {
198
+ console.log("Unknown condition", condition);
199
+ }
200
+ });
201
+ this._conditions = conditions;
202
+ }
203
+
204
+ isTrue(facts: FactCollection): boolean {
205
+ return false;
206
+ }
207
+
208
+ evaluate(facts: FactCollection): EvalResult {
209
+ return { type: "HAS_ERROR", reason: "UNIMPLEMENTED_VARIABLE_TYPE", data: {} }; // TODO
210
+ }
211
+ }
@@ -1,25 +1,25 @@
1
- import { PredefinedVariable } from "./variable/b-variable";
2
- import { BuilderSchemaDto } from "./Builder-schema";
3
-
4
- /**
5
- * This interface is ment to define all information that a schema-admin app
6
- * needs to generate a dynamic form for setting values for predefined variables.
7
- */
8
- export interface SchemaConfig {
9
- readonly schemaName: string;
10
- readonly schemaId: string;
11
- readonly schemaPrefix: string;
12
- readonly variables: ReadonlyArray<Readonly<PredefinedVariable>>;
13
- }
14
-
15
- export const SchemaConfig = {
16
- fromSchema: (schema: BuilderSchemaDto): SchemaConfig => {
17
- const variables = schema.predefinedVariables ?? [];
18
- return {
19
- schemaId: schema.id,
20
- schemaName: schema.name,
21
- schemaPrefix: schema.prefix,
22
- variables,
23
- };
24
- },
25
- } as const;
1
+ import { CodebookPredefinedVariable } from "./code-book/codebook-variable";
2
+ import { BuilderSchemaDto } from "./Builder-schema";
3
+
4
+ /**
5
+ * This interface is ment to define all information that a schema-admin app
6
+ * needs to generate a dynamic form for setting values for predefined variables.
7
+ */
8
+ export interface SchemaConfig {
9
+ readonly schemaName: string;
10
+ readonly schemaId: string;
11
+ readonly schemaPrefix: string;
12
+ readonly variables: ReadonlyArray<CodebookPredefinedVariable>;
13
+ }
14
+
15
+ export const SchemaConfig = {
16
+ fromSchema: (schema: BuilderSchemaDto): SchemaConfig => {
17
+ const variables = schema.predefinedVariables ?? [];
18
+ return {
19
+ schemaId: schema.id,
20
+ schemaName: schema.name,
21
+ schemaPrefix: schema.prefix,
22
+ variables,
23
+ };
24
+ },
25
+ } as const;
@@ -0,0 +1,6 @@
1
+ export interface SumScoreAnswer {
2
+ readonly varId: string;
3
+ readonly varLabel: string;
4
+ readonly value: number;
5
+ readonly valueLabel: string;
6
+ }
@@ -0,0 +1,189 @@
1
+ import { SumScoreVariableDto } from "./sum-score-variable";
2
+ import { SumScoreMemberShipID, SumScoreVariableID } from "../primitives/ID";
3
+ import { SumScoreAnswer } from "./sum-score-answer";
4
+ import { SumScoreManager } from "./sum-score-manager";
5
+ import { SumScoreMembershipDto } from "./sum-score-membership";
6
+
7
+ const createAnswer = (value: number): SumScoreAnswer => {
8
+ const varId = "v" + value;
9
+ const varLabel = "label for " + varId;
10
+ const valueLabel = "label for value " + value;
11
+ return {
12
+ varId,
13
+ varLabel,
14
+ value,
15
+ valueLabel,
16
+ };
17
+ };
18
+
19
+ const a0 = createAnswer(0);
20
+ const a1 = createAnswer(1);
21
+ const a2 = createAnswer(2);
22
+ const a3 = createAnswer(3);
23
+ const a4 = createAnswer(4);
24
+ const a5 = createAnswer(5);
25
+ const a6 = createAnswer(6);
26
+ const a7 = createAnswer(7);
27
+ const a8 = createAnswer(8);
28
+ const a9 = createAnswer(9);
29
+
30
+ const all = [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9];
31
+
32
+ const getData = () => {
33
+ const dep: SumScoreVariableDto = {
34
+ basedOn: [],
35
+ description: "Hvis depresjons index er høyere enn 20, så er det alvorlig.",
36
+ id: SumScoreVariableID.dummy.a,
37
+ name: "Depresjons index",
38
+ useAvg: false,
39
+ };
40
+
41
+ const angst: SumScoreVariableDto = {
42
+ basedOn: [],
43
+ description: "Index for angst. Grenseverdi 28.",
44
+ id: SumScoreVariableID.dummy.d,
45
+ name: "Angst",
46
+ useAvg: false,
47
+ };
48
+
49
+ const depA0: SumScoreMembershipDto = {
50
+ id: SumScoreMemberShipID.create(),
51
+ sumScoreId: dep.id,
52
+ varId: a0.varId,
53
+ weight: 1,
54
+ };
55
+
56
+ const depA1: SumScoreMembershipDto = {
57
+ id: SumScoreMemberShipID.create(),
58
+ sumScoreId: dep.id,
59
+ varId: a1.varId,
60
+ weight: 1,
61
+ };
62
+
63
+ const angstA0: SumScoreMembershipDto = {
64
+ id: SumScoreMemberShipID.create(),
65
+ sumScoreId: dep.id,
66
+ varId: a0.varId,
67
+ weight: 1,
68
+ };
69
+
70
+ const angstA1: SumScoreMembershipDto = {
71
+ id: SumScoreMemberShipID.create(),
72
+ sumScoreId: dep.id,
73
+ varId: a1.varId,
74
+ weight: 1,
75
+ };
76
+
77
+ const angstA5: SumScoreMembershipDto = {
78
+ id: SumScoreMemberShipID.create(),
79
+ sumScoreId: dep.id,
80
+ varId: a5.varId,
81
+ weight: 1,
82
+ };
83
+ return { dep, angst, depA1, depA0, angstA1, angstA0, angstA5 };
84
+ };
85
+
86
+ describe("Sum-score-manager", () => {
87
+ test("Crud for variables work", () => {
88
+ const d = getData();
89
+ const m = SumScoreManager.create([d.dep, d.angst], [d.depA0, d.depA1, d.angstA5]);
90
+ expect(m.memberShips.length).toBe(3);
91
+ expect(m.variables.length).toBe(2);
92
+ const newVar = m.variableAddNew({});
93
+ expect(m.variables.length).toBe(3);
94
+ const newVar2 = m.variableAddNew({
95
+ name: "var 2 name",
96
+ description: "var 2 description",
97
+ useAvg: true,
98
+ });
99
+ expect(m.variables.length).toBe(4);
100
+
101
+ // Will not delete a variable that is not there.
102
+ m.variableDeleteById(SumScoreVariableID.create());
103
+ expect(m.variables.length).toBe(4);
104
+ m.variableDeleteById(d.dep.id);
105
+ expect(m.variables.length).toBe(3);
106
+ });
107
+ test("Can not add same variable twice", () => {
108
+ const d = getData();
109
+ const m = SumScoreManager.create([d.dep, d.angst], [d.depA0, d.depA1, d.angstA5]);
110
+ expect(m.memberShips.length).toBe(3);
111
+ expect(m.variables.length).toBe(2);
112
+ const newVar = m.variableAddNew({});
113
+ expect(m.variables.length).toBe(3);
114
+ const newVar2 = m.variableAddNew({ name: "søvnløshet", description: "", useAvg: true });
115
+ expect(m.variables.length).toBe(4);
116
+
117
+ // Will not delete a variable that is not there.
118
+ m.variableDeleteById(SumScoreVariableID.create());
119
+ expect(m.variables.length).toBe(4);
120
+ m.variableDeleteById(d.dep.id);
121
+ expect(m.variables.length).toBe(3);
122
+ });
123
+
124
+ test("Can update variable", () => {
125
+ const m = SumScoreManager.create([], []);
126
+ const v = m.variableAddNew({});
127
+ expect(m.variables.length).toBe(1);
128
+ expect(v.useAvg).toBe(true);
129
+ // Will update dep
130
+ const updatedName = "Updated name";
131
+ const updatedDescription = "Updated description";
132
+ m.variableUpdate(v.id, {
133
+ name: updatedName,
134
+ description: updatedDescription,
135
+ useAvg: false,
136
+ });
137
+ const updatedVariable = m.variableGetById(v.id) as SumScoreVariableDto;
138
+
139
+ expect(updatedVariable.name).toBe(updatedName);
140
+ expect(updatedVariable.description).toBe(updatedDescription);
141
+ expect(updatedVariable.useAvg).toBe(false);
142
+ });
143
+
144
+ test("Crud membership works", () => {
145
+ const m = SumScoreManager.create([], []);
146
+ const v1 = m.variableAddNew({ name: "V1" });
147
+ const v2 = m.variableAddNew({ name: "V2" });
148
+ expect(m.variables.length).toBe(2);
149
+ const m1 = m.membershipAdd(v1.id, "a") as SumScoreMembershipDto;
150
+ expect(m1).toBeTruthy();
151
+ const m2 = m.membershipAdd(v1.id, "b") as SumScoreMembershipDto;
152
+ expect(m2).toBeTruthy();
153
+ expect(m.memberShips.length).toBe(2);
154
+ const m2_duplicate = m.membershipAdd(v1.id, "b");
155
+ expect(m.memberShips.length).toBe(2);
156
+ expect(m.membershipGetById(m1.id)).toEqual(m1);
157
+ expect(m.membershipGetById(m2.id)).toEqual(m2);
158
+ m.membershipDelete(m2.id);
159
+ expect(m.memberShips.length).toBe(1);
160
+ });
161
+
162
+ test("Get membership by varId works", () => {
163
+ const m = SumScoreManager.create([], []);
164
+ const v1 = m.variableAddNew({ name: "V1" });
165
+ const v2 = m.variableAddNew({ name: "V2" });
166
+ const membership1 = m.membershipAdd(v1.id, "a") as SumScoreMembershipDto;
167
+ const membership2 = m.membershipAdd(v2.id, "a") as SumScoreMembershipDto;
168
+ const membership3 = m.membershipAdd(v1.id, "c") as SumScoreMembershipDto;
169
+
170
+ expect(membership1).toBeTruthy();
171
+ expect(membership2).toBeTruthy();
172
+ expect(membership3).toBeTruthy();
173
+
174
+ expect(m.membershipGetByVarId("a")).toEqual([membership1, membership2]);
175
+ expect(m.membershipGetByVarId("c")).toEqual([membership3]);
176
+ });
177
+
178
+ test("When a variable is deleted, the memberships will be deleted too.", () => {
179
+ const m = SumScoreManager.create([], []);
180
+ const v1 = m.variableAddNew({ name: "V1" });
181
+ const v2 = m.variableAddNew({ name: "V2" });
182
+ const membership1 = m.membershipAdd(v1.id, "a") as SumScoreMembershipDto;
183
+ const membership2 = m.membershipAdd(v2.id, "a") as SumScoreMembershipDto;
184
+ const membership3 = m.membershipAdd(v1.id, "c") as SumScoreMembershipDto;
185
+ expect(m.memberShips.length).toBe(3);
186
+ m.variableDeleteById(v1.id);
187
+ expect(m.membershipGetByVarId("a")).toEqual([membership2]);
188
+ });
189
+ });