@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,257 +1,257 @@
1
- import type { BuilderQuestionDto, BuilderQuestionType } from "./Builder-question";
2
- import { BuilderQuestion } from "./Builder-question";
3
- import { BuilderObject } from "./BuilderObject";
4
- import type { BuilderOptionDto } from "./Builder-option";
5
- import { BuilderOption } from "./Builder-option";
6
- import type { BuilderMainVideoDto } from "./BuilderMainVideoDto";
7
- import type { BuilderMainImageDto } from "./BuilderMainImageDto";
8
- import type { BuilderMainTextDto } from "./BuilderMainText";
9
- import { BuilderMainText } from "./BuilderMainText";
10
- import { BuilderVariableOption, QuestionVariable } from "./rulebuilder/RuleVariable";
11
- import { DUtil, PageID } from "@media-quest/engine";
12
- import { PagePrefix, PagePrefixValue } from "./primitives/page-prefix";
13
- import { VarID } from "./primitives/varID";
14
- import { SchemaPrefix } from "./primitives/schema-prefix";
15
-
16
- const U = DUtil;
17
- export type BuilderPageType = "info-page" | "question" | "multi-select" | "form";
18
-
19
- export interface BuilderPageDto {
20
- readonly id: PageID;
21
- readonly prefix: PagePrefixValue;
22
- _type: BuilderPageType;
23
- mainText: BuilderMainTextDto;
24
- nextButton: BuilderOptionDto;
25
- defaultQuestion: BuilderQuestionDto;
26
- questions: Array<BuilderQuestionDto>;
27
- mainMedia?: BuilderMainImageDto | BuilderMainVideoDto;
28
- autoplaySequence: Array<string>;
29
- tags: ReadonlyArray<string>;
30
- }
31
-
32
- export class BuilderPage extends BuilderObject<"builder-page", BuilderPageDto> {
33
- readonly objectType: "builder-page" = "builder-page";
34
- readonly id: PageID;
35
- private _pageType: BuilderPageType;
36
- private _prefix: PagePrefix;
37
- private _questions: Array<BuilderQuestion> = [];
38
- private readonly _tags: Set<string>;
39
- private _backgroundColor = "#FFFFFF";
40
- mainMedia: BuilderMainVideoDto | BuilderMainImageDto | false = false;
41
- defaultQuestion: BuilderQuestion;
42
- mainText: BuilderMainText;
43
- nextButton = BuilderOption.create(-1, "Neste");
44
-
45
- public static create(type: BuilderPageType, _prefix: PagePrefixValue) {
46
- const id = PageID.create();
47
- const mainTextDto: BuilderMainTextDto = {
48
- text: "",
49
- audioFile: false,
50
- autoplay: false,
51
- autoplayDelay: 0,
52
- };
53
- const nextButtonDto = BuilderOption.create(-1, "page-next-button-text").toJson();
54
- const defaultQuestionDto = BuilderQuestion.create("select-one").toJson();
55
-
56
- const dto: BuilderPageDto = {
57
- _type: type,
58
- autoplaySequence: [],
59
- defaultQuestion: defaultQuestionDto,
60
- id,
61
- nextButton: nextButtonDto,
62
- mainText: mainTextDto,
63
- prefix: _prefix,
64
- questions: [],
65
- tags: [],
66
- };
67
-
68
- const page = new BuilderPage(dto);
69
- return page;
70
- }
71
- public static fromJson(dto: BuilderPageDto): BuilderPage {
72
- const page = new BuilderPage(dto);
73
- return page;
74
- }
75
-
76
- private constructor(dto: BuilderPageDto) {
77
- super(dto);
78
- this.id = dto.id;
79
- this._pageType = dto._type;
80
- const prefixInstance = PagePrefix.castOrCreateRandom(dto.prefix);
81
- this._prefix = prefixInstance;
82
- this.mainText = BuilderMainText.fromJson(dto.mainText);
83
- this.nextButton = BuilderOption.fromJson(dto.nextButton);
84
- this.defaultQuestion = BuilderQuestion.fromJson(dto.defaultQuestion);
85
- this._questions = dto.questions.map((q) => BuilderQuestion.fromJson(q));
86
- const tagList: string[] = Array.isArray(dto.tags) ? dto.tags : [];
87
- this._tags = new Set(tagList);
88
- if (dto.mainMedia) {
89
- this.mainMedia = dto.mainMedia;
90
- }
91
- this.updateRows();
92
- }
93
-
94
- insertQuestion(question: BuilderQuestion, atIndex: number): boolean {
95
- const validIndexFn = U.isInRange(0, this._questions.length);
96
- if (!validIndexFn(atIndex)) {
97
- return false;
98
- }
99
- const hasQuestion = !!this._questions.find((q) => q.id === question.id);
100
- if (hasQuestion) {
101
- return false;
102
- }
103
- this._questions.splice(atIndex, 0, question);
104
- return true;
105
- }
106
-
107
- addQuestion(type: BuilderQuestionType, atIndex = -1): BuilderQuestion {
108
- const question = BuilderQuestion.create(type);
109
- if (atIndex < this._questions.length && atIndex >= 0) {
110
- this._questions.splice(atIndex, 0, question);
111
- } else {
112
- this._questions.push(question);
113
- }
114
- return question;
115
- }
116
-
117
- /**
118
- * Move a question in questions-array
119
- * @param question (reference)
120
- * @param toIndex
121
- */
122
- moveQuestion(question: BuilderQuestion, toIndex: number): boolean {
123
- const validToIndexFn = U.isInRange(0, this._questions.length);
124
- if (!validToIndexFn(toIndex)) {
125
- return false;
126
- }
127
- const currentIndex = this._questions.indexOf(question);
128
- if (currentIndex < 0) {
129
- return false;
130
- }
131
- this._questions.splice(currentIndex, 1);
132
- this._questions.splice(toIndex, 0, question);
133
- return true;
134
- }
135
-
136
- deleteQuestion(question: BuilderQuestion) {
137
- this._questions = this._questions.filter((q) => q !== question);
138
- // TODO EMIT DELETED QUESTION.
139
- this.updateRows();
140
- }
141
- private updateRows() {
142
- if (this._pageType === "question" || this._pageType === "info-page") {
143
- this._questions = [];
144
- }
145
- if (this._pageType === "form" && this._questions.length === 0) {
146
- this._questions = [];
147
- this.addQuestion("text");
148
- }
149
- if (this._pageType === "multi-select" && this._questions.length === 0) {
150
- this._questions = [];
151
- this.addQuestion("text");
152
- }
153
- }
154
-
155
- public addTag(tag: string) {
156
- this._tags.add(tag);
157
- }
158
- public deleteTag(tag: string) {
159
- this._tags.delete(tag);
160
- }
161
-
162
- set pageType(value: BuilderPageType) {
163
- this._pageType = value;
164
- // TODO Emit All questions that are deleted? Listen for removed variables??
165
- this.updateRows();
166
- }
167
-
168
- getQuestionVariables(
169
- modulePrefix: SchemaPrefix,
170
- pageNumber: number,
171
- ): ReadonlyArray<QuestionVariable> {
172
- const variables: QuestionVariable[] = [];
173
-
174
- if (this._pageType === "question") {
175
- const pagePrefix = this.prefix;
176
- const varId = VarID.create(modulePrefix.value, pagePrefix);
177
- const label = this.mainText.text;
178
- const op = this.defaultQuestion.options.map((o) => {
179
- const label = o.label;
180
- const value = o.value;
181
- return new BuilderVariableOption(label, value);
182
- });
183
- const singleVar = new QuestionVariable(varId, label, op, pageNumber);
184
- variables.push(singleVar);
185
- }
186
- return variables;
187
- }
188
-
189
- get tags(): ReadonlyArray<string> {
190
- return [...this._tags];
191
- }
192
- get pageType() {
193
- return this._pageType;
194
- }
195
-
196
- get prefix() {
197
- return this._prefix.value;
198
- }
199
-
200
- set prefix(value: PagePrefixValue) {
201
- this._prefix.value = value;
202
- }
203
-
204
- toJson(): BuilderPageDto {
205
- const questions = this._questions.map((q) => q.toJson());
206
- const mainText = this.mainText.toJson();
207
- const nextButton = this.nextButton.toJson();
208
- const mainMedia = this.mainMedia;
209
- const dto: BuilderPageDto = {
210
- _type: this.pageType,
211
- mainText,
212
- autoplaySequence: [],
213
- nextButton,
214
- id: this.id,
215
- tags: [...this.tags],
216
- prefix: this._prefix.value,
217
- defaultQuestion: this.defaultQuestion.toJson(),
218
- questions,
219
- };
220
- if (mainMedia) {
221
- dto.mainMedia = mainMedia;
222
- }
223
- return dto;
224
- }
225
-
226
- clone(): BuilderPageDto {
227
- const dto = this.toJson();
228
- const defaultQuestionClone = this.defaultQuestion.clone();
229
- const mainTextClone = JSON.parse(JSON.stringify(this.mainText));
230
- // const pagesClone = this
231
- const questionsClone = this.questions.map((q) => q.clone());
232
- const newId = PageID.create();
233
- const clone: BuilderPageDto = {
234
- ...dto,
235
- id: newId,
236
- defaultQuestion: defaultQuestionClone,
237
- mainText: mainTextClone,
238
- questions: questionsClone,
239
- };
240
- // const cloneDto
241
- return clone;
242
- }
243
-
244
- get backgroundColor() {
245
- return this._backgroundColor;
246
- }
247
-
248
- set backgroundColor(color: string) {
249
- if (typeof color === "string") {
250
- this._backgroundColor = color;
251
- }
252
- }
253
-
254
- get questions(): ReadonlyArray<BuilderQuestion> {
255
- return this._questions;
256
- }
257
- }
1
+ import type { BuilderQuestionDto, BuilderQuestionType } from "./Builder-question";
2
+ import { BuilderQuestion } from "./Builder-question";
3
+ import { BuilderObject } from "./BuilderObject";
4
+ import type { BuilderOptionDto } from "./Builder-option";
5
+ import { BuilderOption } from "./Builder-option";
6
+ import type { BuilderMainVideoDto } from "./BuilderMainVideoDto";
7
+ import type { BuilderMainImageDto } from "./BuilderMainImageDto";
8
+ import type { BuilderMainTextDto } from "./BuilderMainText";
9
+ import { BuilderMainText } from "./BuilderMainText";
10
+ import { BuilderVariableOption, QuestionVariable } from "./rulebuilder/RuleVariable";
11
+ import { DUtil, PageID } from "@media-quest/engine";
12
+ import { PagePrefix, PagePrefixValue } from "./primitives/page-prefix";
13
+ import { VarID } from "./primitives/varID";
14
+ import { SchemaPrefix } from "./primitives/schema-prefix";
15
+
16
+ const U = DUtil;
17
+ export type BuilderPageType = "info-page" | "question" | "multi-select" | "form";
18
+
19
+ export interface BuilderPageDto {
20
+ readonly id: PageID;
21
+ readonly prefix: PagePrefixValue;
22
+ _type: BuilderPageType;
23
+ mainText: BuilderMainTextDto;
24
+ nextButton: BuilderOptionDto;
25
+ defaultQuestion: BuilderQuestionDto;
26
+ questions: Array<BuilderQuestionDto>;
27
+ mainMedia?: BuilderMainImageDto | BuilderMainVideoDto;
28
+ autoplaySequence: Array<string>;
29
+ tags: ReadonlyArray<string>;
30
+ }
31
+
32
+ export class BuilderPage extends BuilderObject<"builder-page", BuilderPageDto> {
33
+ readonly objectType: "builder-page" = "builder-page";
34
+ readonly id: PageID;
35
+ private _pageType: BuilderPageType;
36
+ private _prefix: PagePrefix;
37
+ private _questions: Array<BuilderQuestion> = [];
38
+ private readonly _tags: Set<string>;
39
+ private _backgroundColor = "#FFFFFF";
40
+ mainMedia: BuilderMainVideoDto | BuilderMainImageDto | false = false;
41
+ defaultQuestion: BuilderQuestion;
42
+ mainText: BuilderMainText;
43
+ nextButton = BuilderOption.create(-1, "Neste");
44
+
45
+ public static create(type: BuilderPageType, _prefix: PagePrefixValue) {
46
+ const id = PageID.create();
47
+ const mainTextDto: BuilderMainTextDto = {
48
+ text: "",
49
+ audioFile: false,
50
+ autoplay: false,
51
+ autoplayDelay: 0,
52
+ };
53
+ const nextButtonDto = BuilderOption.create(-1, "page-next-button-text").toJson();
54
+ const defaultQuestionDto = BuilderQuestion.create("select-one").toJson();
55
+
56
+ const dto: BuilderPageDto = {
57
+ _type: type,
58
+ autoplaySequence: [],
59
+ defaultQuestion: defaultQuestionDto,
60
+ id,
61
+ nextButton: nextButtonDto,
62
+ mainText: mainTextDto,
63
+ prefix: _prefix,
64
+ questions: [],
65
+ tags: [],
66
+ };
67
+
68
+ const page = new BuilderPage(dto);
69
+ return page;
70
+ }
71
+ public static fromJson(dto: BuilderPageDto): BuilderPage {
72
+ const page = new BuilderPage(dto);
73
+ return page;
74
+ }
75
+
76
+ private constructor(dto: BuilderPageDto) {
77
+ super(dto);
78
+ this.id = dto.id;
79
+ this._pageType = dto._type;
80
+ const prefixInstance = PagePrefix.castOrCreateRandom(dto.prefix);
81
+ this._prefix = prefixInstance;
82
+ this.mainText = BuilderMainText.fromJson(dto.mainText);
83
+ this.nextButton = BuilderOption.fromJson(dto.nextButton);
84
+ this.defaultQuestion = BuilderQuestion.fromJson(dto.defaultQuestion);
85
+ this._questions = dto.questions.map((q) => BuilderQuestion.fromJson(q));
86
+ const tagList: string[] = Array.isArray(dto.tags) ? dto.tags : [];
87
+ this._tags = new Set(tagList);
88
+ if (dto.mainMedia) {
89
+ this.mainMedia = dto.mainMedia;
90
+ }
91
+ this.updateRows();
92
+ }
93
+
94
+ insertQuestion(question: BuilderQuestion, atIndex: number): boolean {
95
+ const validIndexFn = U.isInRange(0, this._questions.length);
96
+ if (!validIndexFn(atIndex)) {
97
+ return false;
98
+ }
99
+ const hasQuestion = !!this._questions.find((q) => q.id === question.id);
100
+ if (hasQuestion) {
101
+ return false;
102
+ }
103
+ this._questions.splice(atIndex, 0, question);
104
+ return true;
105
+ }
106
+
107
+ addQuestion(type: BuilderQuestionType, atIndex = -1): BuilderQuestion {
108
+ const question = BuilderQuestion.create(type);
109
+ if (atIndex < this._questions.length && atIndex >= 0) {
110
+ this._questions.splice(atIndex, 0, question);
111
+ } else {
112
+ this._questions.push(question);
113
+ }
114
+ return question;
115
+ }
116
+
117
+ /**
118
+ * Move a question in questions-array
119
+ * @param question (reference)
120
+ * @param toIndex
121
+ */
122
+ moveQuestion(question: BuilderQuestion, toIndex: number): boolean {
123
+ const validToIndexFn = U.isInRange(0, this._questions.length);
124
+ if (!validToIndexFn(toIndex)) {
125
+ return false;
126
+ }
127
+ const currentIndex = this._questions.indexOf(question);
128
+ if (currentIndex < 0) {
129
+ return false;
130
+ }
131
+ this._questions.splice(currentIndex, 1);
132
+ this._questions.splice(toIndex, 0, question);
133
+ return true;
134
+ }
135
+
136
+ deleteQuestion(question: BuilderQuestion) {
137
+ this._questions = this._questions.filter((q) => q !== question);
138
+ // TODO EMIT DELETED QUESTION.
139
+ this.updateRows();
140
+ }
141
+ private updateRows() {
142
+ if (this._pageType === "question" || this._pageType === "info-page") {
143
+ this._questions = [];
144
+ }
145
+ if (this._pageType === "form" && this._questions.length === 0) {
146
+ this._questions = [];
147
+ this.addQuestion("text");
148
+ }
149
+ if (this._pageType === "multi-select" && this._questions.length === 0) {
150
+ this._questions = [];
151
+ this.addQuestion("text");
152
+ }
153
+ }
154
+
155
+ public addTag(tag: string) {
156
+ this._tags.add(tag);
157
+ }
158
+ public deleteTag(tag: string) {
159
+ this._tags.delete(tag);
160
+ }
161
+
162
+ set pageType(value: BuilderPageType) {
163
+ this._pageType = value;
164
+ // TODO Emit All questions that are deleted? Listen for removed variables??
165
+ this.updateRows();
166
+ }
167
+
168
+ getQuestionVariables(
169
+ modulePrefix: SchemaPrefix,
170
+ pageNumber: number,
171
+ ): ReadonlyArray<QuestionVariable> {
172
+ const variables: QuestionVariable[] = [];
173
+
174
+ if (this._pageType === "question") {
175
+ const pagePrefix = this.prefix;
176
+ const varId = VarID.create(modulePrefix.value, pagePrefix);
177
+ const label = this.mainText.text;
178
+ const op = this.defaultQuestion.options.map((o) => {
179
+ const label = o.label;
180
+ const value = o.value;
181
+ return new BuilderVariableOption(label, value);
182
+ });
183
+ const singleVar = new QuestionVariable(varId, label, op, pageNumber);
184
+ variables.push(singleVar);
185
+ }
186
+ return variables;
187
+ }
188
+
189
+ get tags(): ReadonlyArray<string> {
190
+ return [...this._tags];
191
+ }
192
+ get pageType() {
193
+ return this._pageType;
194
+ }
195
+
196
+ get prefix() {
197
+ return this._prefix.value;
198
+ }
199
+
200
+ set prefix(value: PagePrefixValue) {
201
+ this._prefix.value = value;
202
+ }
203
+
204
+ toJson(): BuilderPageDto {
205
+ const questions = this._questions.map((q) => q.toJson());
206
+ const mainText = this.mainText.toJson();
207
+ const nextButton = this.nextButton.toJson();
208
+ const mainMedia = this.mainMedia;
209
+ const dto: BuilderPageDto = {
210
+ _type: this.pageType,
211
+ mainText,
212
+ autoplaySequence: [],
213
+ nextButton,
214
+ id: this.id,
215
+ tags: [...this.tags],
216
+ prefix: this._prefix.value,
217
+ defaultQuestion: this.defaultQuestion.toJson(),
218
+ questions,
219
+ };
220
+ if (mainMedia) {
221
+ dto.mainMedia = mainMedia;
222
+ }
223
+ return dto;
224
+ }
225
+
226
+ clone(): BuilderPageDto {
227
+ const dto = this.toJson();
228
+ const defaultQuestionClone = this.defaultQuestion.clone();
229
+ const mainTextClone = JSON.parse(JSON.stringify(this.mainText));
230
+ // const pagesClone = this
231
+ const questionsClone = this.questions.map((q) => q.clone());
232
+ const newId = PageID.create();
233
+ const clone: BuilderPageDto = {
234
+ ...dto,
235
+ id: newId,
236
+ defaultQuestion: defaultQuestionClone,
237
+ mainText: mainTextClone,
238
+ questions: questionsClone,
239
+ };
240
+ // const cloneDto
241
+ return clone;
242
+ }
243
+
244
+ get backgroundColor() {
245
+ return this._backgroundColor;
246
+ }
247
+
248
+ set backgroundColor(color: string) {
249
+ if (typeof color === "string") {
250
+ this._backgroundColor = color;
251
+ }
252
+ }
253
+
254
+ get questions(): ReadonlyArray<BuilderQuestion> {
255
+ return this._questions;
256
+ }
257
+ }
@@ -1,68 +1,68 @@
1
- import type { BuilderQuestionDto } from "./Builder-question";
2
- import { BuilderQuestion } from "./Builder-question";
3
- import type { BuilderObjectId } from "./BuilderObject";
4
-
5
- const question1: BuilderQuestionDto = {
6
- id: "q1" as BuilderObjectId.QuestionID,
7
- prefix: "q1",
8
- text: "sadf",
9
- options: [],
10
- _type: "select-one",
11
- };
12
-
13
- const question2: BuilderQuestionDto = {
14
- id: "q3" as BuilderObjectId.QuestionID,
15
- prefix: "a",
16
- text: "har du..",
17
- _type: "select-one",
18
- // options: []
19
- options: [
20
- {
21
- id: "q3-opt1" as BuilderObjectId.QuestionOptionID,
22
- value: 0,
23
- label: "Nei",
24
- },
25
- {
26
- id: "q3-opt2" as BuilderObjectId.QuestionOptionID,
27
- value: 1,
28
- label: "Ja",
29
- },
30
- {
31
- id: "q3-opt3" as BuilderObjectId.QuestionOptionID,
32
- value: 9,
33
- label: "Vet ikke",
34
- },
35
- ],
36
- };
37
-
38
- describe("Builder Question", () => {
39
- test("Can create question from json", () => {
40
- const q1 = BuilderQuestion.fromJson(question1);
41
- const q1AsJson = q1.toJson();
42
- expect(question1).toStrictEqual(q1AsJson);
43
- const q2Instance = BuilderQuestion.fromJson(question2);
44
- const q2AsJson = q2Instance.toJson();
45
- expect(question2).toStrictEqual(q2AsJson);
46
- });
47
- test("Can add options", () => {
48
- const q1 = BuilderQuestion.fromJson(question1);
49
- expect(q1.options.length).toBe(0);
50
- q1.addOption("Ja", 1);
51
- expect(q1.options.length).toBe(1);
52
- const nei = q1.addOption("Nei", 0, 0);
53
- expect(q1.options.length).toBe(2);
54
- expect(q1.options[0]).toBe(nei);
55
- const vetIkke = q1.addOption("Vet-ikke", 9);
56
- expect(q1.options.length).toBe(3);
57
- expect(q1.options[2]).toBe(vetIkke);
58
- });
59
- test("Can delete options", () => {
60
- const q = BuilderQuestion.fromJson(question2);
61
- expect(q.options.length).toBe(3);
62
- const o1 = q.options[0];
63
- const o2 = q.options[1];
64
- const o3 = q.options[2];
65
- const didDelete = q.deleteOption(o1);
66
- expect(q.options.length).toBe(2);
67
- });
68
- });
1
+ import type { BuilderQuestionDto } from "./Builder-question";
2
+ import { BuilderQuestion } from "./Builder-question";
3
+ import type { BuilderObjectId } from "./BuilderObject";
4
+
5
+ const question1: BuilderQuestionDto = {
6
+ id: "q1" as BuilderObjectId.QuestionID,
7
+ prefix: "q1",
8
+ text: "sadf",
9
+ options: [],
10
+ _type: "select-one",
11
+ };
12
+
13
+ const question2: BuilderQuestionDto = {
14
+ id: "q3" as BuilderObjectId.QuestionID,
15
+ prefix: "a",
16
+ text: "har du..",
17
+ _type: "select-one",
18
+ // options: []
19
+ options: [
20
+ {
21
+ id: "q3-opt1" as BuilderObjectId.QuestionOptionID,
22
+ value: 0,
23
+ label: "Nei",
24
+ },
25
+ {
26
+ id: "q3-opt2" as BuilderObjectId.QuestionOptionID,
27
+ value: 1,
28
+ label: "Ja",
29
+ },
30
+ {
31
+ id: "q3-opt3" as BuilderObjectId.QuestionOptionID,
32
+ value: 9,
33
+ label: "Vet ikke",
34
+ },
35
+ ],
36
+ };
37
+
38
+ describe("Builder Question", () => {
39
+ test("Can create question from json", () => {
40
+ const q1 = BuilderQuestion.fromJson(question1);
41
+ const q1AsJson = q1.toJson();
42
+ expect(question1).toStrictEqual(q1AsJson);
43
+ const q2Instance = BuilderQuestion.fromJson(question2);
44
+ const q2AsJson = q2Instance.toJson();
45
+ expect(question2).toStrictEqual(q2AsJson);
46
+ });
47
+ test("Can add options", () => {
48
+ const q1 = BuilderQuestion.fromJson(question1);
49
+ expect(q1.options.length).toBe(0);
50
+ q1.addOption("Ja", 1);
51
+ expect(q1.options.length).toBe(1);
52
+ const nei = q1.addOption("Nei", 0, 0);
53
+ expect(q1.options.length).toBe(2);
54
+ expect(q1.options[0]).toBe(nei);
55
+ const vetIkke = q1.addOption("Vet-ikke", 9);
56
+ expect(q1.options.length).toBe(3);
57
+ expect(q1.options[2]).toBe(vetIkke);
58
+ });
59
+ test("Can delete options", () => {
60
+ const q = BuilderQuestion.fromJson(question2);
61
+ expect(q.options.length).toBe(3);
62
+ const o1 = q.options[0];
63
+ const o2 = q.options[1];
64
+ const o3 = q.options[2];
65
+ const didDelete = q.deleteOption(o1);
66
+ expect(q.options.length).toBe(2);
67
+ });
68
+ });