forked from surveyjs/survey-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurveytests.ts
1775 lines (1671 loc) · 85.7 KB
/
surveytests.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 {SurveyModel} from "../src/survey";
import {PageModel} from "../src/page";
import {PanelModel} from "../src/panel";
import {QuestionFactory} from "../src/questionfactory";
import {Question} from "../src/question";
import {QuestionHtmlModel} from "../src/question_html";
import {SurveyTriggerVisible, SurveyTriggerComplete, SurveyTriggerSetValue} from "../src/trigger";
import {surveyLocalization} from "../src/surveyStrings";
import {EmailValidator, NumericValidator} from "../src/validator";
import {JsonObject} from "../src/jsonobject";
import {QuestionTextModel} from "../src/question_text";
import {QuestionMultipleTextModel, MultipleTextItemModel} from "../src/question_multipletext";
import {QuestionMatrixModel} from "../src/question_matrix";
import {ISurvey, ISurveyData} from "../src/base";
import {ItemValue} from "../src/itemvalue";
import {QuestionDropdownModel} from "../src/question_dropdown";
import {QuestionCheckboxModel} from "../src/question_checkbox";
import {QuestionRadiogroupModel} from "../src/question_radiogroup";
import {QuestionCommentModel} from "../src/question_comment";
import {QuestionFileModel} from "../src/question_file";
import {QuestionMatrixDropdownModel} from "../src/question_matrixdropdown";
import {QuestionMatrixDynamicModel} from "../src/question_matrixdynamic";
import {QuestionRatingModel} from "../src/question_rating";
import {CustomWidgetCollection, QuestionCustomWidget} from "../src/questionCustomWidgets";
import {QuestionSelectBase} from "../src/question_baseselect";
import {LocalizableString} from "../src/localizablestring";
import {surveyCss} from "../src/defaultCss/cssstandard";
import {dxSurveyService} from "../src/dxSurveyService";
export default QUnit.module("Survey");
QUnit.test("set data property", function (assert) {
var survey = new SurveyModel();
assert.deepEqual(survey.data, {}, "there is no data");
survey.data = { strVal: 'item1', intVal: 5 };
assert.deepEqual(survey.data, { strVal: 'item1', intVal: 5 }, "set the object");
survey.data = null;
assert.deepEqual(survey.data, { }, "clear data");
});
QUnit.test("Add two pages", function (assert) {
var survey = new SurveyModel();
survey.addPage(new PageModel("Page 1"));
survey.addPage(new PageModel("Page 2"));
assert.equal(survey.PageCount, 2, "Two pages");
});
QUnit.test("Current Page", function (assert) {
var survey = new SurveyModel();
survey.addPage(createPageWithQuestion("Page 1"));
assert.equal(survey.currentPage, survey.pages[0], "the first page is current");
survey.currentPage = null;
assert.equal(survey.currentPage, survey.pages[0], "can't set curent page to null");
var sPage = createPageWithQuestion("new Page");
survey.addPage(sPage);
survey.currentPage = sPage;
assert.equal(survey.currentPage, survey.pages[1], "second page is current");
survey.pages.pop();
assert.equal(survey.currentPage, survey.pages[0], "the first page is current after removing the current one");
});
QUnit.test("CurrentPageNo", function (assert) {
var survey = new SurveyModel();
survey.addPage(createPageWithQuestion("Page 1"));
assert.equal(survey.currentPageNo, 0, "the first page is current");
survey.currentPageNo = -1;
assert.equal(survey.currentPageNo, 0, "can't set curent page to -1");
survey.currentPageNo = 1;
assert.equal(survey.currentPageNo, 0, "can't set curent page to PageNo + 1");
var sPage = createPageWithQuestion("new Page");
survey.addPage(sPage);
survey.currentPageNo = 1;
assert.equal(survey.currentPageNo, 1, "second page is current");
survey.pages.pop();
assert.equal(survey.currentPageNo, 0, "the first page is current after removing the current one");
});
QUnit.test("Remove Page in design mode", function (assert) {
var survey = new SurveyModel();
survey.setDesignMode(true);
survey.addPage(new PageModel("Page 1"));
survey.addPage(new PageModel("Page 2"));
assert.equal(survey.PageCount, 2, "Two pages");
assert.equal(survey.currentPage.name, "Page 1", "the first page is current");
survey.removePage(survey.pages[0]);
assert.equal(survey.PageCount, 1, "One page left");
assert.equal(survey.currentPage.name, "Page 2", "the second page is current");
});
QUnit.test("Survey.onValueChanged event, #352", function (assert) {
var survey = new SurveyModel();
var page = survey.addNewPage("page1");
var q1 = <QuestionDropdownModel>page.addNewQuestion("dropdown", "q1");
q1.choices = [1, 2, 3];
q1.hasOther = true;
var valueChangedCallCounter = 0;
survey.onValueChanged.add(function(survey, options){
valueChangedCallCounter ++;
});
assert.equal(valueChangedCallCounter, 0, "Nothing happens");
q1.value = 1;
assert.equal(valueChangedCallCounter, 1, "Set one value");
q1.value = q1.otherItem.value;
assert.equal(valueChangedCallCounter, 2, "Set other value");
q1.comment = "new comment";
assert.equal(valueChangedCallCounter, 3, "Set comment to other value");
});
QUnit.test("Do not show errors in display mode", function (assert) {
var survey = twoPageSimplestSurvey();
(<Question>survey.pages[0].questions[0]).isRequired = true;
survey.mode = "display";
survey.nextPage();
assert.equal(survey.currentPage, survey.pages[1], "Can move into another page");
});
QUnit.test("Question is readOnly", function (assert) {
var survey = twoPageSimplestSurvey();
var q1 = <Question>(<Question>survey.pages[0].questions[0]);
assert.equal(q1.isReadOnly, false, "check1. question is not readonly");
q1.readOnly = true;
assert.equal(q1.isReadOnly, true, "check2. question is readonly now");
q1.readOnly = false;
survey.mode = "display";
assert.equal(q1.isReadOnly, true, "check2. question is readonly because survey in the display mode");
});
QUnit.test("Do not show required error for readOnly questions", function (assert) {
var survey = twoPageSimplestSurvey();
var page = survey.pages[0];
var q1 = <Question>(<Question>page.questions[0]);
q1.isRequired = true;
assert.equal(page.hasErrors(), true, "There is a required error");
q1.readOnly = true;
assert.equal(page.hasErrors(), false, "There is no errors, the question is readOnly");
});
QUnit.test("Do not show required error for value 0 and false, #345", function (assert) {
var survey = twoPageSimplestSurvey();
var page = survey.pages[0];
var q1 = <Question>(<Question>page.questions[0]);
q1.isRequired = true;
assert.equal(page.hasErrors(), true, "There is a required error");
survey.setValue("question1", 0)
assert.equal(q1.value, 0, "question1.value == 0");
assert.equal(page.hasErrors(), false, "There is no errors, the question value is 0");
survey.setValue("question1", false)
assert.equal(q1.value, false, "question1.value == false");
assert.equal(page.hasErrors(), false, "There is no errors, the question value is false");
survey.setValue("question1", null)
assert.equal(page.hasErrors(), true, "There is a required error, the question value is null");
});
QUnit.test("Next, Prev, IsFirst and IsLast Page and progressText", function (assert) {
var survey = new SurveyModel();
assert.equal(survey.progressText, "", "there is pages");
survey.addPage(createPageWithQuestion("Page 1"));
survey.addPage(createPageWithQuestion("Second page"));
survey.addPage(createPageWithQuestion("Third page"));
assert.equal(survey.currentPage, survey.pages[0], "Current Page is First");
assert.equal(survey.isFirstPage, true, "Current Page is First");
assert.equal(survey.isLastPage, false, "Current Page is First");
assert.equal(survey.progressText, "Page 1 of 3", "Current Page is First");
survey.nextPage();
assert.equal(survey.currentPage, survey.pages[1], "Current Page is Second");
assert.equal(survey.isFirstPage, false, "Current Page is Second");
assert.equal(survey.isLastPage, false, "Current Page is Second");
assert.equal(survey.progressText, "Page 2 of 3", "Current Page is First");
survey.nextPage();
assert.equal(survey.currentPage, survey.pages[2], "Current Page is Third");
assert.equal(survey.isFirstPage, false, "Current Page is Third");
assert.equal(survey.isLastPage, true, "Current Page is Third");
assert.equal(survey.progressText, "Page 3 of 3", "Current Page is First");
survey.prevPage();
assert.equal(survey.currentPage, survey.pages[1], "Current Page is Second");
assert.equal(survey.isFirstPage, false, "Current Page is Second");
assert.equal(survey.isLastPage, false, "Current Page is Second");
assert.equal(survey.progressText, "Page 2 of 3", "Current Page is First");
survey.prevPage();
assert.equal(survey.currentPage, survey.pages[0], "Current Page is First");
assert.equal(survey.isFirstPage, true, "Current Page is First");
assert.equal(survey.isLastPage, false, "Current Page is First");
assert.equal(survey.progressText, "Page 1 of 3", "Current Page is First");
});
QUnit.test("Next, Prev, Next", function (assert) {
var survey = new SurveyModel();
survey.addPage(createPageWithQuestion("Page 1"));
survey.addPage(createPageWithQuestion("Page 2"));
survey.addPage(createPageWithQuestion("Page 3"));
assert.equal(survey.currentPage, survey.pages[0], "Initial page is first");
survey.nextPage();
assert.equal(survey.currentPage, survey.pages[1], "After next the current page is second");
survey.prevPage();
assert.equal(survey.currentPage, survey.pages[0], "After the prev the current page is again first");
survey.nextPage();
assert.equal(survey.currentPage, survey.pages[1], "After second next the current page is second");
});
QUnit.test("Survey state", function (assert) {
var survey = new SurveyModel();
assert.equal(survey.state, "empty", "There is no a visible page");
survey.addPage(createPageWithQuestion("Page 1"));
survey.addPage(createPageWithQuestion("Page 2"));
assert.equal(survey.state, "running", "Survey is in run mode");
survey.nextPage();
assert.equal(survey.state, "running", "Survey is in run mode");
survey.completeLastPage();
assert.equal(survey.state, "completed", "Survey is completed");
});
QUnit.test("Question Creator", function (assert) {
var inst = QuestionFactory.Instance;
inst.registerQuestion("question1", (name: string) => { return new Question(name); });
inst.registerQuestion("question2", (name: string) => { return new Question(name); });
assert.equal(inst.createQuestion("question1", "Q1").name, "Q1", "Create first type of question");
assert.equal(inst.createQuestion("question2", "Q2").name, "Q2", "Create second type of question");
assert.equal(inst.createQuestion("question3", "Q3"), null, "Create unexisting type of question");
});
QUnit.test("Question Creator getAllQuestions", function (assert) {
var inst = QuestionFactory.Instance;
inst.registerQuestion("question3", (name: string) => { return new Question(name); });
inst.registerQuestion("question4", (name: string) => { return new Question(name); });
var names = inst.getAllTypes();
assert.ok(names.indexOf("question3") > -1, "contains a new type");
});
QUnit.test("Add questions to page", function (assert) {
var page = new PageModel("Page 1");
page.addNewQuestion("text", "Q1");
page.addNewQuestion("checkbox", "Q2");
assert.equal(page.questions.length, 2, "Two questions");
assert.equal(page.questions[0].getType(), "text", "Text question");
assert.equal(page.questions[1].getType(), "checkbox", "Checkbox question");
});
QUnit.test("Survey.getQuestionByName", function (assert) {
var survey = new SurveyModel();
var page = survey.addNewPage("Page 1");
page.addNewQuestion("text", "Q1");
page.addNewQuestion("checkbox", "Q2");
page = survey.addNewPage("Page 1");
page.addNewQuestion("text", "Q3");
page.addNewQuestion("checkbox", "Q4");
assert.equal(survey.getQuestionByName("Q2").name, "Q2", "find question on the first page");
assert.equal(survey.getQuestionByName("Q3").name, "Q3", "find question on the second page");
assert.equal(survey.getQuestionByName("Q0"), null, "return null");
});
QUnit.test("Survey.getPageByQuestion/getPageByElement", function (assert) {
var survey = new SurveyModel();
var page1 = survey.addNewPage("page1");
var page2 = survey.addNewPage("page2");
page1.addNewQuestion("text", "q1");
var panel1 = page1.addNewPanel("panel1");
var q2 = panel1.addNewQuestion("text", "q2");
var panel2 = panel1.addNewPanel("panel2");
var q3 = panel1.addNewQuestion("text", "q3");
var q4 = page2.addNewQuestion("text", "q4");
assert.equal(survey.getPageByQuestion(q2).name, "page1", "q1 - page1");
assert.equal(survey.getPageByQuestion(q3).name, "page1", "q3 - page1");
assert.equal(survey.getPageByQuestion(q4).name, "page2", "q4 - page2");
assert.equal(survey.getPageByElement(panel1).name, "page1", "panel1 - page1");
assert.equal(survey.getPageByElement(panel2).name, "page1", "panel2 - page1");
});
QUnit.test("Add/remove panel", function (assert) {
var survey = new SurveyModel();
var page1 = survey.addNewPage("page1");
var panel1 = page1.addNewPanel("panel1");
var q1 = panel1.addNewQuestion("text", "q1");
var panel2 = panel1.addNewPanel("panel2");
var q2 = panel2.addNewQuestion("text", "q2");
assert.equal(page1.elements.length, 1, "There is one element");
page1.removeElement(panel1);
assert.equal(page1.elements.length, 0, "There is no elements");
});
QUnit.test("Remove element from nested panel, #321", function (assert) {
var survey = new SurveyModel();
var page1 = survey.addNewPage("page1");
var panel1 = page1.addNewPanel("panel1");
var q1 = panel1.addNewQuestion("text", "q1");
assert.equal(panel1.elements.length, 1, "There is one question in the panel");
page1.removeElement(q1);
assert.equal(panel1.elements.length, 0, "There no questions in the panel");
});
QUnit.test("Add panel with questions", function (assert) {
var survey = new SurveyModel();
var page1 = survey.addNewPage("page1");
var panel1 = new PanelModel("panel1");
var q1 = panel1.addNewQuestion("text", "q1");
var panel2 = panel1.addNewPanel("panel2");
var q2 = panel2.addNewQuestion("text", "q2");
page1.addElement(panel1);
assert.equal(panel1.data, survey, "The data is set correctly in the root panel");
assert.equal(q2.survey, survey, "The survey is set correctly in the question of the nested root");
});
QUnit.test("SurveyData interface implementation", function (assert) {
var surveyData: ISurveyData;
surveyData = new SurveyModel();
assert.equal(surveyData.getValue("test1"), null, "No data");
assert.equal(surveyData.getValue("test2"), null, "No data");
surveyData.setValue("test1", 1);
surveyData.setValue("test2", "1");
assert.equal(surveyData.getValue("test1"), 1, "Has value 1");
assert.equal(surveyData.getValue("test2"), "1", "Has value '1'");
});
QUnit.test("Store question value in the survey", function (assert) {
var survey = new SurveyModel();
survey.addPage(new PageModel("Page 1"));
var question = <Question>survey.pages[0].addNewQuestion("text", "question");
assert.equal(survey.getValue("question"), null, "No value");
assert.equal(question.value, null, "No value");
question.value = "mytext";
assert.equal(survey.getValue("question"), "mytext", "set value from question");
assert.equal(question.value, "mytext", "set value from question");
survey.setValue("question", "myNewtext");
assert.equal(survey.getValue("question"), "myNewtext", "set value from survey");
assert.equal(question.value, "myNewtext", "set value from survey");
});
QUnit.test("Store comments in the survey", function (assert) {
var survey = new SurveyModel();
survey.addPage(new PageModel("Page 1"));
var question = <Question>survey.pages[0].addNewQuestion("text", "question");
assert.equal(survey.getComment("question"), "", "Comment is empty");
assert.equal(question.comment, "", "Comment is empty");
question.comment = "myComment";
assert.equal(survey.getComment("question"), "myComment", "set comment from question");
assert.equal(question.comment, "myComment", "set comment from question");
survey.setComment("question", "myNewComment");
assert.equal(survey.getComment("question"), "myNewComment", "set comment from survey");
assert.equal(question.comment, "myNewComment", "set comment from survey");
});
QUnit.test("Should set required questions before go on the next page or finish", function (assert) {
var survey = twoPageSimplestSurvey();
assert.notEqual(survey, null, "Survey is not null");
(<Question>survey.pages[0].questions[0]).isRequired = true;
assert.equal(survey.nextPage(), false, "Can not go to the next page");
assert.equal(survey.pages[0].questions[0].hasErrors(), true, "The question is not filled out.");
assert.equal(survey.pages[0].hasErrors(), true, "The page is not filled out.");
(<Question>survey.pages[0].questions[0]).value = "Test";
assert.equal(survey.nextPage(), true, "Can go to the next page");
assert.equal(survey.pages[0].questions[0].hasErrors(), false, "The question is filled out.");
assert.equal(survey.pages[0].hasErrors(), false, "The page is filled out.");
});
QUnit.test("Should not be errors after prevPage bug#151", function (assert) {
var survey = new SurveyModel();
survey.goNextPageAutomatic = true;
var page = survey.addNewPage("page1");
var question = <QuestionDropdownModel>page.addNewQuestion("dropdown", "q1");
question.choices = [1, 2, 3];
question.isRequired = true;
page = survey.addNewPage("page2");
page.addNewQuestion("text", "q2");
var errorsChangedCounter = 0;
question.errorsChangedCallback = function () { errorsChangedCounter++; };
survey.nextPage();
assert.equal(question.errors.length, 1, "The question is not filled out.");
assert.equal(errorsChangedCounter, 1, "called one time");
question.value = 1;
assert.equal(errorsChangedCounter, 2, "called second time");
assert.equal(question.errors.length, 0, "The question has not errors");
assert.equal(survey.currentPage.name, survey.pages[1].name, "Go to the next page");
survey.prevPage();
assert.equal(errorsChangedCounter, 2, "called second time");
assert.equal(question.errors.length, 0, "The question has not errors");
});
QUnit.test("Invisible required questions should not be take into account", function (assert) {
var survey = twoPageSimplestSurvey();
assert.notEqual(survey, null, "Survey is not null");
(<Question>survey.pages[0].questions[0]).isRequired = true;
assert.equal(survey.nextPage(), false, "Can not go to the next page");
survey.pages[0].questions[0].visible = false;
assert.equal(survey.nextPage(), true, "You can go to the next page now.");
});
QUnit.test("onValueChanged event", function (assert) {
var survey = twoPageSimplestSurvey();
var name = "";
var newValue = null;
var counter = 0;
survey.onValueChanged.add(function (sender: SurveyModel, options: any) {
name = options.name; newValue = options.value; counter++;
});
survey.setValue("question1", "value1");
assert.equal(name, "question1", "onValueChanged event, property name is correct");
assert.equal(newValue, "value1", "onValueChanged event, property newValue is correct");
assert.equal(counter, 1, "onValueChanged event is called one time");
(<Question>survey.pages[0].questions[0]).value = "val";
assert.equal(counter, 2, "onValueChanged event is called one time");
});
QUnit.test("onValueChanged event - do not call on equal value", function (assert) {
var survey = new SurveyModel();
var counter = 0;
survey.onValueChanged.add(function (sender: SurveyModel, options: any) { counter++; });
survey.setValue("name", 1);
assert.equal(counter, 1, "onValueChanged event is called one time");
survey.setValue("name", 1);
assert.equal(counter, 1, "1 is the same value");
survey.setValue("name", { col1: [1, { cel2: "2" }] });
assert.equal(counter, 2, "onValueChanged event is called two times");
survey.setValue("name", { col1: [1, { cel2: "2" }] });
assert.equal(counter, 2, "2, the value is the same");
survey.setValue("name", { col1: [1, { cel2: "2" }, 3] });
assert.equal(counter, 3, "onValueChanged event is called three times");
survey.setValue("name", { col1: [1, { cel2: "2" }, 3] });
assert.equal(counter, 3, "3, the value is the same");
var value = survey.getValue("name");
value.col1.push(4);
survey.setValue("name", value);
assert.equal(counter, 4, "onValueChanged event is called fourth times");
});
QUnit.test("onValueChanged event is not called on changing matrix value", function (assert) {
var survey = twoPageSimplestSurvey();
var matrixQuestion = new QuestionMatrixModel("matrix");
survey.pages[0].addQuestion(matrixQuestion);
matrixQuestion.columns = ["col1", "col2"];
matrixQuestion.rows = ["row1", "row2"];
var name = "";
var newValue = null;
var counter = 0;
survey.onValueChanged.add(function (sender: SurveyModel, options: any) {
name = options.name; newValue = options.value; counter++;
});
matrixQuestion.visibleRows[0].value = "col2";
assert.equal(counter, 1, "onValueChanged event is called one time");
assert.equal(name, "matrix", "onValueChanged event, property name is correct");
assert.deepEqual(newValue, { "row1": "col2" }, "onValueChanged event, property newValue is correct");
});
QUnit.test("onValueChanged event is not called on changing multi text value", function (assert) {
var survey = twoPageSimplestSurvey();
var multiTextQuestion = new QuestionMultipleTextModel("multitext");
survey.pages[0].addQuestion(multiTextQuestion);
multiTextQuestion.items.push(new MultipleTextItemModel("item1"));
multiTextQuestion.items.push(new MultipleTextItemModel("item2"));
var name = "";
var newValue = null;
var counter = 0;
survey.onValueChanged.add(function (sender: SurveyModel, options: any) {
name = options.name; newValue = options.value; counter++;
});
multiTextQuestion.items[1].value = "text1";
assert.equal(counter, 1, "onValueChanged event is called one time");
assert.equal(name, "multitext", "onValueChanged event, property name is correct");
assert.deepEqual(newValue, { "item2": "text1" }, "onValueChanged event, property newValue is correct");
});
QUnit.test("adding, inserting Multiple Text Item correctly", function (assert) {
var survey = twoPageSimplestSurvey();
var multiTextQuestion = new QuestionMultipleTextModel("multitext");
survey.pages[0].addQuestion(multiTextQuestion);
var item1 = new MultipleTextItemModel("item1");
var item2 = new MultipleTextItemModel("item2");
multiTextQuestion.items.push(item1);
multiTextQuestion.items.splice(0, 0, item2);
item1.value = "1";
assert.equal(item1.value, "1", "Check1. data was set correctly");
item2.value = "2";
assert.equal(item2.value, "2", "Check2. data was set correctly");
multiTextQuestion.items = [];
item1 = multiTextQuestion.addItem("item1");
item1.value = "3";
assert.equal(item1.value, "3", "Check3. data was set correctly");
multiTextQuestion.items.splice(0, 0, item2);
item2.value = "4";
assert.equal(item2.value, "4", "Check4. data was set correctly");
});
QUnit.test("Multiple Text required items", function (assert) {
var survey = twoPageSimplestSurvey();
var multiTextQuestion = new QuestionMultipleTextModel("multitext");
survey.pages[0].addQuestion(multiTextQuestion);
var item1 = multiTextQuestion.addItem("item1");
var item2 = multiTextQuestion.addItem("item2");
item1.isRequired = true;
assert.equal(item1.fullTitle, "* item1", "Add isRequired Text");
assert.equal(item2.fullTitle, "item2", "there is no isRequired Text");
assert.equal(multiTextQuestion.hasErrors(), true, "item1 is required and it is empty");
item1.value = 1;
assert.equal(multiTextQuestion.hasErrors(), false, "item1 is required and it has a value");
});
QUnit.test("onComplete event", function (assert) {
var survey = twoPageSimplestSurvey();
var counter = 0;
survey.onComplete.add(function () { counter++; });
survey.nextPage();
survey.nextPage();
survey.completeLastPage();
assert.equal(survey.state, "completed", "The survey is completed");
assert.equal(counter, 1, "onComplete calls one time");
});
QUnit.test("onVisibleChanged event", function (assert) {
var survey = twoPageSimplestSurvey();
var name = "";
var visibility = true;
var counter = 0;
survey.onVisibleChanged.add(function (sender: SurveyModel, options: any) {
name = options.name; visibility = options.visible; counter++;
});
survey.getQuestionByName("question1").visible = false;
assert.equal(name, "question1", "onVisibleChanged event, property name is correct");
assert.equal(visibility, false, "onVisibleChanged event, property visibility is correct");
assert.equal(counter, 1, "onVisibleChanged event is called one time");
survey.getQuestionByName("question1").visible = true;
assert.equal(name, "question1", "onVisibleChanged event, property name is correct");
assert.equal(visibility, true, "onVisibleChanged event, property visibility is correct");
assert.equal(counter, 2, "onVisibleChanged event is called two time");
survey.getQuestionByName("question1").visible = true;
assert.equal(counter, 2, "onVisibleChanged event is called two time");
});
QUnit.test("Question visibleIndex", function (assert) {
var survey = twoPageSimplestSurvey();
assert.equal((<Question>survey.getQuestionByName("question1")).visibleIndex, 0, "the first question");
assert.equal((<Question>survey.getQuestionByName("question3")).visibleIndex, 2, "the third question");
survey.getQuestionByName("question1").visible = false;
assert.equal((<Question>survey.getQuestionByName("question3")).visibleIndex, 1, "the third question is now visible as second");
survey.showQuestionNumbers = "off";
assert.equal((<Question>survey.getQuestionByName("question1")).visibleIndex, -1, "off:the first question");
assert.equal((<Question>survey.getQuestionByName("question2")).visibleIndex, -1, "off:the second question");
assert.equal((<Question>survey.getQuestionByName("question3")).visibleIndex, -1, "off:the third question");
survey.showQuestionNumbers = "onPage";
assert.equal((<Question>survey.getQuestionByName("question1")).visibleIndex, -1, "onPage:the first question");
assert.equal((<Question>survey.getQuestionByName("question2")).visibleIndex, 0, "onPage:the second question");
assert.equal((<Question>survey.getQuestionByName("question3")).visibleIndex, 0, "onPage:the third question");
});
QUnit.test("Question visibleIndex, add-remove questions", function (assert) {
var survey = new SurveyModel();
var page = survey.addNewPage("p1");
var q1 = new QuestionTextModel("q1");
page.elements.push(q1);
page.elements.push(new QuestionTextModel("q2"));
assert.equal((<Question>survey.getQuestionByName("q1")).visibleIndex, 0, "the first question");
assert.equal((<Question>survey.getQuestionByName("q2")).visibleIndex, 1, "the second question");
var q3 = new QuestionTextModel("q3");
page.elements.splice(0, 1, q3);
assert.equal((<Question>survey.getQuestionByName("q3")).visibleIndex, 0, "the first question");
assert.equal((<Question>survey.getQuestionByName("q2")).visibleIndex, 1, "the second question");
});
QUnit.test("showQuestionNumbers - question fullTitle", function (assert) {
var survey = twoPageSimplestSurvey();
assert.equal((<Question>survey.getQuestionByName("question1")).fullTitle, "1. question1", "the first question showQuestionNumbers=on");
assert.equal((<Question>survey.getQuestionByName("question3")).fullTitle, "3. question3", "the thrid question showQuestionNumbers=on");
survey.showQuestionNumbers = "onPage";
assert.equal((<Question>survey.getQuestionByName("question1")).fullTitle, "1. question1", "the first question showQuestionNumbers=onPage");
assert.equal((<Question>survey.getQuestionByName("question3")).fullTitle, "1. question3", "the thrid question showQuestionNumbers=onPage");
survey.showQuestionNumbers = "off";
assert.equal((<Question>survey.getQuestionByName("question1")).fullTitle, "question1", "the first question showQuestionNumbers=onPage");
assert.equal((<Question>survey.getQuestionByName("question3")).fullTitle, "question3", "the thrid question showQuestionNumbers=onPage");
});
QUnit.test("Question visibleIndex and no title question", function (assert) {
var survey = twoPageSimplestSurvey();
assert.equal((<Question>survey.getQuestionByName("question1")).visibleIndex, 0, "the first question");
assert.equal((<Question>survey.getQuestionByName("question3")).visibleIndex, 2, "the third question");
var question = new QuestionHtmlModel("html1");
survey.pages[0].addQuestion(question, 0);
assert.equal((<Question>survey.getQuestionByName("html1")).visibleIndex, -1, "html question");
assert.equal((<Question>survey.getQuestionByName("question1")).visibleIndex, 0, "the first question + html question");
assert.equal((<Question>survey.getQuestionByName("question3")).visibleIndex, 2, "the third question + html question");
});
QUnit.test("Pages visibleIndex and num", function (assert) {
var survey = twoPageSimplestSurvey();
survey.addNewPage("page 3").addNewQuestion("text", "q4");
assert.equal(survey.pages[0].visibleIndex, 0, "start:page 1");
assert.equal(survey.pages[1].visibleIndex, 1, "start:page 2");
assert.equal(survey.pages[2].visibleIndex, 2, "start:page 3");
assert.equal(survey.pages[0].num, -1, "start:page 1, num");
assert.equal(survey.pages[1].num, -1, "start:page 2, num");
assert.equal(survey.pages[2].num, -1, "start:page 3, num");
survey.showPageNumbers = true;
assert.equal(survey.pages[0].num, 1, "showPageNumbers:page 1, num");
assert.equal(survey.pages[1].num, 2, "showPageNumbers:page 2, num");
assert.equal(survey.pages[2].num, 3, "showPageNumbers:page 3, num");
survey.pages[0].visible = false;
assert.equal(survey.pages[0].visibleIndex, -1, "page[0].visible=false:page 1");
assert.equal(survey.pages[1].visibleIndex, 0, "page[0].visible=false:page 2");
assert.equal(survey.pages[2].visibleIndex, 1, "page[0].visible=false:page 3");
assert.equal(survey.pages[0].num, -1, "page[0].visible=false:page 1, num");
assert.equal(survey.pages[1].num, 1, "page[0].visible=false:page 2, num");
assert.equal(survey.pages[2].num, 2, "page[0].visible=false:page 3, num");
});
QUnit.test("Pages num", function (assert) {
var survey = twoPageSimplestSurvey();
assert.equal(survey.pages[0].num, -1, "false:the first page");
assert.equal(survey.pages[1].num, -1, "false:the second page");
survey.showPageNumbers = true;
assert.equal(survey.pages[0].num, 1, "true:the first page");
assert.equal(survey.pages[1].num, 2, "true:the second page");
});
QUnit.test("Server validation", function (assert) {
var survey = twoPageSimplestSurvey();
var serverFunction = function (options) {
if (options.data["question1"] && options.data["question1"] > 100) {
options.errors["question1"] = "Question 1 should be higher than 100";
}
options.complete();
}
survey.onServerValidateQuestions = function (sender, options) {
serverFunction(options);
};
survey.setValue("question1", 101);
survey.nextPage();
assert.equal(survey.currentPage.visibleIndex, 0, "Get server error");
survey.setValue("question1", 10);
survey.nextPage();
assert.equal(survey.currentPage.visibleIndex, 1, "No errors server error");
});
QUnit.test("onVisibleChanged call validation", function (assert) {
var survey = twoPageSimplestSurvey();
survey.onValidateQuestion.add(function (sender, options) {
if (options.name == "question1" && options.value > 100) {
options.error = "Question 1 should be higher than 100";
}
});
assert.equal(survey.isCurrentPageHasErrors, false, "There is no error if the value is empty");
survey.setValue("question1", 1);
assert.equal(survey.isCurrentPageHasErrors, false, "the value is less than 100");
survey.setValue("question1", 101);
assert.equal(survey.isCurrentPageHasErrors, true, "the value is more than 100, no errors");
});
QUnit.test("isCurrentPageHasErrors, required question in the invisible panel, #325", function (assert) {
var survey = twoPageSimplestSurvey();
var panel = survey.pages[0].addNewPanel("panel");
var requiredQuestion = <QuestionTextModel>panel.addNewQuestion("text", "requriedQuestion");
requiredQuestion.isRequired = true;
assert.equal(survey.isCurrentPageHasErrors, true, "requiredQuestion value is empty");
panel.visible = false;
assert.equal(survey.isCurrentPageHasErrors, false, "requiredQuestion value is empty, but the parent panel is invisible");
});
QUnit.test("Page visibility", function (assert) {
var page = new PageModel("page");
assert.equal(page.isVisible, false, "page is invisible if there is no questions in it");
page.addNewQuestion("text", "q1");
assert.equal(page.isVisible, true, "there is one question");
page.visible = false;
assert.equal(page.isVisible, false, "we made the page invisible");
page.visible = true;
assert.equal(page.isVisible, true, "we made the page visible");
page.questions[0].visible = false;
assert.equal(page.isVisible, false, "there is no visible questions on the page");
page.questions[0].visible = true;
assert.equal(page.isVisible, true, "we have made the question visible again");
});
QUnit.test("Survey visiblePages and start using them", function (assert) {
var survey = twoPageSimplestSurvey();
assert.equal(survey.visiblePages.length, 2, "All pages are visible");
assert.equal(survey.currentPage.name, "Page 1", "the first page is current");
survey.pages[0].visible = false;
assert.equal(survey.visiblePages.length, 1, "The first page becomes invisible");
assert.equal(survey.currentPage.name, "Page 2", "the second page is current, because the first is invisible");
});
QUnit.test("Survey visiblePages, make second and third invisbile and go the last page on next", function (assert) {
var survey = twoPageSimplestSurvey();
survey.currentPage = survey.pages[0];
survey.addNewPage("Page 3").addNewQuestion("text", "p3q1");
survey.addNewPage("Page 4").addNewQuestion("text", "p4q1");
survey.pages[1].visible = false;
survey.pages[2].questions[0].visible = false;
survey.nextPage();
assert.equal(survey.currentPage.name, "Page 4", "Bypass two invisible pages");
});
QUnit.test("Visible trigger test", function (assert) {
var survey = twoPageSimplestSurvey();
var trigger = new SurveyTriggerVisible();
survey.triggers.push(trigger);
trigger.name = "question1";
trigger.value = "Hello";
trigger.pages = ["Page 2"];
trigger.questions = ["question2"];
survey.setValue("question1", "H");
assert.equal(survey.getQuestionByName("question2").visible, false, "It is invisible now");
assert.equal(survey.pages[1].visible, false, "It is invisible now");
survey.setValue("question1", "Hello");
assert.equal(survey.getQuestionByName("question2").visible, true, "trigger makes it visible");
assert.equal(survey.pages[1].visible, true, "trigger makes it visible");
survey.setValue("question2", "He");
assert.equal(survey.getQuestionByName("question2").visible, true, "trigger should not be called");
assert.equal(survey.pages[1].visible, true, "trigger should not be called");
});
QUnit.test("Complete trigger test", function (assert) {
var survey = twoPageSimplestSurvey();
var trigger = new SurveyTriggerComplete();
survey.triggers.push(trigger);
trigger.name = "question1";
trigger.value = "Hello";
survey.setValue("question1", "H");
assert.equal(survey.state, "running");
survey.setValue("question1", "Hello");
assert.equal(survey.state, "running");
survey.nextPage();
assert.equal(survey.state, "completed");
});
QUnit.test("Value trigger test", function (assert) {
var survey = twoPageSimplestSurvey();
var trigger = new SurveyTriggerSetValue();
survey.triggers.push(trigger);
trigger.name = "question1";
trigger.value = "Hello";
trigger.setToName = "name1";
trigger.setValue = "val1";
assert.equal(survey.getValue("name1"), null, "value is not set");
survey.setValue("question1", "Hello");
assert.equal(survey.getValue("name1"), "val1", "value is set");
});
QUnit.test("String format", function (assert) {
var strResult = surveyLocalization.getString("textMinLength")["format"](10);
assert.equal(strResult, "Please enter at least 10 characters.", "The format string is working");
});
QUnit.test("Serialize email validator", function (assert) {
var validator = new EmailValidator();
var json = new JsonObject().toJsonObject(validator);
assert.ok(json, "Convert to Json Successful");
var newValidator = {};
new JsonObject().toObject(json, newValidator);
assert.ok(newValidator, "Convert from Json Successful");
});
QUnit.test("pre process title", function (assert) {
var survey = twoPageSimplestSurvey();
survey.data = { name: "John" };
survey.title = "Hello {name}";
assert.equal(survey.processedTitle, "Hello John", "process survey title correctly");
survey.pages[0].title = "Page {PageNo} from {PageCount}.";
assert.equal(survey.pages[0].processedTitle, "Page 1 from 2.");
survey.pages[0].addNewQuestion("text", "email");
survey.setValue("email", "[email protected]");
survey.setVariable("var1", "[it is var1]");
survey.setValue("val1", "[it is val1]");
survey.completedHtml = "<div>Your e-mail: <b>{email}</b>{var1}{val1}</div>";
assert.equal(survey.processedCompletedHtml, "<div>Your e-mail: <b>[email protected]</b>[it is var1][it is val1]</div>");
});
QUnit.test("pre process completedHtml nested properties and arrays", function (assert) {
var survey = new SurveyModel();
var page = survey.addNewPage("page1");
var multipleText = new QuestionMultipleTextModel("mt");
multipleText.addItem("t1");
multipleText.addItem("t2");
page.addQuestion(multipleText);
var dynamicMatrix = new QuestionMatrixDynamicModel("matrix");
dynamicMatrix.addColumn("col1");
dynamicMatrix.addColumn("col2");
dynamicMatrix.addColumn("col3");
page.addQuestion(dynamicMatrix);
multipleText.value = { t2: "Year" };
dynamicMatrix.value = [{ col1: 1 }, {col2: 2017}];
survey.completedHtml = "{mt.t2}:{matrix[1].col2}";
assert.equal(survey.processedCompletedHtml, "Year:2017");
});
QUnit.test("question fullTitle", function (assert) {
var survey = twoPageSimplestSurvey();
var question = <Question>survey.pages[0].questions[1];
question.title = "My Title";
assert.equal(question.fullTitle, "2. My Title");
question.isRequired = true;
assert.equal(question.fullTitle, "2. * My Title");
survey.questionStartIndex = "100";
assert.equal(question.fullTitle, "101. * My Title");
survey.questionStartIndex = "A";
assert.equal(question.fullTitle, "B. * My Title");
survey.questionTitleTemplate = "{no}) {title} ({require})";
assert.equal(question.fullTitle, "B) My Title (*)");
});
QUnit.test("clearInvisibleValues", function (assert) {
var survey = twoPageSimplestSurvey();
survey.clearInvisibleValues = true;
var question1 = <Question>survey.pages[0].questions[0];
question1.value = "myValue";
var question2 = <Question>survey.pages[0].questions[1];
question2.value = "myValue";
question1.visible = false;
survey.doComplete();
assert.equal(question1.value, null, "Clear value of an invisible question");
assert.equal(question2.value, "myValue", "Keep value of a visible question");
});
QUnit.test("clearInvisibleValues - comments and other values, #309", function (assert) {
var survey = new SurveyModel();
var page = survey.addNewPage("p1");
var q1 = <QuestionDropdownModel>page.addNewQuestion("dropdown", "q1");
q1.hasOther = true;
var q2 = <QuestionTextModel>page.addNewQuestion("text", "q2");
q2.hasComment = true;
var q3 = <QuestionTextModel>page.addNewQuestion("text", "q3");
survey.clearInvisibleValues = true;
q1.value = q1.otherItem.value;
q1.comment = "comment1";
q2.value = "val2";
q2.comment = "comment2";
q3.value = "val3";
q1.visible = false;
q2.visible = false;
assert.notDeepEqual(survey.data, {"q3": "val3"}, "There are many vlues yet");
survey.doComplete();
assert.deepEqual(survey.data, {"q3": "val3"}, "There should be one value only");
});
QUnit.test("Do not store others value if others is not selected, #311", function (assert) {
var survey = new SurveyModel();
var page = survey.addNewPage("p1");
var q1 = <QuestionDropdownModel>page.addNewQuestion("dropdown", "q1");
q1.hasOther = true;
q1.value = q1.otherItem.value;
q1.comment = "comment1";
q1.value = 1;
assert.notDeepEqual(survey.data, {"q1": 1}, "There is a comment yet");
survey.doComplete();
assert.deepEqual(survey.data, {"q1": 1}, "There no comment");
});
QUnit.test("merge values", function (assert) {
class MySurvey extends SurveyModel {
constructor() {
super();
}
public doMergeValues(src: any, dest: any) {
super.mergeValues(src, dest);
}
}
var survey = new MySurvey();
var dest = {};
survey.doMergeValues({ val: 1 }, dest);
assert.deepEqual(dest, { val: 1 });
survey.doMergeValues({ val2: { val1: "str" } }, dest);
assert.deepEqual({ val: 1, val2: { val1: "str" } }, dest);
survey.doMergeValues({ val2: { val2: 2 } }, dest);
assert.deepEqual({ val: 1, val2: { val1: "str", val2: 2 } }, dest);
});
QUnit.test("Several questions in one row", function (assert) {
var page = new PageModel();
for (var i = 0; i < 10; i++) page.addNewQuestion("text", "q" + (i + 1));
assert.equal(page.rows.length, 10, "10 rows for each question");
page = new PageModel();
for (var i = 0; i < 10; i++) page.addNewQuestion("text", "q" + (i + 1));
page.questions[0].startWithNewLine = false;
assert.equal(page.rows.length, 10, "still 10 rows for each question");
assert.equal(page.rows[0].questions[0].renderWidth, "100%", "the render width is 100%");
page = new PageModel();
for (var i = 0; i < 10; i++) page.addNewQuestion("text", "q" + (i + 1));
for (var i = 0; i < 10; i++) {
page.questions[i].startWithNewLine = i % 2 == 0;
}
assert.equal(page.rows.length, 5, "every second has startWithNewLine equals false, there 5 rows now");
for (var i = 0; i < 5; i++) {
assert.equal(page.rows[i].questions.length, 2, "two questions for every row");
assert.equal(page.rows[i].questions[0].renderWidth, "50%", "the render width is 50%");
assert.equal(page.rows[i].questions[0].rightIndent, 1, "the indent is 1");
assert.equal(page.rows[i].questions[1].renderWidth, "50%", "the render width is 50%");
assert.equal(page.rows[i].questions[1].rightIndent, 0, "the indent is 0");
}
});
QUnit.test("test goNextPageAutomatic property", function (assert) {
var survey = twoPageSimplestSurvey();
var dropDownQ = <QuestionDropdownModel>survey.pages[1].addNewQuestion("dropdown", "question5");
dropDownQ.choices = [1, 2, 3];
dropDownQ.hasOther = true;
survey.goNextPageAutomatic = true;
assert.equal(survey.currentPage.name, survey.pages[0].name, "the first page is default page");
survey.setValue("question1", 1);
survey.setValue("question2", 2);
assert.equal(survey.currentPage.name, survey.pages[1].name, "go to the second page automatically");
(<Question>survey.currentPage.questions[0]).value = "3";
(<Question>survey.currentPage.questions[1]).value = "4";
dropDownQ.value = dropDownQ.otherItem.value;
assert.equal(survey.currentPage.name, survey.pages[1].name, "stay on the second page");
assert.notEqual(survey.state, "completed", "survey is still running");
dropDownQ.comment = "other value";
assert.equal(survey.state, "completed", "complete the survey");
});
QUnit.test("test goNextPageAutomatic after errors", function (assert) {
var survey = twoPageSimplestSurvey();
survey.goNextPageAutomatic = true;
(<Question>survey.getQuestionByName("question2")).isRequired = true;
assert.equal(survey.currentPage.name, survey.pages[0].name, "the first page is default page");
survey.setValue("question1", 1);
survey.nextPage();
assert.equal(survey.currentPage.name, survey.pages[0].name, "we are still on the first page. There are errors.");
survey.setValue("question2", 2);
assert.equal(survey.currentPage.name, survey.pages[1].name, "go to the second page automatically");
});
QUnit.test("goNextPageAutomatic: should not work for complex questions like matrix, checkbox, multiple text", function (assert) {
var questions = [];
questions.push({ question: new QuestionCheckboxModel("check"), auto: false, value: [1] });
questions.push({ question: new QuestionRadiogroupModel("radio"), auto: true, value: 1 });
questions.push({ question: new QuestionDropdownModel("dropdown"), auto: true, value: 1 });
questions.push({ question: new QuestionCommentModel("comment"), auto: false, value: "1" });
questions.push({ question: new QuestionFileModel("file"), auto: false, value: "1" });
questions.push({ question: new QuestionFileModel("html"), auto: false, value: null });
var matrix = new QuestionMatrixModel("matrix");
matrix.rows = ["row1", "row2"];
matrix.columns = ["col1", "col2"];
questions.push({ question: matrix, auto: false, value: { "row1": "col1" } });
questions.push({ question: matrix, auto: true, value: { "row1": "col1", "row2": "col1" } });
var dropDownMatrix = new QuestionMatrixDropdownModel("matrixdropdown");
dropDownMatrix.addColumn("col1");
dropDownMatrix.rows = ["row1", "row2"];
questions.push({ question: dropDownMatrix, auto: false, value: { "row1": { "col1": 1 } } });
questions.push({ question: dropDownMatrix, auto: true, value: { "row1": { "col1": 1 }, "row2": { "col1": 2 } } });
var dynamicMatrix = new QuestionMatrixDynamicModel("matrixdynamic");
dynamicMatrix.addColumn("col1");
dynamicMatrix.rowCount = 2;
questions.push({ question: dynamicMatrix, auto: false, value: [{ "col1": 1 }] });
questions.push({ question: dynamicMatrix, auto: false, value: [{ "col1": 1 }, { "col1": 1 }] });
var multipleText = new QuestionMultipleTextModel("multitext");
multipleText.addItem("t1");
multipleText.addItem("t2");
questions.push({ question: multipleText, auto: false, value: { t1: "1" } });
questions.push({ question: multipleText, auto: true, value: { t1: "1", t2: "2" } });
questions.push({ question: new QuestionRatingModel("rating"), auto: true, value: 1 });
questions.push({ question: new QuestionTextModel("text"), auto: true, value: "1" });
var pageIndex = 0;
for (var i = 0; i < questions.length; i++) {
var q = questions[i];
var survey = new SurveyModel();
var page = survey.addNewPage("firstpage");
page.addQuestion(q.question);
survey.goNextPageAutomatic = true;
if (q.value) {
q.question.value = q.value;
}
var state = q.auto ? "completed" : "running";
assert.equal(survey.state, state, "goNextPageAutomatic is incorrect for question: " + q.question.name);
}
});
QUnit.test("goNextPageAutomatic bug #200: https://github.com/surveyjs/surveyjs/issues/200", function (assert) {
var survey = new SurveyModel();
var page = survey.addNewPage("page1");
page.addNewQuestion("html", "q1");
var q2 = <QuestionDropdownModel>page.addNewQuestion("dropdown", "q2");
q2.choices = [1, 2, 3];
page = survey.addNewPage("page2");
page.addNewQuestion("text", "q3");
survey.goNextPageAutomatic = true;
(<Question>survey.getQuestionByName("q2")).value = 1;
assert.equal(survey.currentPage.name, survey.pages[1].name, "go to the next page");
});
QUnit.test("goNextPageAutomatic and clearInvisibleValues bug #252: https://github.com/surveyjs/surveyjs/issues/252", function (assert) {
var survey = new SurveyModel();
var page = survey.addNewPage("page1");
var q1 = <QuestionDropdownModel>page.addNewQuestion("dropdown", "q1");
q1.choices = [1, 2, 3];
var q2 = <QuestionDropdownModel>page.addNewQuestion("dropdown", "q2");
q2.visible = false;
q2.value = 1;
survey.goNextPageAutomatic = true;
survey.clearInvisibleValues = true;
(<Question>survey.getQuestionByName("q1")).value = 1;
assert.equal(survey.state, "completed");
});
QUnit.test("isNavigationButtonsShowing", function (assert) {
var survey = twoPageSimplestSurvey();
assert.equal(survey.isNavigationButtonsShowing, true, "by default buttons are shown");
survey.setDesignMode(true);
assert.equal(survey.isNavigationButtonsShowing, false, "do not show buttons at design time");
survey.setDesignMode(false);
assert.equal(survey.isNavigationButtonsShowing, true, "by default buttons are shown");
survey.showNavigationButtons = false;
assert.equal(survey.isNavigationButtonsShowing, false, "showNavigationButtons = false");
survey.pages[0].navigationButtonsVisibility = "show";
assert.equal(survey.isNavigationButtonsShowing, true, "navigationButtonsVisibility = 'show' && showNavigationButtons = false");
survey.showNavigationButtons = true;
survey.pages[0].navigationButtonsVisibility = "hide";
assert.equal(survey.isNavigationButtonsShowing, false, "navigationButtonsVisibility = 'hide' && showNavigationButtons = true");
survey.showNavigationButtons = true;
survey.pages[0].navigationButtonsVisibility = "inherit";
assert.equal(survey.isNavigationButtonsShowing, true, "navigationButtonsVisibility = 'inherit' && showNavigationButtons = true");
});
QUnit.test("simple condition test", function (assert) {
var survey = new SurveyModel({
pages: [{ name: "page1",
questions: [
{ type: "checkbox", name: "q1", choices: ["yes", "no"] },
{ type: "checkbox", name: "q2", choices: ["yes", "no"] }]
}, { name : "page2", visibleIf: "{q1} = 'yes' or {q2} = 'no'",
questions: [
{ type: "text", name: "q3", visibleIf: "{q1} = 'yes' and {q2} = 'no'", },
{ type: "text", name: "q4" }]
}