Add test for caseTypeListCtrl case types storage
[civicrm-core.git] / tests / karma / unit / crmCaseTypeSpec.js
1 'use strict';
2
3 describe('crmCaseType', function() {
4 var $controller;
5 var $compile;
6 var $httpBackend;
7 var $rootScope;
8 var apiCalls;
9 var ctrl;
10 var compile;
11 var scope;
12
13 beforeEach(function() {
14 CRM.resourceUrls = {
15 'civicrm': ''
16 };
17 // CRM_Case_XMLProcessor::REL_TYPE_CNAME
18 CRM.crmCaseType = {
19 'REL_TYPE_CNAME': 'label_b_a'
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_, _$rootScope_) {
29 $controller = _$controller_;
30 $compile = _$compile_;
31 $httpBackend = _$httpBackend_;
32 $rootScope = _$rootScope_;
33 }));
34
35 describe('CaseTypeCtrl', function() {
36 beforeEach(function () {
37 apiCalls = {
38 actStatuses: {
39 values: [
40 {
41 "id": "272",
42 "option_group_id": "25",
43 "label": "Scheduled",
44 "value": "1",
45 "name": "Scheduled",
46 "filter": "0",
47 "is_default": "1",
48 "weight": "1",
49 "is_optgroup": "0",
50 "is_reserved": "1",
51 "is_active": "1"
52 },
53 {
54 "id": "273",
55 "option_group_id": "25",
56 "label": "Completed",
57 "value": "2",
58 "name": "Completed",
59 "filter": "0",
60 "weight": "2",
61 "is_optgroup": "0",
62 "is_reserved": "1",
63 "is_active": "1"
64 }
65 ]
66 },
67 caseStatuses: {
68 values: [
69 {
70 "id": "290",
71 "option_group_id": "28",
72 "label": "Ongoing",
73 "value": "1",
74 "name": "Open",
75 "grouping": "Opened",
76 "filter": "0",
77 "is_default": "1",
78 "weight": "1",
79 "is_optgroup": "0",
80 "is_reserved": "1",
81 "is_active": "1"
82 },
83 {
84 "id": "291",
85 "option_group_id": "28",
86 "label": "Resolved",
87 "value": "2",
88 "name": "Closed",
89 "grouping": "Closed",
90 "filter": "0",
91 "weight": "2",
92 "is_optgroup": "0",
93 "is_reserved": "1",
94 "is_active": "1"
95 }
96 ]
97 },
98 actTypes: {
99 values: [
100 {
101 "id": "784",
102 "option_group_id": "2",
103 "label": "ADC referral",
104 "value": "62",
105 "name": "ADC referral",
106 "filter": "0",
107 "is_default": "0",
108 "weight": "64",
109 "is_optgroup": "0",
110 "is_reserved": "0",
111 "is_active": "1",
112 "component_id": "7"
113 },
114 {
115 "id": "32",
116 "option_group_id": "2",
117 "label": "Add Client To Case",
118 "value": "27",
119 "name": "Add Client To Case",
120 "filter": "0",
121 "is_default": "0",
122 "weight": "26",
123 "description": "",
124 "is_optgroup": "0",
125 "is_reserved": "1",
126 "is_active": "1",
127 "component_id": "7"
128 },
129 {
130 "id": "18",
131 "option_group_id": "2",
132 "label": "Open Case",
133 "value": "13",
134 "name": "Open Case",
135 "filter": "0",
136 "is_default": "0",
137 "weight": "13",
138 "is_optgroup": "0",
139 "is_reserved": "1",
140 "is_active": "1",
141 "component_id": "7",
142 "icon": "fa-folder-open-o"
143 },
144 {
145 "id": "857",
146 "option_group_id": "2",
147 "label": "Medical evaluation",
148 "value": "55",
149 "name": "Medical evaluation",
150 "filter": "0",
151 "is_default": "0",
152 "weight": "56",
153 "is_optgroup": "0",
154 "is_reserved": "0",
155 "is_active": "1",
156 "component_id": "7"
157 },
158 ]
159 },
160 relTypes: {
161 values: [
162 {
163 "id": "14",
164 "name_a_b": "Benefits Specialist is",
165 "label_a_b": "Benefits Specialist is",
166 "name_b_a": "Benefits Specialist",
167 "label_b_a": "Benefits Specialist",
168 "description": "Benefits Specialist",
169 "contact_type_a": "Individual",
170 "contact_type_b": "Individual",
171 "is_reserved": "0",
172 "is_active": "1"
173 },
174 {
175 "id": "9",
176 "name_a_b": "Case Coordinator is",
177 "label_a_b": "Case Coordinator is",
178 "name_b_a": "Case Coordinator",
179 "label_b_a": "Case Coordinator",
180 "description": "Case Coordinator",
181 "contact_type_a": "Individual",
182 "contact_type_b": "Individual",
183 "is_reserved": "0",
184 "is_active": "1"
185 }
186 ]
187 },
188 caseType: {
189 "id": "1",
190 "name": "housing_support",
191 "title": "Housing Support",
192 "description": "Help homeless individuals obtain temporary and long-term housing",
193 "is_active": "1",
194 "is_reserved": "0",
195 "weight": "1",
196 "is_forkable": "1",
197 "is_forked": "",
198 "definition": {
199 "activityTypes": [
200 {"name": "Open Case", "max_instances": "1"}
201 ],
202 "activitySets": [
203 {
204 "name": "standard_timeline",
205 "label": "Standard Timeline",
206 "timeline": "1",
207 "activityTypes": [
208 {
209 "name": "Open Case",
210 "status": "Completed"
211 },
212 {
213 "name": "Medical evaluation",
214 "reference_activity": "Open Case",
215 "reference_offset": "1",
216 "reference_select": "newest"
217 }
218 ]
219 }
220 ],
221 "caseRoles": [
222 {
223 "name": "Homeless Services Coordinator",
224 "creator": "1",
225 "manager": "1"
226 }
227 ]
228 }
229 }
230 };
231 scope = $rootScope.$new();
232 ctrl = $controller('CaseTypeCtrl', {$scope: scope, apiCalls: apiCalls});
233 });
234
235 it('should load activity statuses', function() {
236 expect(scope.activityStatuses).toEqualData(apiCalls.actStatuses.values);
237 });
238
239 it('should load activity types', function() {
240 expect(scope.activityTypes['ADC referral']).toEqualData(apiCalls.actTypes.values[0]);
241 });
242
243 it('addActivitySet should add an activitySet to the case type', function() {
244 scope.addActivitySet('timeline');
245 var activitySets = scope.caseType.definition.activitySets;
246 var newSet = activitySets[activitySets.length - 1];
247 expect(newSet.name).toBe('timeline_1');
248 expect(newSet.timeline).toBe('1');
249 expect(newSet.label).toBe('Timeline');
250 });
251
252 it('addActivitySet handles second timeline correctly', function() {
253 scope.addActivitySet('timeline');
254 scope.addActivitySet('timeline');
255 var activitySets = scope.caseType.definition.activitySets;
256 var newSet = activitySets[activitySets.length - 1];
257 expect(newSet.name).toBe('timeline_2');
258 expect(newSet.timeline).toBe('1');
259 expect(newSet.label).toBe('Timeline #2');
260 });
261 });
262
263 describe('crmAddName', function () {
264 var element;
265
266 beforeEach(function() {
267 scope = $rootScope.$new();
268 scope.activityTypeOptions = [1, 2, 3];
269 element = '<span crm-add-name crm-options="activityTypeOptions"></span>';
270
271 spyOn(CRM.$.fn, 'crmSelect2').and.callThrough();
272
273 element = $compile(element)(scope);
274 scope.$digest();
275 });
276
277 describe('when initialized', function () {
278 var returnValue;
279
280 beforeEach (function () {
281 var dataFunction = CRM.$.fn.crmSelect2.calls.argsFor(0)[0].data;
282 returnValue = dataFunction();
283 });
284
285 it('updates the UI with updated value of scope variable', function () {
286 expect(returnValue).toEqual({ results: scope.activityTypeOptions });
287 });
288 });
289 });
290
291 describe('CaseTypeListCtrl', function () {
292 var caseTypes;
293
294 beforeEach(function () {
295 caseTypes = {
296 values: {
297 1: { id: 1 },
298 2: { id: 2 },
299 3: { id: 3 }
300 }
301 };
302 scope = $rootScope.$new();
303 ctrl = $controller('CaseTypeListCtrl', {
304 $scope: scope,
305 caseTypes: caseTypes
306 });
307 });
308
309 it('should store an index of case types', function() {
310 expect(scope.caseTypes).toEqual(caseTypes.values);
311 });
312 });
313 });