/
propertyConditionEditorTests.ts
1885 lines (1822 loc) · 58.2 KB
/
propertyConditionEditorTests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as ko from "knockout";
import * as Survey from "survey-knockout";
import {
doGetCompletions,
SurveyPropertyConditionEditor,
} from "../../src/propertyEditors/propertyConditionEditor";
import { EditorOptionsTests } from "../editorOptionsTests";
import { SurveyPropertyDropdownColumnsEditor } from "../../src/propertyEditors/propertyMatrixDropdownColumnsEditor";
import { SurveyElementEditorContentModel } from "../../src/questionEditors/questionEditor";
export default QUnit.module("SurveyPropertyConditionEditor");
QUnit.test("Autocomplete without prefix test", function (assert) {
var prevIdentifier = "";
var currentQuestion = new Survey.QuestionDropdown("dropdown");
var usableQuestions = [new Survey.QuestionExpression("expression")];
var completions = null;
var prefix = "";
completions = doGetCompletions(prevIdentifier, prefix, {
question: currentQuestion,
questions: usableQuestions,
});
//TODO uncomment after releasing v1.0.88
//assert.equal(completions.length, 22 + 1, "all completions");
assert.equal(completions[0].value, "{expression}", "questions first");
prefix = null;
completions = doGetCompletions(prevIdentifier, prefix, {
question: currentQuestion,
questions: usableQuestions,
});
//TODO uncomment after releasing v1.0.88
//assert.equal(completions.length, 22 + 1, "all completions");
assert.equal(completions[0].value, "{expression}", "questions first");
});
QUnit.test("Autocomplete without prefix test, using valueName", function (
assert
) {
var prevIdentifier = "";
var currentQuestion = new Survey.QuestionDropdown("dropdown");
var textQuestion = new Survey.QuestionText("question1");
textQuestion.valueName = "value1";
var usableQuestions = [textQuestion];
var completions = null;
var prefix = "";
completions = doGetCompletions(prevIdentifier, prefix, {
question: currentQuestion,
questions: usableQuestions,
});
assert.equal(completions[0].value, "{value1}", "questions first, valueName");
});
QUnit.test("Autocomplete with prefix test", function (assert) {
var prevIdentifier = "";
var currentQuestion = new Survey.QuestionDropdown("dropdown");
var usableQuestions = [
new Survey.QuestionExpression("expression"),
new Survey.QuestionDropdown("dropdown2"),
];
var completions = null;
var prefix = "dr";
completions = doGetCompletions(prevIdentifier, prefix, {
question: currentQuestion,
questions: usableQuestions,
});
assert.equal(completions.length, 1, "filtered completions");
assert.equal(completions[0].value, "{dropdown2}", "dropdown2");
var prefix = "eq";
completions = doGetCompletions(prevIdentifier, prefix, {
question: currentQuestion,
questions: usableQuestions,
});
assert.equal(completions.length, 4, "filtered completions");
assert.equal(completions[0].value, "equal", "equal");
});
QUnit.test("Autocomplete with matrix test", function (assert) {
var prevIdentifier = "";
var question = new Survey.QuestionMatrixDynamic("matrixdynamic");
question.addColumn("c1");
question.addColumn("c2");
var currentQuestion: any = question.columns[0];
var usableQuestions = [];
var completions = null;
var prefix = "{";
completions = doGetCompletions(prevIdentifier, prefix, {
question: currentQuestion,
questions: usableQuestions,
});
assert.equal(completions.length, 1, "row completions");
assert.equal(completions[0].value, "{row.", "first row");
prevIdentifier = "row";
prefix = "";
completions = doGetCompletions(prevIdentifier, prefix, {
question: currentQuestion,
questions: usableQuestions,
});
assert.equal(completions.length, 1, "row completions");
assert.equal(completions[0].value, "{row.c2}", "second row");
});
QUnit.test("Autocomplete with panel test", function (assert) {
var prevIdentifier = "";
var question = new Survey.QuestionPanelDynamic("paneldynamic");
question.template.addNewQuestion("text", "q1");
question.template.addNewQuestion("text", "q2");
question.value = [{}];
var currentQuestion: any = question.panels[0].elements[0];
var usableQuestions = [];
var completions = null;
var prefix = "{";
completions = doGetCompletions(prevIdentifier, prefix, {
question: currentQuestion,
questions: usableQuestions,
});
assert.equal(completions.length, 1, "panel completions");
assert.equal(completions[0].value, "{panel.", "first panel");
prevIdentifier = "panel";
prefix = "";
completions = doGetCompletions(prevIdentifier, prefix, {
question: currentQuestion,
questions: usableQuestions,
});
assert.equal(completions.length, 1, "panel completions");
assert.equal(completions[0].value, "{panel.q2}", "second panel");
});
QUnit.test("SurveyPropertyConditionEditor.isValid", function (assert) {
var question = new Survey.QuestionText("q1");
question.visibleIf = "ddd";
var property = Survey.Serializer.findProperty("question", "visibleIf");
var editor = new SurveyPropertyConditionEditor(property);
assert.equal(
editor.koIsTextConditionValid(),
true,
"The empty value is valid"
);
editor.object = question;
// TODO: reanimate condition error checks
// assert.equal(
// editor.koIsValid(),
// false,
// "The question.visibleIf was not valid"
// );
editor.koTextValue("{q} = 1");
assert.equal(
editor.koIsTextConditionValid(),
true,
"The condition is value now"
);
});
QUnit.test("SurveyPropertyConditionEditor.addCondition", function (assert) {
var survey = new Survey.SurveyModel({
questions: [
{ type: "text", name: "q1" },
{ type: "text", name: "q" },
{ type: "text", name: "q2" },
],
});
var question = survey.getQuestionByName("q1");
question.visibleIf = "{q} = 1";
var property = Survey.Serializer.findProperty("question", "visibleIf");
var editor = new SurveyPropertyConditionEditor(property);
editor.object = question;
editor.beforeShow();
editor.isEditorShowing = true;
editor.addCondition();
var editorItem = editor.koEditorItems()[1];
assert.equal(editorItem.isReady, false, "We can't add condition");
editorItem.questionName = "q2";
assert.equal(editorItem.isReady, false, "value is empty");
editorItem.value = "2";
assert.equal(editorItem.isReady, true, "Can add condition");
assert.equal(
editor.koTextValue(),
"{q} = 1 and {q2} = 2",
"condition was added"
);
editor.koTextValue("");
editorItem = editor.koEditorItems()[0];
assert.notOk(editorItem.questionName, "question should empty");
assert.equal(editorItem.isReady, false, "values were reset.");
editorItem.questionName = "q1";
editorItem.value = "abc";
assert.equal(editor.koTextValue(), "{q1} = 'abc'", "condition was replaced");
editorItem.value = null;
assert.equal(editorItem.isReady, false, "value is not set");
editorItem.operator = "empty";
assert.equal(editorItem.isReady, true, "empty doesn't require value");
assert.equal(editor.koTextValue(), "{q1} empty", "condition was replaced");
});
QUnit.test(
"SurveyPropertyConditionEditor.addCondition quotes - https://surveyjs.answerdesk.io/ticket/details/T2679",
function (assert) {
var survey = new Survey.SurveyModel({
questions: [
{ type: "text", name: "q1" },
{ type: "dropdown", name: "q2" },
],
});
var question = survey.getQuestionByName("q1");
var property = Survey.Serializer.findProperty("question", "visibleIf");
var editor = new SurveyPropertyConditionEditor(property);
editor.object = question;
editor.beforeShow();
editor.isEditorShowing = true;
var editorItem = editor.koEditorItems()[0];
editorItem.questionName = "q2";
assert.ok(editorItem.nameQuestion, "question value is created");
editorItem.value = JSON.stringify(["item1's"]);
assert.equal(
editor.koTextValue(),
'{q2} = ["item1\\\'s"]',
"Single quote escaped in condition"
);
}
);
QUnit.test(
"Apostrophes in answers break VisibleIf - https://github.com/surveyjs/editor/issues/476",
function (assert) {
var survey = new Survey.SurveyModel({
questions: [
{ type: "text", name: "q1" },
{ type: "text", name: "q2" },
],
});
var question = survey.getQuestionByName("q1");
var property = Survey.Serializer.findProperty("question", "visibleIf");
var editor = new SurveyPropertyConditionEditor(property);
editor.object = question;
editor.beforeShow();
editor.isEditorShowing = true;
var editorItem = editor.koEditorItems()[0];
editorItem.questionName = "q2";
editorItem.value = "d'2";
assert.equal(
editorItem.toString(),
"{q2} = 'd\\'2'",
"Apostrophe is escaped"
);
}
);
QUnit.test(
"SurveyPropertyConditionEditor add question for dynamic panel",
function (assert) {
var survey = new Survey.Survey({
elements: [
{
name: "dp",
type: "paneldynamic",
templateElements: [
{
name: "q1",
type: "text",
visibleIf: "{panel.q2} = 1",
},
{
name: "q2",
type: "text",
},
],
},
],
});
survey.setDesignMode(true);
var panel = <Survey.QuestionPanelDynamic>survey.getQuestionByName("dp");
var question = panel.template.getQuestionByName("q1");
var property = Survey.Serializer.findProperty("question", "visibleIf");
var editor = new SurveyPropertyConditionEditor(property);
editor.object = question;
editor.beforeShow();
editor.isEditorShowing = true;
editor.addCondition();
var editorItem = editor.koEditorItems()[1];
editorItem.questionName = "panel.q2";
editorItem.value = "2";
assert.equal(
editor.koTextValue(),
"{panel.q2} = 1 and {panel.q2} = 2",
"condition was added correctly"
);
}
);
QUnit.test(
"SurveyPropertyConditionEditor, use question.valueName, bug: #353",
function (assert) {
var property = Survey.Serializer.findProperty("question", "visibleIf");
var survey = new Survey.Survey();
var page = survey.addNewPage("p");
var question = page.addNewQuestion("text", "q1");
var question2 = <Survey.Question>page.addNewQuestion("text", "q2");
question2.valueName = "val2";
var matrix = <Survey.QuestionMatrixDropdown>(
page.addNewQuestion("matrixdropdown", "matrix")
);
matrix.choices = [1, 2];
matrix.rows = ["row1"];
matrix.addColumn("column1");
matrix.valueName = "val3";
var editor = new SurveyPropertyConditionEditor(property);
editor.object = question;
editor.beforeShow();
editor.isEditorShowing = true;
var editorItem = editor.koEditorItems()[0];
editorItem.questionName = "val2";
editorItem.value = "abc";
assert.equal(
editor.koTextValue(),
"{val2} = 'abc'",
"valueName property is used"
);
editorItem.questionName = "val3.row1.column1";
assert.equal(
editorItem.valueQuestion.getType(),
"dropdown",
"Value question for matrix column created correctly"
);
editorItem.value = "1";
assert.equal(
editor.koTextValue(),
"{val3.row1.column1} = 1",
"valueName property is used in matrix"
);
}
);
QUnit.test(
"SurveyPropertyConditionEditor, use question.valueName, bug: #367",
function (assert) {
var property = Survey.Serializer.findProperty("question", "visibleIf");
var survey = new Survey.Survey();
var page = survey.addNewPage("p");
var question = <Survey.QuestionDropdown>page.addNewQuestion("text", "q1");
var question2 = <Survey.QuestionDropdown>(
page.addNewQuestion("dropdown", "q2.")
);
question2.choices = [1, 2, 3, 4];
question2.valueName = "val2";
var matrix = <Survey.QuestionMatrixDropdown>(
page.addNewQuestion("matrixdropdown", "matrix")
);
matrix.choices = [1, 2];
matrix.rows = ["row1"];
matrix.addColumn("column1");
var editor = new SurveyPropertyConditionEditor(property);
editor.object = question;
editor.beforeShow();
editor.isEditorShowing = true;
var editorItem = editor.koEditorItems()[0];
editorItem.questionName = "val2";
var valueQuestion = editorItem.valueQuestion;
assert.ok(valueQuestion, "Value question created correctly");
assert.equal(
valueQuestion.choices.length,
4,
"value question created correctly"
);
editorItem.questionName = "matrix.row1.column1";
valueQuestion = editorItem.valueQuestion;
assert.ok(
valueQuestion,
"Value question for matrix column created correctly"
);
assert.equal(
valueQuestion.choices.length,
2,
"value question for matrix column created correctly"
);
}
);
QUnit.test(
"SurveyPropertyConditionEditor, use dropdown question instead of readiogroup for editing values",
function (assert) {
var property = Survey.Serializer.findProperty("question", "visibleIf");
var survey = new Survey.Survey();
var page = survey.addNewPage("p");
var question = <Survey.QuestionDropdown>page.addNewQuestion("text", "q1");
var question2 = <Survey.QuestionRadiogroup>(
page.addNewQuestion("radiogroup", "q2")
);
question2.choices = [1, 2, 3, 4];
var editor = new SurveyPropertyConditionEditor(property);
editor.object = question;
editor.beforeShow();
editor.isEditorShowing = true;
var editorItem = editor.koEditorItems()[0];
editorItem.questionName = "q2";
var questionValue = editorItem.valueQuestion;
assert.ok(questionValue, "There question value");
assert.equal(
questionValue.getType(),
"dropdown",
"Use dropdown instead of radiogroup"
);
}
);
QUnit.test(
"SurveyPropertyConditionEditor, add condition from wizard on apply, without pressing 'Add' button",
function (assert) {
var property = Survey.Serializer.findProperty("question", "visibleIf");
var survey = new Survey.Survey();
var page = survey.addNewPage("p");
var question = page.addNewQuestion("text", "q1");
var question2 = <Survey.Question>page.addNewQuestion("text", "q2");
question2.valueName = "val2";
var editor = new SurveyPropertyConditionEditor(property);
editor.object = question;
editor.beforeShow();
editor.isEditorShowing = true;
var editorItem = editor.koEditorItems()[0];
editorItem.questionName = "val2";
editorItem.value = "abc";
assert.equal(
editor.koTextValue(),
"{val2} = 'abc'",
"valueName property is used"
);
}
);
QUnit.test("SurveyPropertyConditionEditor.allConditionQuestions", function (
assert
) {
var property = Survey.Serializer.findProperty("question", "visibleIf");
var survey = new Survey.Survey();
var page = survey.addNewPage("p");
var question = page.addNewQuestion("text", "q1");
page.addNewQuestion("text", "q2");
page.addNewQuestion("text", "q3");
var editor = new SurveyPropertyConditionEditor(property);
editor.object = question;
var res = [];
for (var i = 0; i < editor.allConditionQuestions.length; i++) {
var item = editor.allConditionQuestions[i];
res.push({ name: item.name, text: item.text });
}
assert.deepEqual(
res,
[
{ name: "q2", text: "q2" },
{ name: "q3", text: "q3" },
],
"returns questions correctly"
);
});
QUnit.test(
"SurveyPropertyConditionEditor.allCondtionQuestions for matrix column",
function (assert) {
var property = Survey.Serializer.findProperty(
"matrixdropdowncolumn",
"visibleIf"
);
var survey = new Survey.Survey();
var page = survey.addNewPage("p");
var question = <Survey.QuestionMatrixDropdown>(
page.addNewQuestion("matrixdropdown", "q1")
);
question.columns.splice(0, question.columns.length);
question.addColumn("col1");
var column = question.addColumn("col2");
question.addColumn("col3");
var editor = new SurveyPropertyConditionEditor(property);
editor.object = column;
assert.equal(
editor.allConditionQuestions.filter((e) => e.name === "row.col1").length >
0,
true,
"row.col1 is here"
);
assert.equal(
editor.allConditionQuestions.filter((e) => e.name === "row.col2").length >
0,
false,
"row.col2 is not here"
);
}
);
QUnit.test(
"SurveyPropertyConditionEditor.allCondtionQuestions for panel dynamic",
function (assert) {
var property = Survey.Serializer.findProperty("question", "visibleIf");
var survey = new Survey.Survey();
var page = survey.addNewPage("p");
var question = <Survey.QuestionPanelDynamic>(
page.addNewQuestion("paneldynamic", "q1")
);
question.template.addNewQuestion("text", "q1");
question.template.addNewQuestion("text", "q2");
question.template.addNewQuestion("text", "q3");
question.panelCount = 1;
var panelQuestion = question.template.questions[1];
var editor = new SurveyPropertyConditionEditor(property);
editor.object = panelQuestion;
assert.equal(
editor.allConditionQuestions.filter((e) => e.name === "panel.q1").length >
0,
true,
"panel.q1 is here"
);
assert.equal(
editor.allConditionQuestions.filter((e) => e.name === "panel.q2").length >
0,
false,
"panel.q2 is not here"
);
}
);
QUnit.test(
"SurveyPropertyConditionEditor show invisible choices and make all choices enabled, Bug: https://surveyjs.answerdesk.io/ticket/details/T1921",
function (assert) {
var property = Survey.Serializer.findProperty("question", "visibleIf");
var survey = new Survey.Survey();
var page = survey.addNewPage("p");
var question = page.addNewQuestion("text", "q1");
var radioQuestion = page.addNewQuestion("dropdown", "q2");
radioQuestion.choices = [
{ value: 1, visibleIf: "{a} = 1" },
{ value: 1, enabledIf: "{b} = 1" },
];
var editor = new SurveyPropertyConditionEditor(property);
editor.object = question;
editor.beforeShow();
editor.isEditorShowing = true;
assert.equal(
editor.allConditionQuestions.length,
1,
"There are one question"
);
assert.equal(
editor.allConditionQuestions[0].name,
"q2",
"It is our question"
);
var editorItem = editor.koEditorItems()[0];
editorItem.questionName = "q2";
var vQuestion = editorItem.valueQuestion;
assert.equal(vQuestion.visibleChoices.length, 2, "Show all choices");
}
);
QUnit.test(
"SurveyPropertyConditionEditor, error in value input, Bug# T2598 (customer marked it private)",
function (assert) {
var survey = new Survey.Survey({
elements: [
{ name: "q1", type: "checkbox", choices: [1, 2, 3] },
{ name: "q2", type: "checkbox", choices: [1, 2, 3] },
{ name: "q3", type: "checkbox", choices: ["a", "b"] },
{ name: "q4", type: "text" },
],
});
var question = survey.getQuestionByName("q2");
var property = Survey.Serializer.findProperty("question", "visibleIf");
var editor = new SurveyPropertyConditionEditor(property);
editor.object = question;
editor.beforeShow();
editor.isEditorShowing = true;
var editorItem = editor.koEditorItems()[0];
editorItem.questionName = "q1";
editorItem.valueQuestion.value = 1;
assert.equal(
editor.koTextValue(),
"{q1} = [1]",
"Set expression correctly"
);
editorItem.questionName = "q3";
assert.equal(editor.koTextValue(), "", "Set expression is reset");
}
);
QUnit.test(
"SurveyPropertyConditionEditor, could not edit value for row.column1 on editing column, Bug# T3376 (customer marked it private)",
function (assert) {
var survey = new Survey.Survey({
elements: [
{
name: "question1",
type: "matrixdynamic",
columns: [{ name: "Column1" }, { name: "Column2" }],
choices: [1, 2, 3, 4, 5],
},
],
});
var question = <Survey.QuestionMatrixDynamic>(
survey.getQuestionByName("question1")
);
var property = Survey.Serializer.findProperty(
"matrixdropdownbase",
"columns"
);
var columnsEditor = new SurveyPropertyDropdownColumnsEditor(property);
columnsEditor.object = question;
columnsEditor.beforeShow();
var itemViewModel = columnsEditor.createItemViewModel(question.columns[0]);
columnsEditor.onEditItemClick(itemViewModel);
var colDetailEditor = <SurveyElementEditorContentModel>(
columnsEditor.koEditItem().itemEditor
);
var visEditor = <SurveyPropertyConditionEditor>(
colDetailEditor.getPropertyEditorByName("visibleIf").editor
);
assert.ok(visEditor, "visibleIf editor is here");
visEditor.isEditorShowing = true;
var editorItem = visEditor.koEditorItems()[0];
editorItem.questionName = "row.Column2";
var questionValue = editorItem.valueQuestion;
assert.equal(
questionValue.getType(),
"dropdown",
"value question is dropdown"
);
}
);
QUnit.test(
"SurveyPropertyConditionEditor, question has defaultValue and user could not add condition with it, Bug# T2778 (customer marked it private)",
function (assert) {
var survey = new Survey.Survey({
elements: [
{ name: "q1", type: "radiogroup", choices: [1, 2, 3], defaultValue: 1 },
{ name: "q2", type: "text" },
],
});
var question = survey.getQuestionByName("q2");
var property = Survey.Serializer.findProperty("question", "visibleIf");
var editor = new SurveyPropertyConditionEditor(property);
editor.object = question;
editor.beforeShow();
editor.isEditorShowing = true;
var editorItem = editor.koEditorItems()[0];
assert.equal(editorItem.isReady, false, "There is no question set");
assert.notOk(editorItem.value, "It is empty by default");
editorItem.questionName = "q1";
assert.equal(editorItem.value, 1, "The value equals default value");
assert.equal(editorItem.isReady, true, "The question has default value");
}
);
QUnit.test(
"SurveyPropertyConditionEditor, test koConditionDisplayText property",
function (assert) {
var survey = new Survey.Survey({
elements: [
{
name: "q1",
title: "Question 1",
type: "radiogroup",
choices: [
{ value: 1, text: "Item 1" },
{ value: 2, text: "Item 2" },
{ value: 3, text: "Item 3" },
],
},
{ name: "q2", type: "text", visibleIf: "{q1} = 1" },
],
});
var question = survey.getQuestionByName("q2");
var property = Survey.Serializer.findProperty("question", "visibleIf");
var editor = new SurveyPropertyConditionEditor(property);
editor.options = new EditorOptionsTests();
editor.options.showTitlesInExpressions = true;
editor.object = question;
editor.beforeShow();
assert.equal(editor.koConditionDisplayText(), "{Question 1} == Item 1");
editor.koValue("");
assert.equal(editor.koConditionDisplayText(), "Expression is empty");
editor.koValue("{q1} < 2");
assert.equal(editor.koConditionDisplayText(), "{Question 1} < Item 2");
}
);
QUnit.test(
"SurveyPropertyConditionEditor, automatically modify koTextValue/koValue without apply",
function (assert) {
var survey = new Survey.Survey({
elements: [
{
name: "q1",
title: "Question 1",
type: "radiogroup",
choices: [
{ value: 1, text: "Item 1" },
{ value: 2, text: "Item 2" },
{ value: 3, text: "Item 3" },
],
},
{ name: "q2", type: "text" },
{ name: "q3", type: "text", visibleIf: "{q2} = 1" },
],
});
var question = survey.getQuestionByName("q2");
var property = Survey.Serializer.findProperty("question", "visibleIf");
var editor = new SurveyPropertyConditionEditor(property);
editor.object = question;
editor.beforeShow();
editor.isEditorShowing = true;
var editorItem = editor.koEditorItems()[0];
editorItem.questionName = "q3";
editorItem.value = "val1";
assert.equal(
editor.koTextValue(),
"{q3} = 'val1'",
"Set text value automatically"
);
assert.equal(
editor.koValue(),
"{q3} = 'val1'",
"Set koValue automatically"
);
editorItem.value = "val2";
assert.equal(
editor.koTextValue(),
"{q3} = 'val2'",
"Update text value automatically"
);
assert.equal(
editor.koValue(),
"{q3} = 'val2'",
"Update koValue automatically"
);
editorItem.operator = "notequal";
assert.equal(
editor.koTextValue(),
"{q3} <> 'val2'",
"Update text value automatically on changing operator"
);
assert.equal(
editor.koValue(),
"{q3} <> 'val2'",
"Update koValue automatically on changing operator"
);
editorItem.questionName = "q1";
assert.equal(editor.koTextValue(), "", "Reset the value");
editorItem.value = 2;
assert.equal(
editor.koTextValue(),
"{q1} = 2",
"Set value automatically #2"
);
editor.object = survey.getQuestionByName("q3");
editor.beforeShow();
editor.isEditorShowing = true;
editorItem = editor.koEditorItems()[0];
assert.equal(
editor.koTextValue(),
"{q2} = 1",
"Update the value on object change"
);
editorItem.questionName = "q2";
editorItem.value = 3;
assert.equal(editor.koTextValue(), "{q2} = 3", "update the koTextValue");
}
);
QUnit.test(
"SurveyPropertyConditionEditor, automatically modify koTextValue/koValue with empty/non empty",
function (assert) {
var survey = new Survey.Survey({
elements: [
{
name: "q1",
title: "Question 1",
type: "radiogroup",
choices: [
{ value: 1, text: "Item 1" },
{ value: 2, text: "Item 2" },
{ value: 3, text: "Item 3" },
],
},
{ name: "q2", type: "text" },
],
});
var question = survey.getQuestionByName("q2");
var property = Survey.Serializer.findProperty("question", "visibleIf");
var editor = new SurveyPropertyConditionEditor(property);
editor.object = question;
editor.beforeShow();
editor.isEditorShowing = true;
var editorItem = editor.koEditorItems()[0];
editorItem.questionName = "q1";
editorItem.operator = "empty";
assert.equal(
editor.koTextValue(),
"{q1} empty",
"Set text value automatically"
);
assert.equal(editor.koValue(), "{q1} empty", "Set koValue automatically");
editorItem.operator = "equals";
assert.equal(editor.koValue(), "", "Set koValue is empty");
}
);
QUnit.test("SurveyPropertyConditionEditor, clearCondition", function (assert) {
var survey = new Survey.Survey({
elements: [
{ name: "q1", type: "text" },
{ name: "q2", type: "text" },
],
});
var question = survey.getQuestionByName("q2");
var property = Survey.Serializer.findProperty("question", "visibleIf");
var editor = new SurveyPropertyConditionEditor(property);
editor.object = question;
editor.beforeShow();
editor.isEditorShowing = true;
editor.koTextValue("{q1} = ");
assert.equal(
editor.koIsTextConditionValid(),
false,
"Condition is not valid"
);
assert.equal(editor.koValue(), "", "Invalid condition, value is empty");
assert.equal(
editor.koConditionDisplayText(),
editor.getLocString("pe.expressionIsEmpty"),
"Invalid condition, display text is empty"
);
editor.koTextValue("{q1} = '1'");
assert.equal(
editor.koValue(),
"{q1} = '1'",
"Condition is valid, value is set"
);
assert.equal(
editor.koTextValue(),
"{q1} = '1'",
"Condition is valid, text value is set"
);
});
QUnit.test(
"SurveyPropertyConditionEditor, modify koValue for editor in Modal Window",
function (assert) {
var property = Survey.Serializer.findProperty("question", "visibleIf");
var survey = new Survey.Survey();
var page = survey.addNewPage("p");
var question = page.addNewQuestion("text", "q1");
page.addNewQuestion("text", "q2");
var editor = new SurveyPropertyConditionEditor(property);
editor.onChanged = function (newValue) {
question.visibleIf = newValue;
};
editor.object = question;
editor.beforeShow();
editor.isEditorShowing = true;
var editorItem = editor.koEditorItems()[0];
editorItem.questionName = "q2";
editorItem.value = "abc";
assert.equal(editor.koValue(), "{q2} = 'abc'", "value is set");
assert.equal(
question.visibleIf,
"{q2} = 'abc'",
"value is set into question"
);
}
);
QUnit.test("SurveyPropertyConditionEditor, enabled operators", function (
assert
) {
var survey = new Survey.Survey({
elements: [
{ name: "q1", type: "text" },
{ name: "qText", type: "text" },
{ name: "qComment", type: "comment" },
{ name: "qRadio", type: "radiogroup", choices: [1, 2] },
{ name: "qDropdown", type: "dropdown", choices: [1, 2] },
{ name: "qRating", type: "rating" },
{ name: "qCheck", type: "checkbox", choices: [1, 2] },
{ name: "qBoolean", type: "boolean" },
{ name: "qExpression", type: "expression" },
{ name: "qFile", type: "file" },
{ name: "qImagepicker", type: "imagepicker" },
{ name: "qMatrix", type: "matrix", rows: ["row1", "row2"] },
{
name: "qMatrixdropdown",
type: "matrixdropdown",
rows: ["row1", "row2"],
columns: [
{ cellType: "text", name: "col1" },
{ cellType: "radiogroup", name: "col2" },
{ cellType: "checkbox", name: "col3" },
],
},
{
name: "qMatrixdynamic",
type: "matrixdynamic",
rowCount: 2,
columns: [
{ cellType: "text", name: "col1" },
{ cellType: "radiogroup", name: "col2" },
{ cellType: "checkbox", name: "col3" },
],
},
{ name: "qMultipletext", type: "multipletext" },
],
});
var question = survey.getQuestionByName("q1");
var property = Survey.Serializer.findProperty("question", "visibleIf");
var editor = new SurveyPropertyConditionEditor(property);
editor.object = question;
editor.beforeShow();
editor.isEditorShowing = true;
var checkFun = function (questionName: string, operatorNames: Array<string>) {
var editorItem = editor.koEditorItems()[0];
editorItem.questionName = questionName;
var choices = editorItem.operatorQuestion.choices;
for (var i = 0; i < choices.length; i++) {
var isItemEnabled = choices[i].isEnabled;
var operatorName = choices[i].value;
assert.notOk(
isItemEnabled && operatorNames.indexOf(operatorName) < 0,
"Operator: '" +
operatorName +
" should not be for question: " +
questionName
);
assert.notOk(
!isItemEnabled && operatorNames.indexOf(operatorName) > -1,
"Operator: '" +
operatorName +
" should be for question: " +
questionName
);
}
};
var checkFunMultiple = function (
questionNames: Array<string>,
operatorNames: Array<string>
) {
for (var i = 0; i < questionNames.length; i++) {
checkFun(questionNames[i], operatorNames);
}
};
checkFunMultiple(
[
"qText",
"qComment",
"qExpression",
"qMatrixdropdown.row1.col1",
"qMatrixdynamic[0].col1",
],
[
"empty",
"notempty",
"equal",
"notequal",
"contains",
"notcontains",
"greater",
"less",
"greaterorequal",
"lessorequal",
]
);
checkFunMultiple(
[
"qRadio",
"qDropdown",
"qMatrix.row1",
"qMatrixdropdown.row1.col2",
"qMatrixdynamic[0].col2",