@media-quest/builder 0.0.22 → 0.0.23

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 (56) hide show
  1. package/package.json +1 -1
  2. package/src/Builder-option.ts +66 -66
  3. package/src/Builder-page.spec.ts +320 -320
  4. package/src/Builder-page.ts +257 -257
  5. package/src/Builder-question.spec.ts +68 -68
  6. package/src/Builder-question.ts +101 -101
  7. package/src/Builder-schema.spec.ts +357 -306
  8. package/src/Builder-schema.ts +287 -254
  9. package/src/Builder-text.spec.ts +24 -24
  10. package/src/Builder-text.ts +57 -57
  11. package/src/BuilderMainImageDto.ts +7 -7
  12. package/src/BuilderMainText.ts +81 -81
  13. package/src/BuilderMainVideoDto.ts +10 -10
  14. package/src/BuilderObject.ts +61 -61
  15. package/src/BuilderTag.ts +97 -97
  16. package/src/builder-compiler.ts +14 -0
  17. package/src/codebook.ts +72 -72
  18. package/src/media-files.ts +28 -28
  19. package/src/primitives/page-prefix.ts +58 -58
  20. package/src/primitives/prefix.spec.ts +5 -5
  21. package/src/primitives/schema-prefix.ts +52 -52
  22. package/src/primitives/varID.ts +11 -11
  23. package/src/public-api.ts +3 -1
  24. package/src/rulebuilder/Builder-rule.spec.ts +322 -322
  25. package/src/rulebuilder/Builder-rule.ts +190 -190
  26. package/src/rulebuilder/RuleAction.ts +106 -106
  27. package/src/rulebuilder/RuleBuilder-test-utils.ts +316 -316
  28. package/src/rulebuilder/RuleInput.ts +44 -44
  29. package/src/rulebuilder/RuleVariable.ts +49 -49
  30. package/src/rulebuilder/SingleSelectItem.ts +135 -135
  31. package/src/rulebuilder/condition/Builder-condition-group.spec.ts +47 -47
  32. package/src/rulebuilder/condition/Builder-condition-group.ts +118 -118
  33. package/src/rulebuilder/condition/Builder-condition.spec.ts +195 -195
  34. package/src/rulebuilder/condition/Builder-condition.ts +208 -208
  35. package/src/rulebuilder/condition/Builder-operator.spec.ts +9 -9
  36. package/src/rulebuilder/condition/Builder-operator.ts +31 -31
  37. package/src/rulebuilder/index.ts +22 -22
  38. package/src/rulebuilder/jump-to-action-manager.ts +33 -33
  39. package/src/rulebuilder/multi-select-item.ts +73 -73
  40. package/src/rulebuilder/page-action-manager.ts +31 -31
  41. package/src/rulebuilder/rule2/Rule2.ts +211 -211
  42. package/src/rulebuilder/tag-action-manager.spec.ts +44 -44
  43. package/src/rulebuilder/tag-action-manager.ts +28 -28
  44. package/src/schema-config.ts +25 -25
  45. package/src/theme/AbstractThemeCompiler.ts +7 -7
  46. package/src/theme/IDefaultTheme.ts +226 -226
  47. package/src/theme/css-theme.ts +7 -7
  48. package/src/theme/default-theme-compiler.ts +358 -358
  49. package/src/theme/icon-urls.ts +29 -29
  50. package/src/theme/theme-utils.ts +57 -57
  51. package/src/theme/theme1.spec.ts +52 -52
  52. package/src/variable/mq-variable.spec.ts +91 -0
  53. package/src/{mq-variable.ts → variable/mq-variable.ts} +63 -61
  54. package/src/variable/sum-score-variable.ts +56 -0
  55. package/tsconfig.json +15 -15
  56. package/tsconfig.tsbuildinfo +1 -1
@@ -1,306 +1,357 @@
1
- import type { BuilderSchemaDto } from "./Builder-schema";
2
- import { BuilderSchema } from "./Builder-schema";
3
- import { BuilderPage } from "./Builder-page";
4
- import type { BuilderTagDto } from "./BuilderTag";
5
- import { BuilderTag } from "./BuilderTag";
6
- import type { BuilderObjectId } from "./BuilderObject";
7
- import { PageID, SchemaID } from "@media-quest/engine";
8
- import { PagePrefix } from "./primitives/page-prefix";
9
- import { SchemaPrefix } from "./primitives/schema-prefix";
10
-
11
- const tag1: BuilderTagDto = BuilderTag.create("tag1", "This tag is defined in schemaDto1").toJson();
12
-
13
- const tag2: BuilderTagDto = BuilderTag.create("tag2", "This tag is defined in schemaDto1").toJson();
14
- const tag3: BuilderTagDto = BuilderTag.create("tag3", "This tag is defined in schemaDto1").toJson();
15
-
16
- const schemaDto1: BuilderSchemaDto = {
17
- backgroundColor: "gray",
18
- baseHeight: 1200,
19
- baseWidth: 1022,
20
- mainImage: false,
21
- id: SchemaID.create(),
22
- name: "dto1-name",
23
- tags: [tag1, tag2, tag3],
24
- pages: [
25
- {
26
- id: PageID.ensure("a".repeat(24)),
27
- _type: "info-page",
28
- prefix: PagePrefix.fromStringOrThrow("p1"),
29
- mainText: {
30
- text: "hello from test",
31
- autoplay: false,
32
- autoplayDelay: 0,
33
- audioFile: false,
34
- },
35
-
36
- nextButton: {
37
- id: "next-button-text-id" as BuilderObjectId.QuestionOptionID,
38
- label: "Neste",
39
- value: -1,
40
- },
41
- defaultQuestion: {
42
- id: "q1" as BuilderObjectId.QuestionID,
43
- prefix: "one-prefix",
44
- _type: "select-one",
45
- text: "q1-text",
46
- options: [
47
- {
48
- id: "opt-nei" as BuilderObjectId.QuestionOptionID,
49
- value: 0,
50
- label: "Nei",
51
- },
52
- {
53
- id: "opt-ja" as BuilderObjectId.QuestionOptionID,
54
- value: 1,
55
- label: "Ja",
56
- },
57
- {
58
- id: "opt-vet-ikke" as BuilderObjectId.QuestionOptionID,
59
- value: 9,
60
- label: "Vet ikke",
61
- },
62
- ],
63
- },
64
- tags: [
65
- tag1.tag,
66
- tag2.tag,
67
- // { tag: 'can_read', description: 'The patient can read' },
68
- // { tag: 'is grown up', description: 'Is grownUp.' }
69
- ],
70
- questions: [],
71
- autoplaySequence: [],
72
- },
73
- {
74
- id: PageID.ensure("b".repeat(24)),
75
- _type: "multi-select",
76
- prefix: PagePrefix.fromStringOrThrow("page2-prefix"),
77
- tags: [tag3.tag],
78
- mainText: {
79
- text: "hello from test",
80
- autoplay: false,
81
- autoplayDelay: 0,
82
- audioFile: false,
83
- },
84
- nextButton: {
85
- id: "next-button-id-page2" as BuilderObjectId.QuestionOptionID,
86
- label: "Neste",
87
- value: -1,
88
- },
89
- defaultQuestion: {
90
- id: "default-q" as BuilderObjectId.QuestionID,
91
- prefix: "one-prefix",
92
- _type: "select-one",
93
- text: "q1",
94
- options: [],
95
- },
96
- questions: [
97
- {
98
- id: "q1" as BuilderObjectId.QuestionID,
99
- prefix: "one-prefix",
100
- _type: "select-one",
101
- text: "q1-text",
102
- options: [
103
- {
104
- id: "opt-nei" as BuilderObjectId.QuestionOptionID,
105
- value: 0,
106
- label: "Nei",
107
- },
108
- {
109
- id: "opt-ja" as BuilderObjectId.QuestionOptionID,
110
- value: 1,
111
- label: "Ja",
112
- },
113
- {
114
- id: "opt-vet-ikke" as BuilderObjectId.QuestionOptionID,
115
- value: 9,
116
- label: "Vet ikke",
117
- },
118
- ],
119
- },
120
- ],
121
- autoplaySequence: [],
122
- },
123
- ],
124
- predefinedVariables: [],
125
- rules: [],
126
- prefix: SchemaPrefix.fromValueOrThrow("d1").value,
127
- };
128
-
129
- const SCHEMA_ID = SchemaID.create();
130
- const schemaPrefixA = SchemaPrefix.castOrCreateRandom("a").value;
131
- let builderSchema = BuilderSchema.create(SCHEMA_ID, "test", schemaPrefixA);
132
-
133
- beforeEach(() => {
134
- builderSchema = BuilderSchema.create(SCHEMA_ID, "test-name", schemaPrefixA);
135
- });
136
-
137
- describe("Builder schema", () => {
138
- test("Can add pages.", () => {
139
- builderSchema.addPage("question");
140
- builderSchema.addPage("info-page");
141
- expect(builderSchema.pages.length).toBe(2);
142
- const dto = builderSchema.compile().schema;
143
- expect(dto.pages.length).toBe(2);
144
- expect(dto.id).toBe(builderSchema.id);
145
- expect(dto.baseHeight).toBe(builderSchema.baseHeight);
146
- expect(dto.baseWidth).toBe(builderSchema.baseWidth);
147
- expect(dto.backgroundColor).toBe(builderSchema.backgroundColor);
148
- });
149
-
150
- test("Can delete page by passing reference", () => {
151
- const p1 = builderSchema.addPage("question");
152
- const p2 = builderSchema.addPage("question");
153
- const p3 = builderSchema.addPage("info-page");
154
- expect(builderSchema.pages.length).toBe(3);
155
- const result = builderSchema.deletePage(p1);
156
- expect(builderSchema.pages.length).toBe(2);
157
- expect(result).toBe(true);
158
- const result2 = builderSchema.deletePage(p2);
159
- expect(result2).toBe(true);
160
- expect(builderSchema.pages.length).toBe(1);
161
- expect(builderSchema.pages[0]).toBe(p3);
162
- const result3 = builderSchema.deletePage(p2);
163
- expect(result3).toBe(false);
164
- const result4 = builderSchema.deletePage(p3);
165
- expect(result4).toBe(true);
166
- });
167
-
168
- test("Can create Schema from dto", () => {
169
- const s = BuilderSchema.fromJson(schemaDto1);
170
- expect(s.id).toBe(schemaDto1.id);
171
- expect(s.prefix.value).toBe(schemaDto1.prefix);
172
- expect(s.baseHeight).toBe(schemaDto1.baseHeight);
173
- expect(s.baseWidth).toBe(schemaDto1.baseWidth);
174
- expect(s.backgroundColor).toBe(schemaDto1.backgroundColor);
175
- expect(s.pages.length).toBe(schemaDto1.pages.length);
176
- const p1Dto = schemaDto1.pages[0];
177
- expect(s.pages[0].id).toBe(p1Dto.id);
178
- const defaultQuestion = s.pages[0].defaultQuestion;
179
- expect(defaultQuestion).toBeDefined();
180
- expect(defaultQuestion.id).toBe(p1Dto.defaultQuestion.id);
181
- expect(defaultQuestion.options.length).toBe(p1Dto.defaultQuestion.options.length);
182
- const options = defaultQuestion.options;
183
- const nei = options[0];
184
- const ja = options[1];
185
- const vetIkke = options[2];
186
- expect(nei.label).toBe("Nei");
187
- expect(ja.label).toBe("Ja");
188
- expect(vetIkke.label).toBe("Vet ikke");
189
- expect(vetIkke.id).toBe(p1Dto.defaultQuestion.options[2].id);
190
- });
191
-
192
- test("fromJson === toJson", () => {
193
- const s = BuilderSchema.fromJson(schemaDto1);
194
- const json = s.toJson();
195
- expect(schemaDto1).toStrictEqual(json);
196
- });
197
- test("Can add page at concrete index", () => {
198
- const p1 = builderSchema.addPage("form");
199
- expect(p1.pageType).toBe("form");
200
- const p2 = builderSchema.addPage("multi-select", 0);
201
- expect(p2.pageType).toBe("multi-select");
202
- const pages = builderSchema.pages;
203
- expect(pages[0].id).toBe(p2.id);
204
- builderSchema.addPage("form");
205
- builderSchema.addPage("form");
206
- const last = builderSchema.addPage("form");
207
- expect(builderSchema.pages[builderSchema.pages.length - 1]).toBe(last);
208
- const p3 = builderSchema.addPage("info-page", 4);
209
- expect(pages[4].id).toBe(p3.id);
210
- });
211
- test("Can move page up and down", () => {
212
- const p1 = builderSchema.addPage("form");
213
- const p2 = builderSchema.addPage("form");
214
- const p3 = builderSchema.addPage("form");
215
- const p4 = builderSchema.addPage("multi-select");
216
- const last = builderSchema.addPage("form");
217
- const pages = builderSchema.pages;
218
- builderSchema.movePage(p2, 0);
219
- expect(pages[0]).toBe(p2);
220
- expect(pages[1]).toBe(p1);
221
- expect(builderSchema.movePage(p4, 4)).toBeTruthy();
222
- expect(builderSchema.movePage(p4, 5)).toBeFalsy();
223
- expect(builderSchema.movePage(p4, 10)).toBeFalsy();
224
-
225
- // expect(pages[0].id).toBe(p4.id);
226
- });
227
- test("Can clone a page and insert at index", () => {
228
- const p1 = builderSchema.addPage("form");
229
- const p2 = builderSchema.addPage("form");
230
- const p3 = builderSchema.addPage("form");
231
- const mainTextContent = "Hello from test";
232
- const p4 = builderSchema.addPage("multi-select");
233
- p3.mainText.text = mainTextContent;
234
- const p1Clone = BuilderPage.fromJson(p1.clone());
235
- const beforeLen = builderSchema.pages.length;
236
- builderSchema.insertPage(p1Clone, 2);
237
- const pages = builderSchema.pages;
238
- expect(beforeLen + 1).toBe(pages.length);
239
- expect(pages[2]).toBe(p1Clone);
240
- });
241
- test("Will not insert a page that is already in array.", () => {
242
- const p1 = builderSchema.addPage("form");
243
- const p2 = builderSchema.addPage("form");
244
- const p3 = builderSchema.addPage("form");
245
- const beforeLen = builderSchema.pages.length;
246
- const result = builderSchema.insertPage(p1, 0);
247
- const pages = builderSchema.pages;
248
- expect(beforeLen).toBe(pages.length);
249
- expect(result).toBe(false);
250
- });
251
-
252
- test("Can generate a info-page", () => {
253
- const p1 = builderSchema.addPage("info-page");
254
- const p2 = builderSchema.addPage("question");
255
- p2.defaultQuestion.addOption("Ja", 1, 0);
256
- p2.defaultQuestion.addOption("Nei", 0);
257
- p1.nextButton.label = "Neste";
258
- builderSchema.backgroundColor = "red";
259
- builderSchema.baseHeight = 600;
260
- builderSchema.baseWidth = 500;
261
- // builderSchema.prefix = "as";
262
- builderSchema.name = "depressed";
263
- const schema = builderSchema.compile().schema;
264
- expect(schema.backgroundColor).toBe("red");
265
- expect(schema.baseHeight).toBe(600);
266
- expect(schema.baseWidth).toBe(500);
267
- expect(schema.pages.length).toBe(2);
268
- const schemaP1 = schema.pages[0];
269
- const schemaP2 = schema.pages[1];
270
- expect(schemaP1.id).toBe(p1.id);
271
- expect(schemaP2.id).toBe(p2.id);
272
- // expect(schemaP1.elements.length).toBe(2);
273
-
274
- // Has Buttons
275
- const options = p2.defaultQuestion.options;
276
- // expect(schemaP2.elements.length).toBe(options.length + 1);
277
- });
278
- test("Can get ruleInput!", () => {
279
- const p0 = builderSchema.addPage("info-page");
280
- // p0.prefix = "info_page_prefix_";
281
- const p1 = builderSchema.addPage("question");
282
- // p1.prefix = "p1_prefix_";
283
- const p2 = builderSchema.addPage("question");
284
- // p2.prefix = "p2_prefix_";
285
- p1.defaultQuestion.addOption("Ja", 1, 0);
286
- p1.defaultQuestion.addOption("Nei", 0, 0);
287
- p2.defaultQuestion.addOption("Ja", 1, 0);
288
- p2.defaultQuestion.addOption("Nei", 0);
289
- const ruleInput = builderSchema.getRuleInput();
290
- expect(ruleInput.questionVars.length).toBe(2);
291
- expect(ruleInput.jumpToPageActions.length).toBe(3);
292
- // TODO add TAGS!!
293
- expect(ruleInput.excludeByTagActions.length).toBe(0);
294
- expect(ruleInput.excludeByPageIdActions.length).toBe(3);
295
- const allPageIds = new Set(ruleInput.jumpToPageActions.map((a) => a.pageId));
296
- expect(allPageIds.has(p0.id)).toBe(true);
297
- expect(allPageIds.has(p1.id)).toBe(true);
298
- expect(allPageIds.has(p2.id)).toBe(true);
299
- // ruleInput.questionVars.forEach((v) => {
300
- // expect(allPageIds.has(v.varId)).toBe(true);
301
- // });
302
- ruleInput.jumpToPageActions.forEach((v) => {
303
- expect(allPageIds.has(v.pageId)).toBe(true);
304
- });
305
- });
306
- });
1
+ import type { BuilderSchemaDto } from "./Builder-schema";
2
+ import { BuilderSchema } from "./Builder-schema";
3
+ import { BuilderPage } from "./Builder-page";
4
+ import type { BuilderTagDto } from "./BuilderTag";
5
+ import { BuilderTag } from "./BuilderTag";
6
+ import type { BuilderObjectId } from "./BuilderObject";
7
+ import { PageID, SchemaID } from "@media-quest/engine";
8
+ import { PagePrefix } from "./primitives/page-prefix";
9
+ import { SchemaPrefix } from "./primitives/schema-prefix";
10
+ import { SumScoreVariable } from "./variable/sum-score-variable";
11
+
12
+ const tag1: BuilderTagDto = BuilderTag.create("tag1", "This tag is defined in schemaDto1").toJson();
13
+
14
+ const tag2: BuilderTagDto = BuilderTag.create("tag2", "This tag is defined in schemaDto1").toJson();
15
+ const tag3: BuilderTagDto = BuilderTag.create("tag3", "This tag is defined in schemaDto1").toJson();
16
+
17
+ const schemaDto1: BuilderSchemaDto = {
18
+ backgroundColor: "gray",
19
+ baseHeight: 1200,
20
+ baseWidth: 1022,
21
+ mainImage: false,
22
+ id: SchemaID.create(),
23
+ name: "dto1-name",
24
+ tags: [tag1, tag2, tag3],
25
+ sumScoreVariables: [
26
+ {
27
+ kind: "numeric-variable",
28
+ origin: "sum-score",
29
+ label: "label for dummy variable",
30
+ initialValue: 0,
31
+ varId: "testId",
32
+ basedOn: [],
33
+ },
34
+ ],
35
+ pages: [
36
+ {
37
+ id: PageID.ensure("a".repeat(24)),
38
+ _type: "info-page",
39
+ prefix: PagePrefix.fromStringOrThrow("p1"),
40
+ mainText: {
41
+ text: "hello from test",
42
+ autoplay: false,
43
+ autoplayDelay: 0,
44
+ audioFile: false,
45
+ },
46
+
47
+ nextButton: {
48
+ id: "next-button-text-id" as BuilderObjectId.QuestionOptionID,
49
+ label: "Neste",
50
+ value: -1,
51
+ },
52
+ defaultQuestion: {
53
+ id: "q1" as BuilderObjectId.QuestionID,
54
+ prefix: "one-prefix",
55
+ _type: "select-one",
56
+ text: "q1-text",
57
+ options: [
58
+ {
59
+ id: "opt-nei" as BuilderObjectId.QuestionOptionID,
60
+ value: 0,
61
+ label: "Nei",
62
+ },
63
+ {
64
+ id: "opt-ja" as BuilderObjectId.QuestionOptionID,
65
+ value: 1,
66
+ label: "Ja",
67
+ },
68
+ {
69
+ id: "opt-vet-ikke" as BuilderObjectId.QuestionOptionID,
70
+ value: 9,
71
+ label: "Vet ikke",
72
+ },
73
+ ],
74
+ },
75
+ tags: [
76
+ tag1.tag,
77
+ tag2.tag,
78
+ // { tag: 'can_read', description: 'The patient can read' },
79
+ // { tag: 'is grown up', description: 'Is grownUp.' }
80
+ ],
81
+ questions: [],
82
+ autoplaySequence: [],
83
+ },
84
+ {
85
+ id: PageID.ensure("b".repeat(24)),
86
+ _type: "multi-select",
87
+ prefix: PagePrefix.fromStringOrThrow("page2-prefix"),
88
+ tags: [tag3.tag],
89
+ mainText: {
90
+ text: "hello from test",
91
+ autoplay: false,
92
+ autoplayDelay: 0,
93
+ audioFile: false,
94
+ },
95
+ nextButton: {
96
+ id: "next-button-id-page2" as BuilderObjectId.QuestionOptionID,
97
+ label: "Neste",
98
+ value: -1,
99
+ },
100
+ defaultQuestion: {
101
+ id: "default-q" as BuilderObjectId.QuestionID,
102
+ prefix: "one-prefix",
103
+ _type: "select-one",
104
+ text: "q1",
105
+ options: [],
106
+ },
107
+ questions: [
108
+ {
109
+ id: "q1" as BuilderObjectId.QuestionID,
110
+ prefix: "one-prefix",
111
+ _type: "select-one",
112
+ text: "q1-text",
113
+ options: [
114
+ {
115
+ id: "opt-nei" as BuilderObjectId.QuestionOptionID,
116
+ value: 0,
117
+ label: "Nei",
118
+ },
119
+ {
120
+ id: "opt-ja" as BuilderObjectId.QuestionOptionID,
121
+ value: 1,
122
+ label: "Ja",
123
+ },
124
+ {
125
+ id: "opt-vet-ikke" as BuilderObjectId.QuestionOptionID,
126
+ value: 9,
127
+ label: "Vet ikke",
128
+ },
129
+ ],
130
+ },
131
+ ],
132
+ autoplaySequence: [],
133
+ },
134
+ ],
135
+ predefinedVariables: [],
136
+ rules: [],
137
+ prefix: SchemaPrefix.fromValueOrThrow("d1").value,
138
+ };
139
+
140
+ const SCHEMA_ID = SchemaID.create();
141
+ const schemaPrefixA = SchemaPrefix.castOrCreateRandom("a").value;
142
+ let builderSchema = BuilderSchema.create(SCHEMA_ID, "test", schemaPrefixA);
143
+
144
+ beforeEach(() => {
145
+ builderSchema = BuilderSchema.create(SCHEMA_ID, "test-name", schemaPrefixA);
146
+ });
147
+
148
+ describe("Builder schema", () => {
149
+ test("Can add pages.", () => {
150
+ builderSchema.addPage("question");
151
+ builderSchema.addPage("info-page");
152
+ expect(builderSchema.pages.length).toBe(2);
153
+ const dto = builderSchema.compile().schema;
154
+ expect(dto.pages.length).toBe(2);
155
+ expect(dto.id).toBe(builderSchema.id);
156
+ expect(dto.baseHeight).toBe(builderSchema.baseHeight);
157
+ expect(dto.baseWidth).toBe(builderSchema.baseWidth);
158
+ expect(dto.backgroundColor).toBe(builderSchema.backgroundColor);
159
+ });
160
+
161
+ test("Can delete page by passing reference", () => {
162
+ const p1 = builderSchema.addPage("question");
163
+ const p2 = builderSchema.addPage("question");
164
+ const p3 = builderSchema.addPage("info-page");
165
+ expect(builderSchema.pages.length).toBe(3);
166
+ const result = builderSchema.deletePage(p1);
167
+ expect(builderSchema.pages.length).toBe(2);
168
+ expect(result).toBe(true);
169
+ const result2 = builderSchema.deletePage(p2);
170
+ expect(result2).toBe(true);
171
+ expect(builderSchema.pages.length).toBe(1);
172
+ expect(builderSchema.pages[0]).toBe(p3);
173
+ const result3 = builderSchema.deletePage(p2);
174
+ expect(result3).toBe(false);
175
+ const result4 = builderSchema.deletePage(p3);
176
+ expect(result4).toBe(true);
177
+ });
178
+
179
+ test("Can create Schema from dto", () => {
180
+ const s = BuilderSchema.fromJson(schemaDto1);
181
+ expect(s.id).toBe(schemaDto1.id);
182
+ expect(s.prefix.value).toBe(schemaDto1.prefix);
183
+ expect(s.baseHeight).toBe(schemaDto1.baseHeight);
184
+ expect(s.baseWidth).toBe(schemaDto1.baseWidth);
185
+ expect(s.backgroundColor).toBe(schemaDto1.backgroundColor);
186
+ expect(s.pages.length).toBe(schemaDto1.pages.length);
187
+ const p1Dto = schemaDto1.pages[0];
188
+ expect(s.pages[0].id).toBe(p1Dto.id);
189
+ const defaultQuestion = s.pages[0].defaultQuestion;
190
+ expect(defaultQuestion).toBeDefined();
191
+ expect(defaultQuestion.id).toBe(p1Dto.defaultQuestion.id);
192
+ expect(defaultQuestion.options.length).toBe(p1Dto.defaultQuestion.options.length);
193
+ const options = defaultQuestion.options;
194
+ const nei = options[0];
195
+ const ja = options[1];
196
+ const vetIkke = options[2];
197
+ expect(nei.label).toBe("Nei");
198
+ expect(ja.label).toBe("Ja");
199
+ expect(vetIkke.label).toBe("Vet ikke");
200
+ expect(vetIkke.id).toBe(p1Dto.defaultQuestion.options[2].id);
201
+ });
202
+
203
+ test("fromJson === toJson", () => {
204
+ const s = BuilderSchema.fromJson(schemaDto1);
205
+ const json = s.toJson();
206
+ expect(schemaDto1).toStrictEqual(json);
207
+ });
208
+ test("Can add page at concrete index", () => {
209
+ const p1 = builderSchema.addPage("form");
210
+ expect(p1.pageType).toBe("form");
211
+ const p2 = builderSchema.addPage("multi-select", 0);
212
+ expect(p2.pageType).toBe("multi-select");
213
+ const pages = builderSchema.pages;
214
+ expect(pages[0].id).toBe(p2.id);
215
+ builderSchema.addPage("form");
216
+ builderSchema.addPage("form");
217
+ const last = builderSchema.addPage("form");
218
+ expect(builderSchema.pages[builderSchema.pages.length - 1]).toBe(last);
219
+ const p3 = builderSchema.addPage("info-page", 4);
220
+ expect(pages[4].id).toBe(p3.id);
221
+ });
222
+ test("Can move page up and down", () => {
223
+ const p1 = builderSchema.addPage("form");
224
+ const p2 = builderSchema.addPage("form");
225
+ const p3 = builderSchema.addPage("form");
226
+ const p4 = builderSchema.addPage("multi-select");
227
+ const last = builderSchema.addPage("form");
228
+ const pages = builderSchema.pages;
229
+ builderSchema.movePage(p2, 0);
230
+ expect(pages[0]).toBe(p2);
231
+ expect(pages[1]).toBe(p1);
232
+ expect(builderSchema.movePage(p4, 4)).toBeTruthy();
233
+ expect(builderSchema.movePage(p4, 5)).toBeFalsy();
234
+ expect(builderSchema.movePage(p4, 10)).toBeFalsy();
235
+
236
+ // expect(pages[0].id).toBe(p4.id);
237
+ });
238
+ test("Can clone a page and insert at index", () => {
239
+ const p1 = builderSchema.addPage("form");
240
+ const p2 = builderSchema.addPage("form");
241
+ const p3 = builderSchema.addPage("form");
242
+ const mainTextContent = "Hello from test";
243
+ const p4 = builderSchema.addPage("multi-select");
244
+ p3.mainText.text = mainTextContent;
245
+ const p1Clone = BuilderPage.fromJson(p1.clone());
246
+ const beforeLen = builderSchema.pages.length;
247
+ builderSchema.insertPage(p1Clone, 2);
248
+ const pages = builderSchema.pages;
249
+ expect(beforeLen + 1).toBe(pages.length);
250
+ expect(pages[2]).toBe(p1Clone);
251
+ });
252
+ test("Will not insert a page that is already in array.", () => {
253
+ const p1 = builderSchema.addPage("form");
254
+ const p2 = builderSchema.addPage("form");
255
+ const p3 = builderSchema.addPage("form");
256
+ const beforeLen = builderSchema.pages.length;
257
+ const result = builderSchema.insertPage(p1, 0);
258
+ const pages = builderSchema.pages;
259
+ expect(beforeLen).toBe(pages.length);
260
+ expect(result).toBe(false);
261
+ });
262
+
263
+ test("Can generate a info-page", () => {
264
+ const p1 = builderSchema.addPage("info-page");
265
+ const p2 = builderSchema.addPage("question");
266
+ p2.defaultQuestion.addOption("Ja", 1, 0);
267
+ p2.defaultQuestion.addOption("Nei", 0);
268
+ p1.nextButton.label = "Neste";
269
+ builderSchema.backgroundColor = "red";
270
+ builderSchema.baseHeight = 600;
271
+ builderSchema.baseWidth = 500;
272
+ // builderSchema.prefix = "as";
273
+ builderSchema.name = "depressed";
274
+ const schema = builderSchema.compile().schema;
275
+ expect(schema.backgroundColor).toBe("red");
276
+ expect(schema.baseHeight).toBe(600);
277
+ expect(schema.baseWidth).toBe(500);
278
+ expect(schema.pages.length).toBe(2);
279
+ const schemaP1 = schema.pages[0];
280
+ const schemaP2 = schema.pages[1];
281
+ expect(schemaP1.id).toBe(p1.id);
282
+ expect(schemaP2.id).toBe(p2.id);
283
+ // expect(schemaP1.elements.length).toBe(2);
284
+
285
+ // Has Buttons
286
+ const options = p2.defaultQuestion.options;
287
+ // expect(schemaP2.elements.length).toBe(options.length + 1);
288
+ });
289
+ test("Can get ruleInput!", () => {
290
+ const p0 = builderSchema.addPage("info-page");
291
+ // p0.prefix = "info_page_prefix_";
292
+ const p1 = builderSchema.addPage("question");
293
+ // p1.prefix = "p1_prefix_";
294
+ const p2 = builderSchema.addPage("question");
295
+ // p2.prefix = "p2_prefix_";
296
+ p1.defaultQuestion.addOption("Ja", 1, 0);
297
+ p1.defaultQuestion.addOption("Nei", 0, 0);
298
+ p2.defaultQuestion.addOption("Ja", 1, 0);
299
+ p2.defaultQuestion.addOption("Nei", 0);
300
+ const ruleInput = builderSchema.getRuleInput();
301
+ expect(ruleInput.questionVars.length).toBe(2);
302
+ expect(ruleInput.jumpToPageActions.length).toBe(3);
303
+ // TODO add TAGS!!
304
+ expect(ruleInput.excludeByTagActions.length).toBe(0);
305
+ expect(ruleInput.excludeByPageIdActions.length).toBe(3);
306
+ const allPageIds = new Set(ruleInput.jumpToPageActions.map((a) => a.pageId));
307
+ expect(allPageIds.has(p0.id)).toBe(true);
308
+ expect(allPageIds.has(p1.id)).toBe(true);
309
+ expect(allPageIds.has(p2.id)).toBe(true);
310
+ // ruleInput.questionVars.forEach((v) => {
311
+ // expect(allPageIds.has(v.varId)).toBe(true);
312
+ // });
313
+ ruleInput.jumpToPageActions.forEach((v) => {
314
+ expect(allPageIds.has(v.pageId)).toBe(true);
315
+ });
316
+ });
317
+ test.skip("Can add sum-variables - and save as json", () => {
318
+ // p0.prefix = "info_page_prefix_";
319
+ const p1 = builderSchema.addPage("question");
320
+ const p2 = builderSchema.addPage("question");
321
+ p1.prefix = PagePrefix.fromStringOrThrow("p1_prefix");
322
+ p2.prefix = PagePrefix.fromStringOrThrow("p2_prefix");
323
+ p1.defaultQuestion.addOption("Ja", 1, 0);
324
+ p1.defaultQuestion.addOption("Nei", 0, 0);
325
+ p2.defaultQuestion.addOption("Ja", 1, 0);
326
+ p2.defaultQuestion.addOption("Nei", 0);
327
+
328
+ const ruleInput = builderSchema.getRuleInput();
329
+ const ss1: SumScoreVariable = {
330
+ label: "ss1",
331
+ varId: "ss1_var_id",
332
+ initialValue: 0,
333
+ kind: "numeric-variable",
334
+ origin: "sum-score",
335
+ basedOn: [],
336
+ };
337
+
338
+ builderSchema.addSumScoreVariable(ss1);
339
+
340
+ expect(ruleInput.questionVars.length).toBe(2);
341
+ expect(ruleInput.jumpToPageActions.length).toBe(3);
342
+
343
+ // TODO add TAGS!!
344
+ // expect(ruleInput.excludeByTagActions.length).toBe(0);
345
+ // expect(ruleInput.excludeByPageIdActions.length).toBe(3);
346
+ // const allPageIds = new Set(ruleInput.jumpToPageActions.map((a) => a.pageId));
347
+ // expect(allPageIds.has(p0.id)).toBe(true);
348
+ // expect(allPageIds.has(p1.id)).toBe(true);
349
+ // expect(allPageIds.has(p2.id)).toBe(true);
350
+ // ruleInput.questionVars.forEach((v) => {
351
+ // expect(allPageIds.has(v.varId)).toBe(true);
352
+ // });
353
+ // ruleInput.jumpToPageActions.forEach((v) => {
354
+ // expect(allPageIds.has(v.pageId)).toBe(true);
355
+ // });
356
+ });
357
+ });