-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathcaseManagement.js
More file actions
818 lines (669 loc) · 34.5 KB
/
caseManagement.js
File metadata and controls
818 lines (669 loc) · 34.5 KB
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
/* eslint-disable no-unused-expressions */
import chai from "chai";
import $ from "jquery";
import _ from "underscore";
import util from "tests/utils";
import xmlLib from "vellum/xml";
import BASELINE_XML from "static/caseManagement/baseline.xml";
import BASELINE_NO_MAPPING_XML from "static/caseManagement/baseline_no_mapping_block.xml";
import MULTIPLE_PROPERTIES_XML from "static/caseManagement/multiple_properties.xml";
import EXTRA_QUESTION_ATTRS_XML from "static/caseManagement/extra_question_attrs.xml";
import PROPERTY_CONFLICT_XML from "static/caseManagement/property_conflict.xml";
import PROPERTY_CONFLICT_DELETED_XML from "static/caseManagement/property_conflict_deleted.xml";
import GROUP_MAPPINGS_XML from "static/caseManagement/group_mappings.xml";
import INVALID_MAPPING_XML from "static/caseManagement/invalid_mapping.xml";
const assert = chai.assert;
const call = util.call;
function getMappingAndQuestionElementsFromXML(xml) {
const $xml = xmlLib.parseXML(xml);
const mappings = $xml.find("vellum\\:case_mappings > mapping");
const mappedQuestions = mappings.find("question");
return [mappings, mappedQuestions];
}
function getCaseManagementSection() {
const propertiesPane = $(".fd-content-right");
return propertiesPane.find('fieldset[data-slug="caseManagement"]');
}
const CASE_PROPERTY_WIDGET_TYPE = "select";
describe("The Case Management plugin", function () {
before(function (done) {
util.init({
features: {
},
core: {
onReady: function () {
assert(this.isPluginEnabled("caseManagement"),
"caseManagement plugin should be enabled");
done();
}
}
});
});
beforeEach(() => {
const data = $("#vellum").vellum("get").data.caseManagement;
if (data) {
data.baseline = {};
delete data.caseMappings;
}
});
it("preserves case mapping between loading and writing XML", function () {
util.loadXML(BASELINE_XML);
const xml = call("createXML", {withCaseMappings: true});
util.assertXmlEqual(xml, BASELINE_XML);
});
it("preserves case mapping with deleted property conflict between loading and writing XML", function () {
util.loadXML(PROPERTY_CONFLICT_DELETED_XML);
const xml = call("createXML", {withCaseMappings: true});
util.assertXmlEqual(xml, PROPERTY_CONFLICT_DELETED_XML);
});
it("outputs an empty mapping block if form lacks mappings data", function () {
util.loadXML(BASELINE_NO_MAPPING_XML);
const xml = call("createXML", {withCaseMappings: true});
const $xml = xmlLib.parseXML(xml);
const mappings = $xml.find("vellum\\:case_mappings");
// ensure the case mappings block is present, but that it contains no concrete mappings
assert.equal(mappings.length, 1);
assert.equal(mappings.children().length, 0);
});
it("should display the case management section for generic fields", function () {
util.loadXML("");
util.addQuestion("Text", "text");
const caseManagementSection = getCaseManagementSection();
assert.exists(caseManagementSection[0]);
});
it("should hide the case management section for labels", function () {
util.loadXML("");
util.addQuestion("Trigger", "label");
assert.notExists(getCaseManagementSection()[0]);
});
it("should hide the case management section for normal groups" , function () {
util.loadXML("");
util.addQuestion("Group", "normal_group");
assert.notExists(getCaseManagementSection()[0]);
});
it("should hide the case management section for repeat groups", function () {
util.loadXML("");
util.addQuestion("Repeat", "repeat_group");
assert.notExists(getCaseManagementSection()[0]);
});
it("should hide the case management section for questions in repeat groups", function () {
util.loadXML("");
const repeat = util.addQuestion("Repeat", "repeat_group");
const group = util.addQuestion("Group", "group");
const text = util.addQuestion("Text", "text");
util.clickQuestion(text);
assert.equal(text.parentMug, group);
assert.equal(group.parentMug, repeat);
assert.notExists(getCaseManagementSection()[0]);
});
it("should hide the case management section for question lists", function () {
util.loadXML("");
util.addQuestion("FieldList", "question_list");
assert.notExists(getCaseManagementSection()[0]);
});
it("should show the saved case property data", function () {
util.loadXML(BASELINE_XML);
const question1 = call("getMugByPath", "/data/question1");
util.clickQuestion(question1);
const caseManagementSection = getCaseManagementSection();
const caseProperty = caseManagementSection.find(CASE_PROPERTY_WIDGET_TYPE);
assert.equal(caseProperty.val(), "one");
});
it("should show the first property when multiple exist", function () {
// and disable the select, display warning
util.loadXML(MULTIPLE_PROPERTIES_XML);
const question1 = call("getMugByPath", "/data/question1");
util.clickQuestion(question1);
const caseManagementSection = getCaseManagementSection();
const casePropertySelect = caseManagementSection.find(CASE_PROPERTY_WIDGET_TYPE);
// fetching by selected option, because jquery does not return disabled select values
const selectedOption = casePropertySelect.find("option:checked");
const messages = casePropertySelect.find("~ .messages");
assert.isTrue(casePropertySelect.prop("disabled"));
// this question should be mapped to both 'one' and 'two', but since 'one' is first, that is expected
assert.equal(selectedOption.val(), "one");
assert.include(messages.text(),
"This question is used to update multiple case properties"
);
// but this is not an error state, so ensure that the mug is still valid
assert.isTrue(util.isTreeNodeValid(question1));
});
it("should disable case property selector with multiple properties after save", function () {
util.loadXML(BASELINE_XML);
const question1 = call("getMugByPath", "/data/question1");
util.clickQuestion(question1);
const select = getCaseManagementSection().find(CASE_PROPERTY_WIDGET_TYPE);
assert.isFalse(select.prop("disabled"), "question1 case property select should be enabled");
util.call("onFormSave", {"caseManagement": {"mappings": {
"one": [{"question_path": "/data/question1"}],
"two": [{"question_path": "/data/question1"}],
}}});
assert.isTrue(select.prop("disabled"),
"multiple properties: question1 case property select should be disabled");
});
it("should enable case property selector with one property after save", function () {
util.loadXML(MULTIPLE_PROPERTIES_XML);
const question1 = call("getMugByPath", "/data/question1");
util.clickQuestion(question1);
const select = getCaseManagementSection().find(CASE_PROPERTY_WIDGET_TYPE);
assert.isTrue(select.prop("disabled"),
"multiple properties: question1 case property select should be disabled");
util.call("onFormSave", {"caseManagement": {"mappings": {
"one": [{"question_path": "/data/question1"}],
}}});
assert.isFalse(select.prop("disabled"), "question1 case property select should be enabled");
});
it("should save the case property to the XML", function () {
util.loadXML("");
util.addQuestion("Text", "question");
// set the value
const caseManagementSection = getCaseManagementSection();
const casePropertySelect = caseManagementSection.find(CASE_PROPERTY_WIDGET_TYPE);
casePropertySelect.val("one").trigger("change");
const xml = call("createXML", {withCaseMappings: true});
const [mappings, mappedQuestions] = getMappingAndQuestionElementsFromXML(xml);
assert.equal(mappings.length, 1);
assert.equal(mappings.attr("property"), "one");
assert.equal(mappedQuestions.length, 1);
assert.equal(mappedQuestions.attr("question_path"), "/data/question");
});
it("should modify the existing mapping", function () {
util.loadXML(BASELINE_XML);
const question1 = call("getMugByPath", "/data/question1");
util.clickQuestion(question1);
// set the value
const caseManagementSection = getCaseManagementSection();
const casePropertySelect = caseManagementSection.find(CASE_PROPERTY_WIDGET_TYPE);
casePropertySelect.val("two").trigger("change");
const xml = call("createXML", {withCaseMappings: true});
const [mappings, mappedQuestions] = getMappingAndQuestionElementsFromXML(xml);
assert.equal(mappings.length, 1);
assert.equal(mappings.attr("property"), "two");
assert.equal(mappedQuestions.length, 1);
assert.equal(mappedQuestions.attr("question_path"), "/data/question1");
});
it("should preserve additional question attributes", function () {
util.loadXML(EXTRA_QUESTION_ATTRS_XML);
const question1 = call("getMugByPath", "/data/question1");
util.clickQuestion(question1);
// set the value
const caseManagementSection = getCaseManagementSection();
const casePropertySelect = caseManagementSection.find(CASE_PROPERTY_WIDGET_TYPE);
casePropertySelect.val("two").trigger("change");
const xml = call("createXML", {withCaseMappings: true});
const [mappings, mappedQuestions] = getMappingAndQuestionElementsFromXML(xml);
assert.equal(mappings.length, 1);
assert.equal(mappings.attr("property"), "two");
assert.equal(mappedQuestions.length, 1);
assert.equal(mappedQuestions.attr("question_path"), "/data/question1");
assert.equal(mappedQuestions.attr("update_mode"), "edit");
});
it("should load reinstated mapping from baseline", function () {
util.loadXML(EXTRA_QUESTION_ATTRS_XML);
const data = $("#vellum").vellum("get").data.caseManagement;
data.baseline = JSON.parse(JSON.stringify(data.caseMappings));
const mug = call("getMugByPath", "/data/question1");
const select = getCaseManagementSection().find(CASE_PROPERTY_WIDGET_TYPE);
select.val('').trigger('change');
assert(!mug.p.caseProperty, `expected ${mug.p.caseProperty} to be false-ish`);
select.val('one').trigger('change');
assert.equal(mug.p.caseProperty, "one");
const xml = call("createXML", {withCaseMappings: true});
assert.include(xml, ' update_mode="edit"', xml);
});
it("should display a dropdown of potential case management properties", function () {
util.loadXML("");
util.addQuestion("Text", "question1");
const caseManagementSection = getCaseManagementSection();
const casePropertySelect = caseManagementSection.find(CASE_PROPERTY_WIDGET_TYPE);
const displayedOptions = [];
$.each(casePropertySelect.prop("options"), function(i) {
const $opt = $(this);
displayedOptions.push($opt.text());
});
assert.deepEqual(displayedOptions, ["name", "one", "three", "two", ""]);
});
it("should display a warning when multiple questions are saving to the same case property", function () {
util.loadXML("");
const question1 = util.addQuestion("Text", "question1");
const question2 = util.addQuestion("Text", "question2");
// set the case property on value 1, verify no warning
util.clickQuestion(question1);
let caseManagementSection = getCaseManagementSection();
let casePropertySelect = caseManagementSection.find(CASE_PROPERTY_WIDGET_TYPE);
casePropertySelect.val("one").trigger("change");
// verify no warning
assert.isTrue(util.isTreeNodeValid(question1));
// set the case property on value 2, now verify that this question displays a warning
// and that the warning icon is next to both icons in the tree
util.clickQuestion(question2);
caseManagementSection = getCaseManagementSection();
casePropertySelect = caseManagementSection.find(CASE_PROPERTY_WIDGET_TYPE);
casePropertySelect.val("one").trigger("change");
let messages = casePropertySelect.find("~ .messages");
assert.isFalse(util.isTreeNodeValid(question1));
assert.isFalse(util.isTreeNodeValid(question2));
const expectedMessage = (
"\"one\" is assigned by another question. You can still save the form, " +
"but will need to have only one question for any case property in order to " +
"build the application"
);
assert.include(messages.text(), expectedMessage);
// click back to the first question, verify that the warning is visible
util.clickQuestion(question1);
messages = casePropertySelect.find("~ .messages");
assert.include(messages.text(), expectedMessage);
assert.isFalse(util.isTreeNodeValid(question1));
});
it("should display a warning when loading xml with a property conflict", function () {
util.loadXML(PROPERTY_CONFLICT_XML);
const question1 = call("getMugByPath", "/data/question1");
const question2 = call("getMugByPath", "/data/question2");
assert.isFalse(util.isTreeNodeValid(question1));
assert.isFalse(util.isTreeNodeValid(question2));
util.clickQuestion(question1);
const caseManagementSection = getCaseManagementSection();
const casePropertySelect = caseManagementSection.find(CASE_PROPERTY_WIDGET_TYPE);
const messages = casePropertySelect.find("~ .messages");
assert.equal(messages.children().length, 1);
});
it("should clear the conflict warning when one of the affected questions changes its property",
function () {
util.loadXML("");
const question1 = util.addQuestion("Text", "question1");
const question2 = util.addQuestion("Text", "question2");
// set the case property on value 1
util.clickQuestion(question1);
let caseManagementSection = getCaseManagementSection();
let casePropertySelect = caseManagementSection.find(CASE_PROPERTY_WIDGET_TYPE);
casePropertySelect.val("one").trigger("change");
// set the case property on value 2
util.clickQuestion(question2);
caseManagementSection = getCaseManagementSection();
casePropertySelect = caseManagementSection.find(CASE_PROPERTY_WIDGET_TYPE);
casePropertySelect.val("one").trigger("change");
// click back to the first question, verify that the warning is visible
util.clickQuestion(question1);
caseManagementSection = getCaseManagementSection();
casePropertySelect = caseManagementSection.find(CASE_PROPERTY_WIDGET_TYPE);
casePropertySelect.val("two").trigger("change");
const messages = casePropertySelect.find("~ .messages");
assert.equal(messages.children().length, 0); // no messages
assert.isTrue(util.isTreeNodeValid(question1));
assert.isTrue(util.isTreeNodeValid(question2));
});
it("should preserve mappings when a question is renamed", function () {
util.loadXML(PROPERTY_CONFLICT_XML);
const question1 = call("getMugByPath", "/data/question1");
question1.p.nodeID = "question3";
const vellum = $("#vellum").vellum("get");
const caseManagementData = vellum.data.caseManagement;
const questionPaths = caseManagementData.caseMappings.one.map(
questionObj => questionObj.question_path);
assert.sameOrderedMembers(questionPaths, ['/data/question3', '/data/question2']);
assert.sameOrderedMembers(caseManagementData.caseMappingsByQuestion['/data/question3'], ['one']);
assert.notExists(caseManagementData.caseMappingsByQuestion['/data/question1']);
});
it("should display a warning on load xml with a conflicting delete", function () {
util.loadXML(PROPERTY_CONFLICT_DELETED_XML);
const mug = call("getMugByPath", "/data/question1");
assert.isFalse(util.isTreeNodeValid(mug), "question1 should not be valid");
util.clickQuestion(mug);
const select = getCaseManagementSection().find(CASE_PROPERTY_WIDGET_TYPE);
const messages = select.find("~ .messages");
assert.include(messages.text(), "mapping was concurrently changed and deleted");
assert.equal(messages.children().length, 1);
});
it("should enable save button on drop conflicting delete message", function () {
const vellum = $("#vellum").vellum("get");
util.loadXML(PROPERTY_CONFLICT_DELETED_XML);
vellum.data.core.saveButton.setState("saved");
const mug = call("getMugByPath", "/data/question1");
util.clickQuestion(mug);
assert.equal(vellum.data.core.saveButton.state, 'saved');
mug.dropMessage("caseProperty", "mug-caseProperty-conflicting-delete");
assert.equal(vellum.data.core.saveButton.state, "save", "save button should be enabled");
assert.isTrue(util.isTreeNodeValid(mug), "Unexpected error: " + util.getMessages(mug));
});
it("should dismiss conflicting delete message on change case property", function () {
util.loadXML(PROPERTY_CONFLICT_DELETED_XML);
const mug = call("getMugByPath", "/data/question1");
util.clickQuestion(mug);
const select = getCaseManagementSection().find(CASE_PROPERTY_WIDGET_TYPE);
select.val("two").trigger("change");
assert.isTrue(util.isTreeNodeValid(mug), "Unexpected error: " + util.getMessages(mug));
// should not include conflicting_delete attribute in XML
const xml = call("createXML", {withCaseMappings: true});
assert.notInclude(xml, 'conflicting_delete', xml);
});
it("should show conflicting delete message after save introducing conflict", function () {
util.loadXML(BASELINE_XML);
const mug = call("getMugByPath", "/data/question1");
util.call("onFormSave", {"caseManagement": {"mappings": {
"one": [{"question_path": "/data/question1", "conflicting_delete": true}],
}}});
assert.include(util.getMessages(mug), "mapping was concurrently changed and deleted");
// should include conflicting_delete attribute in XML
const xml = call("createXML", {withCaseMappings: true});
assert.include(xml, 'conflicting_delete', xml);
});
it("should dismiss conflicting delete message after save resolves conflict", function () {
util.loadXML(PROPERTY_CONFLICT_DELETED_XML);
const question1 = call("getMugByPath", "/data/question1");
util.clickQuestion(question1);
util.call("onFormSave", {"caseManagement": {"mappings": {
"two": [{"question_path": "/data/question2"}],
}}});
const select = getCaseManagementSection().find(CASE_PROPERTY_WIDGET_TYPE);
assert.equal(util.getMessages(question1), "", "conflicting delete should be resolved");
assert.equal(select.val(), "", "widget value should be updated");
assert.equal(question1.p.caseProperty, null, "mug value should be updated");
// should include conflicting_delete attribute in XML
const xml = call("createXML", {withCaseMappings: true});
assert.notInclude(xml, 'conflicting_delete', xml);
});
it("should not attempt to dismiss conflicting delete on unknown question", function () {
util.loadXML("");
util.call("onFormSave", {"caseManagement": {"mappings": {
"one": [
{"question_path": "/data/unknown1"},
{"question_path": "/data/unknown2"},
],
}}});
const xml = call("createXML", {withCaseMappings: true});
assert.notInclude(xml, 'conflicting_delete', xml);
});
it("should show conflicting delete message on all relevant mugs", function () {
util.loadXML(PROPERTY_CONFLICT_DELETED_XML);
util.call("onFormSave", {"caseManagement": {"mappings": {
"one": [
{"question_path": "/data/question1"},
{"question_path": "/data/question2"},
],
"two": [
{"question_path": "/data/question1"},
{"question_path": "/data/question2", "conflicting_delete": true},
],
}}});
const question1 = call("getMugByPath", "/data/question1");
const question2 = call("getMugByPath", "/data/question2");
assert.notInclude(util.getMessages(question1), "mapping was concurrently changed and deleted");
assert.include(util.getMessages(question2), "mapping was concurrently changed and deleted");
});
it("should add custom options to future dropdowns", function () {
util.loadXML();
// Verify that custom options will be included for other questions
const question1 = util.addQuestion("Text", "question1");
let caseManagementSection = getCaseManagementSection();
let casePropertySelect = caseManagementSection.find(CASE_PROPERTY_WIDGET_TYPE);
const newOption = new Option("newCaseProperty", "newCaseProperty", true, true);
casePropertySelect.append(newOption).trigger("change");
const question2 = util.addQuestion("Text", "question2");
caseManagementSection = getCaseManagementSection();
casePropertySelect = caseManagementSection.find(CASE_PROPERTY_WIDGET_TYPE);
let displayedOptions = [];
$.each(casePropertySelect.prop("options"), function(i) {
const $opt = $(this);
displayedOptions.push($opt.text());
});
assert.deepEqual(displayedOptions, ["name", "newCaseProperty", "one", "three", "two", ""]);
// Verify that custom options will be removed when no longer in use
util.clickQuestion(question1);
caseManagementSection = getCaseManagementSection();
casePropertySelect = caseManagementSection.find(CASE_PROPERTY_WIDGET_TYPE);
casePropertySelect.val(null).trigger("change");
util.clickQuestion(question2);
caseManagementSection = getCaseManagementSection();
casePropertySelect = caseManagementSection.find(CASE_PROPERTY_WIDGET_TYPE);
displayedOptions = [];
$.each(casePropertySelect.prop("options"), function(i) {
const $opt = $(this);
displayedOptions.push($opt.text());
});
assert.deepEqual(displayedOptions, ["question2", "name", "one", "three", "two", ""]);
});
it("should remove child mappings when parent group is deleted", function () {
util.loadXML(GROUP_MAPPINGS_XML);
const group1 = call("getMugByPath", "/data/group1");
const form = group1.form;
form.removeMugsFromForm([group1]);
const vellum = $("#vellum").vellum("get");
const assignedCaseProperties = Object.keys(vellum.data.caseManagement.caseMappings);
assert.notInclude(assignedCaseProperties, "one");
assert.notInclude(assignedCaseProperties, "two");
});
it("should restore mappings when a deletion is undone", function () {
util.loadXML(GROUP_MAPPINGS_XML);
const group1 = call("getMugByPath", "/data/group1");
const form = group1.form;
form.removeMugsFromForm([group1]);
form.undo();
const vellum = $("#vellum").vellum("get");
const assignedCaseProperties = Object.keys(vellum.data.caseManagement.caseMappings);
assert.include(assignedCaseProperties, "one");
assert.include(assignedCaseProperties, "two");
});
it("should preserve child group mappings when the parent is renamed", function () {
util.loadXML(GROUP_MAPPINGS_XML);
const group1 = call("getMugByPath", "/data/group1");
group1.p.nodeID = "group2";
const vellum = $("#vellum").vellum("get");
const caseManagementData = vellum.data.caseManagement;
// assert that the group1 mappings were transferred to group2
const one_paths = caseManagementData.caseMappings.one.map(question => question.question_path);
assert.sameOrderedMembers(one_paths, ["/data/group2/q1"]);
const two_paths = caseManagementData.caseMappings.two.map(question => question.question_path);
assert.sameOrderedMembers(two_paths, ["/data/group2/q2"]);
});
it("should clear warnings when deleting a question", function () {
util.loadXML(PROPERTY_CONFLICT_XML);
const question2 = call("getMugByPath", "/data/question2");
question2.form.removeMugsFromForm([question2]);
const question1 = call("getMugByPath", "/data/question1");
const CONFLICT_MSG_KEY = "mug-caseProperty-conflict";
const message = question1.messages.get("caseProperty", CONFLICT_MSG_KEY);
assert.isNull(message);
});
it("should remove invalid mappings", function () {
// this XML only contains "question1",
// but its sole mapping is for "question2"
util.loadXML(INVALID_MAPPING_XML);
const vellum = $("#vellum").vellum("get");
const caseManagementData = vellum.data.caseManagement;
assert.deepEqual(caseManagementData.caseMappings, {});
});
it("should preserve mappings for xml loaded without case mapping information", function () {
// If the user pastes old xml into the edit xml modal, we don't want them accidentally
// deleting all case mapping information
util.loadXML(BASELINE_XML); // establishes a mapping: "one"->"/data/question1"
util.loadXML(BASELINE_NO_MAPPING_XML, {preserveCaseMappings: true});
const vellum = $("#vellum").vellum("get");
const caseManagementData = vellum.data.caseManagement;
const one_paths = caseManagementData.caseMappings.one.map(question => question.question_path);
assert.sameOrderedMembers(one_paths, ["/data/question1"]);
});
it("should override existing mappings", function () {
util.loadXML(BASELINE_NO_MAPPING_XML);
util.loadXML(BASELINE_XML, {preserveCaseMappings: true});
const vellum = $("#vellum").vellum("get");
const caseManagementData = vellum.data.caseManagement;
const one_paths = caseManagementData.caseMappings.one.map(question => question.question_path);
assert.sameOrderedMembers(one_paths, ["/data/question1"]);
});
it("should replace case property spaces with underscores", function () {
util.loadXML(BASELINE_XML);
const question1 = call("getMugByPath", "/data/question1");
util.clickQuestion(question1);
const caseManagementSection = getCaseManagementSection();
const casePropertySelect = caseManagementSection.find(CASE_PROPERTY_WIDGET_TYPE);
// This will result in both "hello world" and "hello_world" being in the dropdown, but
// it doesn't appear select2 has a clean way of mimicing entering a custom option
var optionWithSpaces = new Option("hello world", "hello world", false, false);
casePropertySelect.append(optionWithSpaces);
casePropertySelect.val("hello world").trigger("change");
const vellum = $("#vellum").vellum("get");
const caseManagementData = vellum.data.caseManagement;
const assignedProperties = caseManagementData.caseMappingsByQuestion["/data/question1"];
assert.sameOrderedMembers(assignedProperties, ["hello_world"]);
});
it("should show an error for reserved case properties", function () {
util.loadXML("");
const question = util.addQuestion("Text", "question1");
let caseManagementSection = getCaseManagementSection();
let casePropertySelect = caseManagementSection.find(CASE_PROPERTY_WIDGET_TYPE);
const reservedOption = new Option("case_id", "case_id", true, true);
casePropertySelect.append(reservedOption).trigger("change");
const messages = casePropertySelect.find("~ .messages");
assert.include(messages.text(), "case_id is a reserved word");
assert.isFalse(util.isTreeNodeValid(question));
});
it("validation should not fail with empty form", function () {
util.loadXML("");
const alerts = call("preSaveValidation");
assert.equal(alerts.length, 0);
});
it("validation should fail when case name is not mapped", function () {
util.loadXML("");
util.addQuestion("Text", "one");
const alerts = call("preSaveValidation");
const msg = _(alerts).find(a => a.indexOf('missing a case name') >= 0);
assert.ok(msg, JSON.stringify(alerts));
});
describe("with unknown question path", function () {
before(function () {
util.loadXML("");
});
const args = [
[{"question_path": null}],
[{"question_path": ""}],
[{"question_path": "/data/unknown"}],
[{"question_path": "/data/unknown"}, {"question_path": ""}],
];
args.forEach(questions => {
it("should prune " + JSON.stringify(questions), function () {
util.call("onFormSave", {"caseManagement": {"mappings": {"name": questions}}});
const vellum = $("#vellum").vellum("get");
assert.deepEqual(vellum.data.caseManagement.caseMappings, {});
assert.deepEqual(vellum.data.caseManagement.caseMappingsByQuestion, {});
assert.deepEqual(vellum.data.caseManagement.baseline, {});
});
});
});
describe("should auto-assign case name to", function () {
let popover;
before(() => {
popover = call("getData").core.saveButton.ui.data('bs.popover');
// disable popover animation for deterministic hide
// otherwise next test may fail if it interacts with popovers
popover.options.animation = false;
});
after(() => {
delete popover.options.animation;
});
it("first mug", function (done) {
util.loadXML("");
const mug = util.addQuestion("Text", "first");
const save = call("getData").core.saveButton.ui;
function test() {
save.off('shown.bs.popover.test');
$(".fd-auto-assign-case-name").trigger("click");
assert.equal(mug.p.caseProperty, 'name');
done();
}
save.on('shown.bs.popover.test', test);
save.popover("show");
});
it("new mug when first mug has mapping", function (done) {
util.loadXML("");
const mug = util.addQuestion("Text", "test");
mug.p.caseProperty = 'test';
const save = call("getData").core.saveButton.ui;
function test() {
save.off('shown.bs.popover.test');
$(".fd-auto-assign-case-name").trigger("click");
const name = call("getMugByPath", "/data/case-name");
assert.equal(name.p.nodeID, 'case-name');
assert.equal(name.p.caseProperty, 'name');
done();
}
save.on('shown.bs.popover.test', test);
save.popover("show");
});
describe("new mug when first mug is a", function () {
([
"Trigger",
"Group",
"Repeat",
"SaveToCase",
"Audio",
"Image",
"Video",
"Document",
]).forEach(type => {
it(type, function (done) {
util.loadXML("");
const mug = util.addQuestion(type, "test");
const save = call("getData").core.saveButton.ui;
function test() {
save.off('shown.bs.popover.test');
$(".fd-auto-assign-case-name").trigger("click");
const name = call("getMugByPath", "/data/case-name");
assert.isUndefined(mug.p.caseProperty, type + " caseProperty should be undefined");
assert.equal(name.p.nodeID, 'case-name');
assert.equal(name.p.caseProperty, 'name');
done();
}
save.on('shown.bs.popover.test', test);
save.popover("show");
});
});
});
it("new mug with unique node ID", function (done) {
util.loadXML("");
const mug = util.addQuestion("Text", "case-name");
mug.p.caseProperty = 'test';
const save = call("getData").core.saveButton.ui;
function test() {
save.off('shown.bs.popover.test');
$(".fd-auto-assign-case-name").trigger("click");
const name = mug.form.findFirstMatchingChild(null, () => true);
assert.notEqual(name.p.nodeID, 'case-name');
assert.endsWith(name.p.nodeID, 'case-name');
assert.equal(name.p.caseProperty, 'name');
assert.equal(mug.p.caseProperty, 'test');
done();
}
save.on('shown.bs.popover.test', test);
save.popover("show");
});
});
describe("with case management disabled", function () {
const plugins = util.options.options.plugins || [];
before(function (done) {
util.init({
plugins: _(plugins).without("caseManagement"),
core: {
onReady: function () {
assert(!this.isPluginEnabled("caseManagement"),
"caseManagement plugin should not be enabled");
done();
}
},
});
});
it ("should exclude case mappings from XML", function () {
util.loadXML(BASELINE_XML); // baseline includes case mappings
const xml = call("createXML", {withCaseMappings: true});
const $xml = xmlLib.parseXML(xml);
const mappings = $xml.find("vellum\\:case_mappings");
// ensure no mappings are created in XML
assert.equal(mappings.length, 0);
});
it ("should hide the case management section", function () {
util.loadXML("");
util.addQuestion("Text", "text");
const caseManagementSection = getCaseManagementSection();
assert.notExists(caseManagementSection[0]);
});
});
});