Fix queue code to use CiviCRM's version of what the time is, instead of server's.
[civicrm-core.git] / tests / karma / unit / crmCaseTypeSpec.js
1 /* global $, _, CRM:true */
2 'use strict';
3
4 describe('crmCaseType', function() {
5 var $controller;
6 var $compile;
7 var $httpBackend;
8 var $q;
9 var $rootScope;
10 var $timeout;
11 var apiCalls;
12 var ctrl;
13 var compile;
14 var defaultAssigneeDefaultValue;
15 var scope;
16
17 beforeEach(function() {
18 CRM.resourceUrls = {
19 'civicrm': ''
20 };
21 module('crmCaseType');
22 module('crmJsonComparator');
23 inject(function(crmJsonComparator) {
24 crmJsonComparator.register(jasmine);
25 });
26 });
27
28 beforeEach(inject(function(_$controller_, _$compile_, _$httpBackend_, _$q_, _$rootScope_, _$timeout_) {
29 $controller = _$controller_;
30 $compile = _$compile_;
31 $httpBackend = _$httpBackend_;
32 $q = _$q_;
33 $rootScope = _$rootScope_;
34 $timeout = _$timeout_;
35 }));
36
37 describe('CaseTypeCtrl', function() {
38 beforeEach(function () {
39 apiCalls = {
40 actStatuses: {
41 values: [
42 {
43 "id": "272",
44 "option_group_id": "25",
45 "label": "Scheduled",
46 "value": "1",
47 "name": "Scheduled",
48 "filter": "0",
49 "is_default": "1",
50 "weight": "1",
51 "is_optgroup": "0",
52 "is_reserved": "1",
53 "is_active": "1"
54 },
55 {
56 "id": "273",
57 "option_group_id": "25",
58 "label": "Completed",
59 "value": "2",
60 "name": "Completed",
61 "filter": "0",
62 "weight": "2",
63 "is_optgroup": "0",
64 "is_reserved": "1",
65 "is_active": "1"
66 }
67 ]
68 },
69 caseStatuses: {
70 values: [
71 {
72 "id": "290",
73 "option_group_id": "28",
74 "label": "Ongoing",
75 "value": "1",
76 "name": "Open",
77 "grouping": "Opened",
78 "filter": "0",
79 "is_default": "1",
80 "weight": "1",
81 "is_optgroup": "0",
82 "is_reserved": "1",
83 "is_active": "1"
84 },
85 {
86 "id": "291",
87 "option_group_id": "28",
88 "label": "Resolved",
89 "value": "2",
90 "name": "Closed",
91 "grouping": "Closed",
92 "filter": "0",
93 "weight": "2",
94 "is_optgroup": "0",
95 "is_reserved": "1",
96 "is_active": "1"
97 }
98 ]
99 },
100 actTypes: {
101 values: [
102 {
103 "id": "784",
104 "option_group_id": "2",
105 "label": "ADC referral",
106 "value": "62",
107 "name": "ADC referral",
108 "filter": "0",
109 "is_default": "0",
110 "weight": "64",
111 "is_optgroup": "0",
112 "is_reserved": "0",
113 "is_active": "1",
114 "component_id": "7"
115 },
116 {
117 "id": "32",
118 "option_group_id": "2",
119 "label": "Add Client To Case",
120 "value": "27",
121 "name": "Add Client To Case",
122 "filter": "0",
123 "is_default": "0",
124 "weight": "26",
125 "description": "",
126 "is_optgroup": "0",
127 "is_reserved": "1",
128 "is_active": "1",
129 "component_id": "7"
130 },
131 {
132 "id": "18",
133 "option_group_id": "2",
134 "label": "Open Case",
135 "value": "13",
136 "name": "Open Case",
137 "filter": "0",
138 "is_default": "0",
139 "weight": "13",
140 "is_optgroup": "0",
141 "is_reserved": "1",
142 "is_active": "1",
143 "component_id": "7",
144 "icon": "fa-folder-open-o"
145 },
146 {
147 "id": "857",
148 "option_group_id": "2",
149 "label": "Medical evaluation",
150 "value": "55",
151 "name": "Medical evaluation",
152 "filter": "0",
153 "is_default": "0",
154 "weight": "56",
155 "is_optgroup": "0",
156 "is_reserved": "0",
157 "is_active": "1",
158 "component_id": "7"
159 },
160 ]
161 },
162 relTypes: {
163 values: [
164 {
165 "id": "14",
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",
173 "is_reserved": "0",
174 "is_active": "1"
175 },
176 {
177 "id": "9",
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",
185 "is_reserved": "0",
186 "is_active": "1"
187 },
188 {
189 "id": "10",
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",
197 "is_reserved": "0",
198 "is_active": "1"
199 },
200 {
201 "id": "2",
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",
209 "is_reserved": "0",
210 "is_active": "1"
211 },
212 // include one where name is different from label
213 {
214 "id": "27",
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",
222 "is_reserved": "0",
223 "is_active": "1"
224 }
225 ]
226 },
227 // Where is this used in the tests?
228 // It seems to be in the format for the activity assignee.
229 relTypesForm: {
230 values: [
231 {
232 "key": "14_b_a",
233 "value": "Benefits Specialist"
234 },
235 {
236 "key": "14_a_b",
237 "value": "Benefits Specialist is"
238 },
239 {
240 "key": "9_b_a",
241 "value": "Case Coordinator"
242 },
243 {
244 "key": "9_a_b",
245 "value": "Case Coordinator is"
246 },
247 {
248 "key": "2_b_a",
249 "value": "Spouse of"
250 }
251 ]
252 },
253 caseType: {
254 "id": "1",
255 "name": "housing_support",
256 "title": "Housing Support",
257 "description": "Help homeless individuals obtain temporary and long-term housing",
258 "is_active": "1",
259 "is_reserved": "0",
260 "weight": "1",
261 "is_forkable": "1",
262 "is_forked": "",
263 "definition": {
264 "activityTypes": [
265 {"name": "Open Case", "max_instances": "1"}
266 ],
267 "activitySets": [
268 {
269 "name": "standard_timeline",
270 "label": "Standard Timeline",
271 "timeline": "1",
272 "activityTypes": [
273 {
274 "name": "Open Case",
275 "status": "Completed"
276 },
277 {
278 "name": "Medical evaluation",
279 "reference_activity": "Open Case",
280 "reference_offset": "1",
281 "reference_select": "newest"
282 }
283 ]
284 }
285 ],
286 "caseRoles": [
287 {
288 "name": "Homeless Services Coordinator",
289 "creator": "1",
290 "manager": "1"
291 }
292 ]
293 }
294 },
295 defaultAssigneeTypes: {
296 values: [
297 {
298 "id": "1174",
299 "option_group_id": "152",
300 "label": "None",
301 "value": "1",
302 "name": "NONE",
303 "filter": "0",
304 "is_default": "1",
305 "weight": "1",
306 "is_optgroup": "0",
307 "is_reserved": "0",
308 "is_active": "1"
309 },
310 {
311 "id": "1175",
312 "option_group_id": "152",
313 "label": "By relationship to workflow client",
314 "value": "2",
315 "name": "BY_RELATIONSHIP",
316 "filter": "0",
317 "is_default": "0",
318 "weight": "2",
319 "is_optgroup": "0",
320 "is_reserved": "0",
321 "is_active": "1"
322 },
323 {
324 "id": "1176",
325 "option_group_id": "152",
326 "label": "Specific contact",
327 "value": "3",
328 "name": "SPECIFIC_CONTACT",
329 "filter": "0",
330 "is_default": "0",
331 "weight": "3",
332 "is_optgroup": "0",
333 "is_reserved": "0",
334 "is_active": "1"
335 },
336 {
337 "id": "1177",
338 "option_group_id": "152",
339 "label": "User creating the workflow",
340 "value": "4",
341 "name": "USER_CREATING_THE_CASE",
342 "filter": "0",
343 "is_default": "0",
344 "weight": "4",
345 "is_optgroup": "0",
346 "is_reserved": "0",
347 "is_active": "1"
348 }
349 ]
350 }
351 };
352 defaultAssigneeDefaultValue = _.find(apiCalls.defaultAssigneeTypes.values, { is_default: '1' });
353 scope = $rootScope.$new();
354 ctrl = $controller('CaseTypeCtrl', {$scope: scope, apiCalls: apiCalls});
355 });
356
357 it('should load activity statuses', function() {
358 expect(scope.activityStatuses).toEqualData(apiCalls.actStatuses.values);
359 });
360
361 it('should load activity types', function() {
362 expect(scope.activityTypes['ADC referral']).toEqualData(apiCalls.actTypes.values[0]);
363 });
364
365 it('should store the default assignee types', function() {
366 expect(scope.defaultAssigneeTypes).toBe(apiCalls.defaultAssigneeTypes.values);
367 });
368
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();
372
373 expect(scope.defaultAssigneeTypeValues).toEqual(defaultAssigneeTypeValues);
374 });
375
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;
379
380 result.push({
381 label: relType.label_b_a,
382 value: relType.id + '_a_b'
383 });
384
385 if (!isBidirectionalRelationship) {
386 result.push({
387 label: relType.label_a_b,
388 value: relType.id + '_b_a'
389 });
390 }
391 }, []);
392
393 expect(scope.defaultRelationshipTypeOptions).toEqual(defaultRelationshipTypeOptions);
394 });
395
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');
403 });
404
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');
413 });
414
415 describe('when clearing the activity\'s default assignee type values', function() {
416 var activity;
417
418 beforeEach(function() {
419 activity = {
420 default_assignee_relationship: 1,
421 default_assignee_contact: 2
422 };
423
424 scope.clearActivityDefaultAssigneeValues(activity);
425 });
426
427 it('clears the default assignee relationship for the activity', function() {
428 expect(activity.default_assignee_relationship).toBe(null);
429 });
430
431 it('clears the default assignee contact for the activity', function() {
432 expect(activity.default_assignee_contact).toBe(null);
433 });
434 });
435
436 describe('when adding a new activity to a set', function() {
437 var activitySet;
438
439 beforeEach(function() {
440 activitySet = { activityTypes: [] };
441 scope.activityTypes = { comment: { label: 'Add a new comment' } };
442
443 scope.addActivity(activitySet, 'comment');
444 });
445
446 it('adds a new Comment activity to the set', function() {
447 expect(activitySet.activityTypes[0]).toEqual({
448 name: 'comment',
449 label: scope.activityTypes.comment.label,
450 status: 'Scheduled',
451 reference_activity: 'Open Case',
452 reference_offset: '1',
453 reference_select: 'newest',
454 default_assignee_type: defaultAssigneeDefaultValue.value
455 });
456 });
457 });
458
459 describe('when creating a new workflow', function() {
460 beforeEach(inject(function ($controller) {
461 apiCalls.caseType = null;
462
463 ctrl = $controller('CaseTypeCtrl', {$scope: scope, apiCalls: apiCalls});
464 }));
465
466 it('sets default values for the case type title, name, and active status', function() {
467 expect(scope.caseType).toEqual(jasmine.objectContaining({
468 title: '',
469 name: '',
470 is_active: '1'
471 }));
472 });
473
474 it('adds an Open Case activty to the default activty set', function() {
475 expect(scope.caseType.definition.activitySets[0].activityTypes).toEqual([{
476 name: 'Open Case',
477 label: 'Open Case',
478 status: 'Completed',
479 default_assignee_type: defaultAssigneeDefaultValue.value
480 }]);
481 });
482 });
483
484 describe('when adding a role', function() {
485 beforeEach(inject(function ($controller) {
486 ctrl = $controller('CaseTypeCtrl', {$scope: scope, apiCalls: apiCalls});
487 }));
488
489 it('updates the case roles', function() {
490
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');
499
500 expect(scope.caseType.definition.caseRoles).toEqual(
501 [
502 {
503 name: 'Homeless Services Coordinator',
504 creator: '1',
505 manager: '1',
506 displaylabel: 'Homeless Services Coordinator is'
507 },
508 {
509 name: 'Case Coordinator',
510 displaylabel: 'Case Coordinator is'
511 }
512 ]
513 );
514 });
515
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(
520 [
521 {
522 name: 'Homeless Services Coordinator',
523 creator: '1',
524 manager: '1',
525 displaylabel: 'Homeless Services Coordinator is'
526 },
527 {
528 name: 'Homeless Services Coordinator is',
529 displaylabel: 'Homeless Services Coordinator'
530 }
531 ]
532 );
533 });
534
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(
539 [
540 {
541 name: 'Homeless Services Coordinator',
542 creator: '1',
543 manager: '1',
544 displaylabel: 'Homeless Services Coordinator is'
545 }
546 ]
547 );
548 });
549 });
550 });
551
552 describe('crmAddName', function () {
553 var element;
554
555 beforeEach(function() {
556 scope = $rootScope.$new();
557 scope.activityTypeOptions = [1, 2, 3];
558 element = '<span crm-add-name crm-options="activityTypeOptions"></span>';
559
560 spyOn(CRM.$.fn, 'crmSelect2').and.callThrough();
561
562 element = $compile(element)(scope);
563 scope.$digest();
564 });
565
566 describe('when initialized', function () {
567 var returnValue;
568
569 beforeEach (function () {
570 var dataFunction = CRM.$.fn.crmSelect2.calls.argsFor(0)[0].data;
571 returnValue = dataFunction();
572 });
573
574 it('updates the UI with updated value of scope variable', function () {
575 expect(returnValue).toEqual({ results: scope.activityTypeOptions });
576 });
577 });
578 });
579
580 describe('crmEditableTabTitle', function () {
581 var element, titleLabel, penIcon, saveButton, cancelButton;
582
583 beforeEach(function() {
584 scope = $rootScope.$new();
585 element = '<div crm-editable-tab-title title="Click to edit">' +
586 '<span>{{ activitySet.label }}</span>' +
587 '</div>';
588
589 scope.activitySet = { label: 'Title'};
590 element = $compile(element)(scope);
591
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]');
596
597 scope.$digest();
598 });
599
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);
604 });
605 });
606
607 describe('when clicked on title label', function () {
608 beforeEach(function () {
609 titleLabel.click();
610 });
611
612 it('hides the pen icon', function () {
613 expect(penIcon.css('display') === 'none').toBe(true);
614 });
615
616 it('shows the save button', function () {
617 expect(saveButton.parent().css('display') !== 'none').toBe(true);
618 });
619
620 it('makes the title editable', function () {
621 expect(titleLabel.attr('contenteditable')).toBe('true');
622 });
623 });
624
625 describe('when clicked outside of the editable area', function () {
626 beforeEach(function () {
627 titleLabel.click();
628 titleLabel.text('Updated Title');
629 titleLabel.blur();
630 $timeout.flush();
631 scope.$digest();
632 });
633
634 it('shows the pen icon', function () {
635 expect(penIcon.css('display') !== 'none').toBe(true);
636 });
637
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);
641 });
642
643 it('makes the title non editable', function () {
644 expect(titleLabel.attr('contenteditable')).not.toBe('true');
645 });
646
647 it('does not update the title in angular context', function () {
648 expect(scope.activitySet.label).toBe('Title');
649 });
650 });
651
652 describe('when ESCAPE key is pressed while typing', function () {
653 beforeEach(function () {
654 var eventObj = $.Event('keydown');
655 eventObj.key = 'Escape';
656
657 titleLabel.click();
658 titleLabel.text('Updated Title');
659 titleLabel.trigger(eventObj);
660 scope.$digest();
661 });
662
663 it('shows the pen icon', function () {
664 expect(penIcon.css('display') !== 'none').toBe(true);
665 });
666
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);
670 });
671
672 it('makes the title non editable', function () {
673 expect(titleLabel.attr('contenteditable')).not.toBe('true');
674 });
675
676 it('does not update the title', function () {
677 expect(scope.activitySet.label).toBe('Title');
678 });
679 });
680
681 describe('when ENTER key is pressed while typing', function () {
682 beforeEach(function () {
683 var eventObj = $.Event('keydown');
684 eventObj.key = 'Enter';
685
686 titleLabel.click();
687 titleLabel.text('Updated Title');
688 titleLabel.trigger(eventObj);
689 scope.$digest();
690 });
691
692 it('shows the pen icon', function () {
693 expect(penIcon.css('display') !== 'none').toBe(true);
694 });
695
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);
699 });
700
701 it('makes the title non editable', function () {
702 expect(titleLabel.attr('contenteditable')).not.toBe('true');
703 });
704
705 it('updates the title in angular context', function () {
706 expect(scope.activitySet.label).toBe('Updated Title');
707 });
708 });
709
710 describe('when SAVE button is clicked', function () {
711 beforeEach(function () {
712 titleLabel.click();
713 titleLabel.text('Updated Title');
714 saveButton.click();
715 scope.$digest();
716 });
717
718 it('shows the pen icon', function () {
719 expect(penIcon.css('display') !== 'none').toBe(true);
720 });
721
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);
725 });
726
727 it('makes the title non editable', function () {
728 expect(titleLabel.attr('contenteditable')).not.toBe('true');
729 });
730
731 it('updates the title in angular context', function () {
732 expect(scope.activitySet.label).toBe('Updated Title');
733 });
734 });
735
736 describe('when CANCEL button is clicked', function () {
737 beforeEach(function () {
738 titleLabel.click();
739 titleLabel.text('Updated Title');
740 cancelButton.click();
741 scope.$digest();
742 });
743
744 it('shows the pen icon', function () {
745 expect(penIcon.css('display') !== 'none').toBe(true);
746 });
747
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);
751 });
752
753 it('makes the title non editable', function () {
754 expect(titleLabel.attr('contenteditable')).not.toBe('true');
755 });
756
757 it('does not update the title in angular context', function () {
758 expect(scope.activitySet.label).toBe('Title');
759 });
760 });
761 });
762
763 describe('CaseTypeListCtrl', function() {
764 var caseTypes, crmApiSpy;
765
766 beforeEach(function() {
767 caseTypes = {
768 values: {
769 1: { id: 1 },
770 2: { id: 2 },
771 3: { id: 3 }
772 }
773 };
774 crmApiSpy = jasmine.createSpy('crmApi').and.returnValue($q.resolve());
775 scope = $rootScope.$new();
776 ctrl = $controller('CaseTypeListCtrl', {
777 $scope: scope,
778 caseTypes: caseTypes,
779 crmApi: crmApiSpy
780 });
781 });
782
783 it('should store an index of case types', function() {
784 expect(scope.caseTypes).toEqual(caseTypes.values);
785 });
786
787 describe('toggleCaseType', function() {
788 var caseType = { id: _.uniqueId() };
789
790 describe('when the case is active', function() {
791 beforeEach(function() {
792 caseType.is_active = '1';
793
794 scope.toggleCaseType(caseType);
795 });
796
797 it('sets the case type as inactive', function() {
798 expect(crmApiSpy).toHaveBeenCalledWith('CaseType', 'create', jasmine.objectContaining({
799 id: caseType.id,
800 is_active: '0'
801 }), true);
802 });
803 });
804
805 describe('when the case is inactive', function() {
806 beforeEach(function() {
807 caseType.is_active = '0';
808
809 scope.toggleCaseType(caseType);
810 });
811
812 it('sets the case type as active', function() {
813 expect(crmApiSpy).toHaveBeenCalledWith('CaseType', 'create', jasmine.objectContaining({
814 id: caseType.id,
815 is_active: '1'
816 }), true);
817 });
818 });
819 });
820
821 describe('deleteCaseType', function() {
822 var caseType = { id: _.uniqueId() };
823
824 beforeEach(function() {
825 crmApiSpy.and.returnValue($q.resolve(caseType));
826 scope.caseTypes[caseType.id] = caseType;
827
828 scope.deleteCaseType(caseType);
829 scope.$digest();
830 });
831
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));
835 });
836
837 it('removes the case type from the list', function() {
838 expect(scope.caseTypes[caseType.id]).toBeUndefined();
839 });
840 });
841
842 describe('when the case type cannot be delted', function() {
843 var error = { error_message: 'Error Message' };
844
845 beforeEach(function() {
846 var errorHandler;
847
848 crmApiSpy.and.returnValue($q.reject(error));
849 scope.caseTypes[caseType.id] = caseType;
850
851 spyOn(CRM, 'alert');
852 scope.deleteCaseType(caseType);
853 scope.$digest();
854
855 errorHandler = crmApiSpy.calls.mostRecent().args[3].error;
856 errorHandler(error);
857 });
858
859 it('displays the error message', function() {
860 expect(CRM.alert).toHaveBeenCalledWith(error.error_message, 'Error', 'error');
861 });
862 });
863
864 describe('revertCaseType', function() {
865 var caseType = {
866 id: _.uniqueId(),
867 definition: {},
868 is_forked: '1'
869 };
870
871 describe('when reverting a case type', function() {
872 beforeEach(function() {
873 scope.revertCaseType(caseType);
874 });
875
876 it('resets the case type information using the api', function() {
877 expect(crmApiSpy).toHaveBeenCalledWith('CaseType', 'create', jasmine.objectContaining({
878 id: caseType.id,
879 definition: 'null',
880 is_forked: '0'
881 }), true);
882 });
883 });
884 });
885 });
886 });
887 });