1 /* global $, _, CRM:true */
4 describe('crmCaseType', function() {
14 var defaultAssigneeDefaultValue
;
17 beforeEach(function() {
21 module('crmCaseType');
22 module('crmJsonComparator');
23 inject(function(crmJsonComparator
) {
24 crmJsonComparator
.register(jasmine
);
28 beforeEach(inject(function(_
$controller_
, _
$compile_
, _
$httpBackend_
, _
$q_
, _
$rootScope_
, _
$timeout_
) {
29 $controller
= _
$controller_
;
30 $compile
= _
$compile_
;
31 $httpBackend
= _
$httpBackend_
;
33 $rootScope
= _
$rootScope_
;
34 $timeout
= _
$timeout_
;
37 describe('CaseTypeCtrl', function() {
38 beforeEach(function () {
44 "option_group_id": "25",
57 "option_group_id": "25",
73 "option_group_id": "28",
87 "option_group_id": "28",
104 "option_group_id": "2",
105 "label": "ADC referral",
107 "name": "ADC referral",
118 "option_group_id": "2",
119 "label": "Add Client To Case",
121 "name": "Add Client To Case",
133 "option_group_id": "2",
134 "label": "Open Case",
144 "icon": "fa-folder-open-o"
148 "option_group_id": "2",
149 "label": "Medical evaluation",
151 "name": "Medical evaluation",
166 "name_a_b": "Benefits Specialist is",
167 "label_a_b": "Benefits Specialist is",
168 "name_b_a": "Benefits Specialist",
169 "label_b_a": "Benefits Specialist",
170 "description": "Benefits Specialist",
171 "contact_type_a": "Individual",
172 "contact_type_b": "Individual",
178 "name_a_b": "Case Coordinator is",
179 "label_a_b": "Case Coordinator is",
180 "name_b_a": "Case Coordinator",
181 "label_b_a": "Case Coordinator",
182 "description": "Case Coordinator",
183 "contact_type_a": "Individual",
184 "contact_type_b": "Individual",
190 "name_a_b": "Homeless Services Coordinator is",
191 "label_a_b": "Homeless Services Coordinator is",
192 "name_b_a": "Homeless Services Coordinator",
193 "label_b_a": "Homeless Services Coordinator",
194 "description": "Homeless Services Coordinator",
195 "contact_type_a": "Individual",
196 "contact_type_b": "Individual",
202 "name_a_b": "Spouse of",
203 "label_a_b": "Spouse of",
204 "name_b_a": "Spouse of",
205 "label_b_a": "Spouse of",
206 "description": "Spousal relationship.",
207 "contact_type_a": "Individual",
208 "contact_type_b": "Individual",
212 // include one where name is different from label
215 "name_a_b": "GA123ab",
216 "label_a_b": "Guardian Angel is",
217 "name_b_a": "GA123ba",
218 "label_b_a": "Guardian Angel for",
219 "description": "Guardian Angel.",
220 "contact_type_a": "Individual",
221 "contact_type_b": "Individual",
227 // Where is this used in the tests?
228 // It seems to be in the format for the activity assignee.
233 "value": "Benefits Specialist"
237 "value": "Benefits Specialist is"
241 "value": "Case Coordinator"
245 "value": "Case Coordinator is"
255 "name": "housing_support",
256 "title": "Housing Support",
257 "description": "Help homeless individuals obtain temporary and long-term housing",
265 {"name": "Open Case", "max_instances": "1"}
269 "name": "standard_timeline",
270 "label": "Standard Timeline",
275 "status": "Completed"
278 "name": "Medical evaluation",
279 "reference_activity": "Open Case",
280 "reference_offset": "1",
281 "reference_select": "newest"
288 "name": "Homeless Services Coordinator",
295 defaultAssigneeTypes
: {
299 "option_group_id": "152",
312 "option_group_id": "152",
313 "label": "By relationship to workflow client",
315 "name": "BY_RELATIONSHIP",
325 "option_group_id": "152",
326 "label": "Specific contact",
328 "name": "SPECIFIC_CONTACT",
338 "option_group_id": "152",
339 "label": "User creating the workflow",
341 "name": "USER_CREATING_THE_CASE",
352 defaultAssigneeDefaultValue
= _
.find(apiCalls
.defaultAssigneeTypes
.values
, { is_default
: '1' });
353 scope
= $rootScope
.$new();
354 ctrl
= $controller('CaseTypeCtrl', {$scope
: scope
, apiCalls
: apiCalls
});
357 it('should load activity statuses', function() {
358 expect(scope
.activityStatuses
).toEqualData(apiCalls
.actStatuses
.values
);
361 it('should load activity types', function() {
362 expect(scope
.activityTypes
['ADC referral']).toEqualData(apiCalls
.actTypes
.values
[0]);
365 it('should store the default assignee types', function() {
366 expect(scope
.defaultAssigneeTypes
).toBe(apiCalls
.defaultAssigneeTypes
.values
);
369 it('should store the default assignee types values indexed by name', function() {
370 var defaultAssigneeTypeValues
= _
.chain(apiCalls
.defaultAssigneeTypes
.values
)
371 .indexBy('name').mapValues('value').value();
373 expect(scope
.defaultAssigneeTypeValues
).toEqual(defaultAssigneeTypeValues
);
376 it('should store the default assignee relationship type options', function() {
377 var defaultRelationshipTypeOptions
= _
.transform(apiCalls
.relTypes
.values
, function(result
, relType
) {
378 var isBidirectionalRelationship
= relType
.label_a_b
=== relType
.label_b_a
;
381 label
: relType
.label_b_a
,
382 value
: relType
.id
+ '_a_b'
385 if (!isBidirectionalRelationship
) {
387 label
: relType
.label_a_b
,
388 value
: relType
.id
+ '_b_a'
393 expect(scope
.defaultRelationshipTypeOptions
).toEqual(defaultRelationshipTypeOptions
);
396 it('addActivitySet should add an activitySet to the case type', function() {
397 scope
.addActivitySet('timeline');
398 var activitySets
= scope
.caseType
.definition
.activitySets
;
399 var newSet
= activitySets
[activitySets
.length
- 1];
400 expect(newSet
.name
).toBe('timeline_1');
401 expect(newSet
.timeline
).toBe('1');
402 expect(newSet
.label
).toBe('Timeline');
405 it('addActivitySet handles second timeline correctly', function() {
406 scope
.addActivitySet('timeline');
407 scope
.addActivitySet('timeline');
408 var activitySets
= scope
.caseType
.definition
.activitySets
;
409 var newSet
= activitySets
[activitySets
.length
- 1];
410 expect(newSet
.name
).toBe('timeline_2');
411 expect(newSet
.timeline
).toBe('1');
412 expect(newSet
.label
).toBe('Timeline #2');
415 describe('when clearing the activity\'s default assignee type values', function() {
418 beforeEach(function() {
420 default_assignee_relationship
: 1,
421 default_assignee_contact
: 2
424 scope
.clearActivityDefaultAssigneeValues(activity
);
427 it('clears the default assignee relationship for the activity', function() {
428 expect(activity
.default_assignee_relationship
).toBe(null);
431 it('clears the default assignee contact for the activity', function() {
432 expect(activity
.default_assignee_contact
).toBe(null);
436 describe('when adding a new activity to a set', function() {
439 beforeEach(function() {
440 activitySet
= { activityTypes
: [] };
441 scope
.activityTypes
= { comment
: { label
: 'Add a new comment' } };
443 scope
.addActivity(activitySet
, 'comment');
446 it('adds a new Comment activity to the set', function() {
447 expect(activitySet
.activityTypes
[0]).toEqual({
449 label
: scope
.activityTypes
.comment
.label
,
451 reference_activity
: 'Open Case',
452 reference_offset
: '1',
453 reference_select
: 'newest',
454 default_assignee_type
: defaultAssigneeDefaultValue
.value
459 describe('when creating a new workflow', function() {
460 beforeEach(inject(function ($controller
) {
461 apiCalls
.caseType
= null;
463 ctrl
= $controller('CaseTypeCtrl', {$scope
: scope
, apiCalls
: apiCalls
});
466 it('sets default values for the case type title, name, and active status', function() {
467 expect(scope
.caseType
).toEqual(jasmine
.objectContaining({
474 it('adds an Open Case activty to the default activty set', function() {
475 expect(scope
.caseType
.definition
.activitySets
[0].activityTypes
).toEqual([{
479 default_assignee_type
: defaultAssigneeDefaultValue
.value
484 describe('when adding a role', function() {
485 beforeEach(inject(function ($controller
) {
486 ctrl
= $controller('CaseTypeCtrl', {$scope
: scope
, apiCalls
: apiCalls
});
489 it('updates the case roles', function() {
491 // This line sort of simulates selecting a relationship type from the
492 // dropdown. It doesn't test that clicking picks the right value to
493 // add, just that if it did then the function that gets called does
494 // the right thing with it.
495 // Note the value returned by the dropdown is "backwards", e.g.
496 // for the client perspective direction the dropdown returns the other
497 // direction, which is also what's stored in the xml.
498 scope
.addRole(scope
.caseType
.definition
.caseRoles
, 'Case Coordinator');
500 expect(scope
.caseType
.definition
.caseRoles
).toEqual(
503 name
: 'Homeless Services Coordinator',
506 displaylabel
: 'Homeless Services Coordinator is'
509 name
: 'Case Coordinator',
510 displaylabel
: 'Case Coordinator is'
516 it('updates case roles if choose non-client-perspective direction', function() {
517 // again, the dropdown returns the opposite direction
518 scope
.addRole(scope
.caseType
.definition
.caseRoles
, 'Homeless Services Coordinator is');
519 expect(scope
.caseType
.definition
.caseRoles
).toEqual(
522 name
: 'Homeless Services Coordinator',
525 displaylabel
: 'Homeless Services Coordinator is'
528 name
: 'Homeless Services Coordinator is',
529 displaylabel
: 'Homeless Services Coordinator'
535 it("doesn't add the same role twice", function() {
536 // This is in the mock casetype to start, so check if can add twice.
537 scope
.addRole(scope
.caseType
.definition
.caseRoles
, 'Homeless Services Coordinator');
538 expect(scope
.caseType
.definition
.caseRoles
).toEqual(
541 name
: 'Homeless Services Coordinator',
544 displaylabel
: 'Homeless Services Coordinator is'
552 describe('crmAddName', function () {
555 beforeEach(function() {
556 scope
= $rootScope
.$new();
557 scope
.activityTypeOptions
= [1, 2, 3];
558 element
= '<span crm-add-name crm-options="activityTypeOptions"></span>';
560 spyOn(CRM
.$.fn
, 'crmSelect2').and
.callThrough();
562 element
= $compile(element
)(scope
);
566 describe('when initialized', function () {
569 beforeEach (function () {
570 var dataFunction
= CRM
.$.fn
.crmSelect2
.calls
.argsFor(0)[0].data
;
571 returnValue
= dataFunction();
574 it('updates the UI with updated value of scope variable', function () {
575 expect(returnValue
).toEqual({ results
: scope
.activityTypeOptions
});
580 describe('crmEditableTabTitle', function () {
581 var element
, titleLabel
, penIcon
, saveButton
, cancelButton
;
583 beforeEach(function() {
584 scope
= $rootScope
.$new();
585 element
= '<div crm-editable-tab-title title="Click to edit">' +
586 '<span>{{ activitySet.label }}</span>' +
589 scope
.activitySet
= { label
: 'Title'};
590 element
= $compile(element
)(scope
);
592 titleLabel
= $(element
).find('span');
593 penIcon
= $(element
).find('i.fa-pencil');
594 saveButton
= $(element
).find('button[type=button]');
595 cancelButton
= $(element
).find('button[type=cancel]');
600 describe('when initialized', function () {
601 it('hides the save and cancel button', function () {
602 expect(saveButton
.parent().css('display') === 'none').toBe(true);
603 expect(cancelButton
.parent().css('display') === 'none').toBe(true);
607 describe('when clicked on title label', function () {
608 beforeEach(function () {
612 it('hides the pen icon', function () {
613 expect(penIcon
.css('display') === 'none').toBe(true);
616 it('shows the save button', function () {
617 expect(saveButton
.parent().css('display') !== 'none').toBe(true);
620 it('makes the title editable', function () {
621 expect(titleLabel
.attr('contenteditable')).toBe('true');
625 describe('when clicked outside of the editable area', function () {
626 beforeEach(function () {
628 titleLabel
.text('Updated Title');
634 it('shows the pen icon', function () {
635 expect(penIcon
.css('display') !== 'none').toBe(true);
638 it('hides the save and cancel button', function () {
639 expect(saveButton
.parent().css('display') === 'none').toBe(true);
640 expect(cancelButton
.parent().css('display') === 'none').toBe(true);
643 it('makes the title non editable', function () {
644 expect(titleLabel
.attr('contenteditable')).not
.toBe('true');
647 it('does not update the title in angular context', function () {
648 expect(scope
.activitySet
.label
).toBe('Title');
652 describe('when ESCAPE key is pressed while typing', function () {
653 beforeEach(function () {
654 var eventObj
= $.Event('keydown');
655 eventObj
.key
= 'Escape';
658 titleLabel
.text('Updated Title');
659 titleLabel
.trigger(eventObj
);
663 it('shows the pen icon', function () {
664 expect(penIcon
.css('display') !== 'none').toBe(true);
667 it('hides the save and cancel button', function () {
668 expect(saveButton
.parent().css('display') === 'none').toBe(true);
669 expect(cancelButton
.parent().css('display') === 'none').toBe(true);
672 it('makes the title non editable', function () {
673 expect(titleLabel
.attr('contenteditable')).not
.toBe('true');
676 it('does not update the title', function () {
677 expect(scope
.activitySet
.label
).toBe('Title');
681 describe('when ENTER key is pressed while typing', function () {
682 beforeEach(function () {
683 var eventObj
= $.Event('keydown');
684 eventObj
.key
= 'Enter';
687 titleLabel
.text('Updated Title');
688 titleLabel
.trigger(eventObj
);
692 it('shows the pen icon', function () {
693 expect(penIcon
.css('display') !== 'none').toBe(true);
696 it('hides the save and cancel button', function () {
697 expect(saveButton
.parent().css('display') === 'none').toBe(true);
698 expect(cancelButton
.parent().css('display') === 'none').toBe(true);
701 it('makes the title non editable', function () {
702 expect(titleLabel
.attr('contenteditable')).not
.toBe('true');
705 it('updates the title in angular context', function () {
706 expect(scope
.activitySet
.label
).toBe('Updated Title');
710 describe('when SAVE button is clicked', function () {
711 beforeEach(function () {
713 titleLabel
.text('Updated Title');
718 it('shows the pen icon', function () {
719 expect(penIcon
.css('display') !== 'none').toBe(true);
722 it('hides the save and cancel button', function () {
723 expect(saveButton
.parent().css('display') === 'none').toBe(true);
724 expect(cancelButton
.parent().css('display') === 'none').toBe(true);
727 it('makes the title non editable', function () {
728 expect(titleLabel
.attr('contenteditable')).not
.toBe('true');
731 it('updates the title in angular context', function () {
732 expect(scope
.activitySet
.label
).toBe('Updated Title');
736 describe('when CANCEL button is clicked', function () {
737 beforeEach(function () {
739 titleLabel
.text('Updated Title');
740 cancelButton
.click();
744 it('shows the pen icon', function () {
745 expect(penIcon
.css('display') !== 'none').toBe(true);
748 it('hides the save and cancel button', function () {
749 expect(saveButton
.parent().css('display') === 'none').toBe(true);
750 expect(cancelButton
.parent().css('display') === 'none').toBe(true);
753 it('makes the title non editable', function () {
754 expect(titleLabel
.attr('contenteditable')).not
.toBe('true');
757 it('does not update the title in angular context', function () {
758 expect(scope
.activitySet
.label
).toBe('Title');
763 describe('CaseTypeListCtrl', function() {
764 var caseTypes
, crmApiSpy
;
766 beforeEach(function() {
774 crmApiSpy
= jasmine
.createSpy('crmApi').and
.returnValue($q
.resolve());
775 scope
= $rootScope
.$new();
776 ctrl
= $controller('CaseTypeListCtrl', {
778 caseTypes
: caseTypes
,
783 it('should store an index of case types', function() {
784 expect(scope
.caseTypes
).toEqual(caseTypes
.values
);
787 describe('toggleCaseType', function() {
788 var caseType
= { id
: _
.uniqueId() };
790 describe('when the case is active', function() {
791 beforeEach(function() {
792 caseType
.is_active
= '1';
794 scope
.toggleCaseType(caseType
);
797 it('sets the case type as inactive', function() {
798 expect(crmApiSpy
).toHaveBeenCalledWith('CaseType', 'create', jasmine
.objectContaining({
805 describe('when the case is inactive', function() {
806 beforeEach(function() {
807 caseType
.is_active
= '0';
809 scope
.toggleCaseType(caseType
);
812 it('sets the case type as active', function() {
813 expect(crmApiSpy
).toHaveBeenCalledWith('CaseType', 'create', jasmine
.objectContaining({
821 describe('deleteCaseType', function() {
822 var caseType
= { id
: _
.uniqueId() };
824 beforeEach(function() {
825 crmApiSpy
.and
.returnValue($q
.resolve(caseType
));
826 scope
.caseTypes
[caseType
.id
] = caseType
;
828 scope
.deleteCaseType(caseType
);
832 describe('when the case type can be deleted', function() {
833 it('deletes the case from the api', function() {
834 expect(crmApiSpy
).toHaveBeenCalledWith('CaseType', 'delete', { id
: caseType
.id
}, jasmine
.any(Object
));
837 it('removes the case type from the list', function() {
838 expect(scope
.caseTypes
[caseType
.id
]).toBeUndefined();
842 describe('when the case type cannot be delted', function() {
843 var error
= { error_message
: 'Error Message' };
845 beforeEach(function() {
848 crmApiSpy
.and
.returnValue($q
.reject(error
));
849 scope
.caseTypes
[caseType
.id
] = caseType
;
852 scope
.deleteCaseType(caseType
);
855 errorHandler
= crmApiSpy
.calls
.mostRecent().args
[3].error
;
859 it('displays the error message', function() {
860 expect(CRM
.alert
).toHaveBeenCalledWith(error
.error_message
, 'Error', 'error');
864 describe('revertCaseType', function() {
871 describe('when reverting a case type', function() {
872 beforeEach(function() {
873 scope
.revertCaseType(caseType
);
876 it('resets the case type information using the api', function() {
877 expect(crmApiSpy
).toHaveBeenCalledWith('CaseType', 'create', jasmine
.objectContaining({