Merge pull request #12020 from yashodha/inv_report
[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 apiCalls;
11 var ctrl;
12 var compile;
13 var scope;
14
15 beforeEach(function() {
16 CRM.resourceUrls = {
17 'civicrm': ''
18 };
19 // CRM_Case_XMLProcessor::REL_TYPE_CNAME
20 CRM.crmCaseType = {
21 'REL_TYPE_CNAME': 'label_b_a'
22 };
23 module('crmCaseType');
24 module('crmJsonComparator');
25 inject(function(crmJsonComparator) {
26 crmJsonComparator.register(jasmine);
27 });
28 });
29
30 beforeEach(inject(function(_$controller_, _$compile_, _$httpBackend_, _$q_, _$rootScope_) {
31 $controller = _$controller_;
32 $compile = _$compile_;
33 $httpBackend = _$httpBackend_;
34 $q = _$q_;
35 $rootScope = _$rootScope_;
36 }));
37
38 describe('CaseTypeCtrl', function() {
39 beforeEach(function () {
40 apiCalls = {
41 actStatuses: {
42 values: [
43 {
44 "id": "272",
45 "option_group_id": "25",
46 "label": "Scheduled",
47 "value": "1",
48 "name": "Scheduled",
49 "filter": "0",
50 "is_default": "1",
51 "weight": "1",
52 "is_optgroup": "0",
53 "is_reserved": "1",
54 "is_active": "1"
55 },
56 {
57 "id": "273",
58 "option_group_id": "25",
59 "label": "Completed",
60 "value": "2",
61 "name": "Completed",
62 "filter": "0",
63 "weight": "2",
64 "is_optgroup": "0",
65 "is_reserved": "1",
66 "is_active": "1"
67 }
68 ]
69 },
70 caseStatuses: {
71 values: [
72 {
73 "id": "290",
74 "option_group_id": "28",
75 "label": "Ongoing",
76 "value": "1",
77 "name": "Open",
78 "grouping": "Opened",
79 "filter": "0",
80 "is_default": "1",
81 "weight": "1",
82 "is_optgroup": "0",
83 "is_reserved": "1",
84 "is_active": "1"
85 },
86 {
87 "id": "291",
88 "option_group_id": "28",
89 "label": "Resolved",
90 "value": "2",
91 "name": "Closed",
92 "grouping": "Closed",
93 "filter": "0",
94 "weight": "2",
95 "is_optgroup": "0",
96 "is_reserved": "1",
97 "is_active": "1"
98 }
99 ]
100 },
101 actTypes: {
102 values: [
103 {
104 "id": "784",
105 "option_group_id": "2",
106 "label": "ADC referral",
107 "value": "62",
108 "name": "ADC referral",
109 "filter": "0",
110 "is_default": "0",
111 "weight": "64",
112 "is_optgroup": "0",
113 "is_reserved": "0",
114 "is_active": "1",
115 "component_id": "7"
116 },
117 {
118 "id": "32",
119 "option_group_id": "2",
120 "label": "Add Client To Case",
121 "value": "27",
122 "name": "Add Client To Case",
123 "filter": "0",
124 "is_default": "0",
125 "weight": "26",
126 "description": "",
127 "is_optgroup": "0",
128 "is_reserved": "1",
129 "is_active": "1",
130 "component_id": "7"
131 },
132 {
133 "id": "18",
134 "option_group_id": "2",
135 "label": "Open Case",
136 "value": "13",
137 "name": "Open Case",
138 "filter": "0",
139 "is_default": "0",
140 "weight": "13",
141 "is_optgroup": "0",
142 "is_reserved": "1",
143 "is_active": "1",
144 "component_id": "7",
145 "icon": "fa-folder-open-o"
146 },
147 {
148 "id": "857",
149 "option_group_id": "2",
150 "label": "Medical evaluation",
151 "value": "55",
152 "name": "Medical evaluation",
153 "filter": "0",
154 "is_default": "0",
155 "weight": "56",
156 "is_optgroup": "0",
157 "is_reserved": "0",
158 "is_active": "1",
159 "component_id": "7"
160 },
161 ]
162 },
163 relTypes: {
164 values: [
165 {
166 "id": "14",
167 "name_a_b": "Benefits Specialist is",
168 "label_a_b": "Benefits Specialist is",
169 "name_b_a": "Benefits Specialist",
170 "label_b_a": "Benefits Specialist",
171 "description": "Benefits Specialist",
172 "contact_type_a": "Individual",
173 "contact_type_b": "Individual",
174 "is_reserved": "0",
175 "is_active": "1"
176 },
177 {
178 "id": "9",
179 "name_a_b": "Case Coordinator is",
180 "label_a_b": "Case Coordinator is",
181 "name_b_a": "Case Coordinator",
182 "label_b_a": "Case Coordinator",
183 "description": "Case Coordinator",
184 "contact_type_a": "Individual",
185 "contact_type_b": "Individual",
186 "is_reserved": "0",
187 "is_active": "1"
188 }
189 ]
190 },
191 caseType: {
192 "id": "1",
193 "name": "housing_support",
194 "title": "Housing Support",
195 "description": "Help homeless individuals obtain temporary and long-term housing",
196 "is_active": "1",
197 "is_reserved": "0",
198 "weight": "1",
199 "is_forkable": "1",
200 "is_forked": "",
201 "definition": {
202 "activityTypes": [
203 {"name": "Open Case", "max_instances": "1"}
204 ],
205 "activitySets": [
206 {
207 "name": "standard_timeline",
208 "label": "Standard Timeline",
209 "timeline": "1",
210 "activityTypes": [
211 {
212 "name": "Open Case",
213 "status": "Completed"
214 },
215 {
216 "name": "Medical evaluation",
217 "reference_activity": "Open Case",
218 "reference_offset": "1",
219 "reference_select": "newest"
220 }
221 ]
222 }
223 ],
224 "caseRoles": [
225 {
226 "name": "Homeless Services Coordinator",
227 "creator": "1",
228 "manager": "1"
229 }
230 ]
231 }
232 }
233 };
234 scope = $rootScope.$new();
235 ctrl = $controller('CaseTypeCtrl', {$scope: scope, apiCalls: apiCalls});
236 });
237
238 it('should load activity statuses', function() {
239 expect(scope.activityStatuses).toEqualData(apiCalls.actStatuses.values);
240 });
241
242 it('should load activity types', function() {
243 expect(scope.activityTypes['ADC referral']).toEqualData(apiCalls.actTypes.values[0]);
244 });
245
246 it('addActivitySet should add an activitySet to the case type', function() {
247 scope.addActivitySet('timeline');
248 var activitySets = scope.caseType.definition.activitySets;
249 var newSet = activitySets[activitySets.length - 1];
250 expect(newSet.name).toBe('timeline_1');
251 expect(newSet.timeline).toBe('1');
252 expect(newSet.label).toBe('Timeline');
253 });
254
255 it('addActivitySet handles second timeline correctly', function() {
256 scope.addActivitySet('timeline');
257 scope.addActivitySet('timeline');
258 var activitySets = scope.caseType.definition.activitySets;
259 var newSet = activitySets[activitySets.length - 1];
260 expect(newSet.name).toBe('timeline_2');
261 expect(newSet.timeline).toBe('1');
262 expect(newSet.label).toBe('Timeline #2');
263 });
264 });
265
266 describe('crmAddName', function () {
267 var element;
268
269 beforeEach(function() {
270 scope = $rootScope.$new();
271 scope.activityTypeOptions = [1, 2, 3];
272 element = '<span crm-add-name crm-options="activityTypeOptions"></span>';
273
274 spyOn(CRM.$.fn, 'crmSelect2').and.callThrough();
275
276 element = $compile(element)(scope);
277 scope.$digest();
278 });
279
280 describe('when initialized', function () {
281 var returnValue;
282
283 beforeEach (function () {
284 var dataFunction = CRM.$.fn.crmSelect2.calls.argsFor(0)[0].data;
285 returnValue = dataFunction();
286 });
287
288 it('updates the UI with updated value of scope variable', function () {
289 expect(returnValue).toEqual({ results: scope.activityTypeOptions });
290 });
291 });
292 });
293
294 describe('CaseTypeListCtrl', function() {
295 var caseTypes, crmApiSpy;
296
297 beforeEach(function() {
298 caseTypes = {
299 values: {
300 1: { id: 1 },
301 2: { id: 2 },
302 3: { id: 3 }
303 }
304 };
305 crmApiSpy = jasmine.createSpy('crmApi').and.returnValue($q.resolve());
306 scope = $rootScope.$new();
307 ctrl = $controller('CaseTypeListCtrl', {
308 $scope: scope,
309 caseTypes: caseTypes,
310 crmApi: crmApiSpy
311 });
312 });
313
314 it('should store an index of case types', function() {
315 expect(scope.caseTypes).toEqual(caseTypes.values);
316 });
317
318 describe('toggleCaseType', function() {
319 var caseType = { id: _.uniqueId() };
320
321 describe('when the case is active', function() {
322 beforeEach(function() {
323 caseType.is_active = '1';
324
325 scope.toggleCaseType(caseType);
326 });
327
328 it('sets the case type as inactive', function() {
329 expect(crmApiSpy).toHaveBeenCalledWith('CaseType', 'create', jasmine.objectContaining({
330 id: caseType.id,
331 is_active: '0'
332 }), true);
333 });
334 });
335
336 describe('when the case is inactive', function() {
337 beforeEach(function() {
338 caseType.is_active = '0';
339
340 scope.toggleCaseType(caseType);
341 });
342
343 it('sets the case type as active', function() {
344 expect(crmApiSpy).toHaveBeenCalledWith('CaseType', 'create', jasmine.objectContaining({
345 id: caseType.id,
346 is_active: '1'
347 }), true);
348 });
349 });
350 });
351
352 describe('deleteCaseType', function() {
353 var caseType = { id: _.uniqueId() };
354
355 beforeEach(function() {
356 crmApiSpy.and.returnValue($q.resolve(caseType));
357 scope.caseTypes[caseType.id] = caseType;
358
359 scope.deleteCaseType(caseType);
360 scope.$digest();
361 });
362
363 describe('when the case type can be deleted', function() {
364 it('deletes the case from the api', function() {
365 expect(crmApiSpy).toHaveBeenCalledWith('CaseType', 'delete', { id: caseType.id }, jasmine.any(Object));
366 });
367
368 it('removes the case type from the list', function() {
369 expect(scope.caseTypes[caseType.id]).toBeUndefined();
370 });
371 });
372
373 describe('when the case type cannot be delted', function() {
374 var error = { error_message: 'Error Message' };
375
376 beforeEach(function() {
377 var errorHandler;
378
379 crmApiSpy.and.returnValue($q.reject(error));
380 scope.caseTypes[caseType.id] = caseType;
381
382 spyOn(CRM, 'alert');
383 scope.deleteCaseType(caseType);
384 scope.$digest();
385
386 errorHandler = crmApiSpy.calls.mostRecent().args[3].error;
387 errorHandler(error);
388 });
389
390 it('displays the error message', function() {
391 expect(CRM.alert).toHaveBeenCalledWith(error.error_message, 'Error', 'error');
392 });
393 });
394
395 describe('revertCaseType', function() {
396 var caseType = {
397 id: _.uniqueId(),
398 definition: {},
399 is_forked: '1'
400 };
401
402 describe('when reverting a case type', function() {
403 beforeEach(function() {
404 scope.revertCaseType(caseType);
405 });
406
407 it('resets the case type information using the api', function() {
408 expect(crmApiSpy).toHaveBeenCalledWith('CaseType', 'create', jasmine.objectContaining({
409 id: caseType.id,
410 definition: 'null',
411 is_forked: '0'
412 }), true);
413 });
414 });
415 });
416 });
417 });
418 });