blob: 4321410a18f1758f73545f6387f1f222598584b8 [file] [log] [blame]
Matteo Scandoloc49f53c2016-04-20 11:38:42 -07001/**
2 * © OpenCORD
3 *
4 * Created by teone on 4/18/16.
5 */
6
7(function () {
8 'use strict';
9
10 describe('The xos.helper module', function(){
11
12 describe('The XosFormHelper service', () => {
13 let service;
14
15 let fields = [
16 'id',
17 'name',
Matteo Scandolob389f7a2016-04-20 14:59:39 -070018 'mail',
Matteo Scandoloc49f53c2016-04-20 11:38:42 -070019 'active',
20 'created',
21 'custom'
22 ];
23
24 let modelField = {
25 id: {},
26 name: {},
Matteo Scandolob389f7a2016-04-20 14:59:39 -070027 mail: {},
Matteo Scandoloc49f53c2016-04-20 11:38:42 -070028 active: {},
29 created: {},
30 custom: {}
31 };
32
33 let model = {
34 id: 1,
35 name: 'test',
Matteo Scandolob389f7a2016-04-20 14:59:39 -070036 mail: 'test@onlab.us',
Matteo Scandoloc49f53c2016-04-20 11:38:42 -070037 active: true,
38 created: '2016-04-18T23:44:16.883181Z',
39 custom: 'MyCustomValue'
40 };
41
42 let customField = {
43 custom: {
44 label: 'Custom Label',
45 type: 'number',
46 validators: {}
47 }
48 };
49
50 let formObject = {
51 id: {
52 label: 'Id:',
53 type: 'number',
54 validators: {}
55 },
56 name: {
57 label: 'Name:',
58 type: 'string',
59 validators: {}
60 },
Matteo Scandolob389f7a2016-04-20 14:59:39 -070061 mail: {
62 label: 'Mail:',
63 type: 'email',
64 validators: {}
65 },
Matteo Scandoloc49f53c2016-04-20 11:38:42 -070066 active: {
67 label: 'Active:',
68 type: 'boolean',
69 validators: {}
70 },
71 created: {
72 label: 'Created:',
73 type: 'date',
74 validators: {}
75 },
76 custom: {
77 label: 'Custom Label:',
78 type: 'number',
79 validators: {}
80 }
81 };
82
83 // load the application module
84 beforeEach(module('xos.helpers'));
85
86 // inject the cartService
87 beforeEach(inject(function (_XosFormHelpers_) {
88 // The injector unwraps the underscores (_) from around the parameter names when matching
89 service = _XosFormHelpers_;
90 }));
91
Matteo Scandolob389f7a2016-04-20 14:59:39 -070092 describe('the _isEmail method', () => {
93 it('should return true', () => {
94 expect(service._isEmail('test@onlab.us')).toEqual(true);
95 });
96 it('should return false', () => {
97 expect(service._isEmail('testonlab.us')).toEqual(false);
98 expect(service._isEmail('test@onlab')).toEqual(false);
99 });
100 });
101
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700102 describe('the _getFieldFormat method', () => {
Matteo Scandolo824a7cb2016-04-20 12:24:52 -0700103 it('should return string', () => {
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700104 expect(service._getFieldFormat('string')).toEqual('string');
Matteo Scandolo28e49b72016-04-22 14:14:03 -0700105 expect(service._getFieldFormat(null)).toEqual('string');
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700106 });
Matteo Scandolob389f7a2016-04-20 14:59:39 -0700107 it('should return mail', () => {
108 expect(service._getFieldFormat('test@onlab.us')).toEqual('email');
109 });
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700110 it('should return number', () => {
111 expect(service._getFieldFormat(1)).toEqual('number');
Matteo Scandolo5fe58df2016-04-26 16:34:56 -0700112 // this is skipped because not realistic and js Date sucks
113 // expect(service._getFieldFormat('1')).toEqual('number');
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700114 });
Matteo Scandolo824a7cb2016-04-20 12:24:52 -0700115 it('should return boolean', () => {
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700116 expect(service._getFieldFormat(false)).toEqual('boolean');
117 expect(service._getFieldFormat(true)).toEqual('boolean');
118 });
119
120 it('should return date', () => {
121 expect(service._getFieldFormat('2016-04-19T23:09:1092Z')).toEqual('string');
122 expect(service._getFieldFormat(new Date())).toEqual('date');
123 expect(service._getFieldFormat('2016-04-19T23:09:10.208092Z')).toEqual('date');
124 });
125 });
126
Matteo Scandolo824a7cb2016-04-20 12:24:52 -0700127 it('should convert the fields array in an empty form object', () => {
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700128 expect(service.parseModelField(fields)).toEqual(modelField);
129 });
130
Matteo Scandolod02ef502016-04-27 15:58:16 -0700131 xdescribe('when modelField are provided', () => {
132 it('should combine modelField and customField in a form object', () => {
133 expect(service.buildFormStructure(modelField, customField, model)).toEqual(formObject);
134 });
135 });
136
137 describe('when model field is an empty array', () => {
138 let modelField = {
139 // 5: {}
140 };
141 let customFields = {
142 id: {
143 label: 'Id',
144 type: 'number'
145 },
146 name: {
147 label: 'Name',
148 type: 'string'
149 },
150 mail: {
151 label: 'Mail',
152 type: 'email'
153 },
154 active: {
155 label: 'Active',
156 type: 'boolean'
157 },
158 created: {
159 label: 'Created',
160 type: 'date'
161 },
162 custom: {
163 label: 'Custom Label',
164 type: 'number'
165 }
166 };
167
168 let formObject = {
169 id: {
170 label: 'Id:',
171 type: 'number',
172 validators: {}
173 },
174 name: {
175 label: 'Name:',
176 type: 'string',
177 validators: {}
178 },
179 mail: {
180 label: 'Mail:',
181 type: 'email',
182 validators: {}
183 },
184 active: {
185 label: 'Active:',
186 type: 'boolean',
187 validators: {}
188 },
189 created: {
190 label: 'Created:',
191 type: 'date',
192 validators: {}
193 },
194 custom: {
195 label: 'Custom Label:',
196 type: 'number',
197 validators: {}
198 }
199 };
200
201 let model = {5: 'Nan'}
202
203 it('should create a form object', () => {
204 let res = service.buildFormStructure(modelField, customFields, model)
205 expect(res.id).toEqual(formObject.id);
206 expect(res.name).toEqual(formObject.name);
207 expect(res.mail).toEqual(formObject.mail);
208 expect(res.active).toEqual(formObject.active);
209 expect(res.created).toEqual(formObject.created);
210 expect(res.custom).toEqual(formObject.custom);
211 expect(res).toEqual(formObject);
212 });
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700213 });
214 });
215
Matteo Scandolod02ef502016-04-27 15:58:16 -0700216 xdescribe('The xos-form component', () => {
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700217
218 let element, scope, isolatedScope;
219
220 beforeEach(module('xos.helpers'));
221
222 it('should throw an error if no config is specified', inject(($compile, $rootScope) => {
223 function errorFunctionWrapper(){
224 $compile(angular.element('<xos-form></xos-form>'))($rootScope);
225 $rootScope.$digest();
226 }
227 expect(errorFunctionWrapper).toThrow(new Error('[xosForm] Please provide a configuration via the "config" attribute'));
228 }));
229
230 it('should throw an error if no actions is specified', inject(($compile, $rootScope) => {
231 function errorFunctionWrapper(){
232 let scope = $rootScope.$new();
233 scope.config = 'green';
234 $compile(angular.element('<xos-form config="config"></xos-form>'))(scope);
235 $rootScope.$digest();
236 }
237 expect(errorFunctionWrapper).toThrow(new Error('[xosForm] Please provide an action list in the configuration'));
238 }));
239
240 describe('when correctly configured', () => {
241
242 let cb = jasmine.createSpy('callback');
243
244 beforeEach(inject(($compile, $rootScope) => {
245
246
247 scope = $rootScope.$new();
248
249 scope.config = {
250 exclude: ['excludedField'],
Matteo Scandolo28e49b72016-04-22 14:14:03 -0700251 formName: 'testForm',
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700252 actions: [
253 {
254 label: 'Save',
255 icon: 'ok', // refers to bootstraps glyphicon
256 cb: cb,
257 class: 'success'
258 }
259 ],
Matteo Scandolo824a7cb2016-04-20 12:24:52 -0700260 fields: {
261 first_name: {
262 label: 'Custom Label'
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700263 }
Matteo Scandolo824a7cb2016-04-20 12:24:52 -0700264 }
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700265 };
266
267 scope.model = {
268 id: 1,
269 first_name: 'Jhon',
270 last_name: 'Snow',
271 age: 25,
Matteo Scandolofb667b02016-04-20 16:36:17 -0700272 email: 'test@onlab.us',
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700273 birthDate: '2016-04-18T23:44:16.883181Z',
274 enabled: true,
275 role: 'user', //select
276 a_permissions: [
277
278 ],
279 o_permissions: {
280
281 }
282 };
283
284 element = angular.element(`<xos-form config="config" ng-model="model"></xos-form>`);
285 $compile(element)(scope);
286 scope.$digest();
287 isolatedScope = element.isolateScope().vm;
288 }));
289
290 it('should add excluded properties to the list', () => {
291 let expected = ['id', 'validators', 'created', 'updated', 'deleted', 'backend_status', 'excludedField'];
292 expect(isolatedScope.excludedField).toEqual(expected);
293 });
294
Matteo Scandolofb667b02016-04-20 16:36:17 -0700295 it('should render 8 input field', () => {
296 // boolean are in the form model, but are not input
297 expect(Object.keys(isolatedScope.formField).length).toEqual(9);
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700298 var field = element[0].getElementsByTagName('input');
299 expect(field.length).toEqual(8);
300 });
301
Matteo Scandolofb667b02016-04-20 16:36:17 -0700302 it('should render 1 boolean field', () => {
303 expect($(element).find('.boolean-field > button').length).toEqual(2)
304 });
305
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700306 it('when clicking on action should invoke callback', () => {
Matteo Scandolofb667b02016-04-20 16:36:17 -0700307 var link = $(element).find('[role="button"]');
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700308 link.click();
309 expect(cb).toHaveBeenCalledWith(scope.model);
310 });
311
312 it('should set a custom label', () => {
313 let nameField = element[0].getElementsByClassName('form-group')[0];
314 let label = angular.element(nameField.getElementsByTagName('label')[0]).text()
315 expect(label).toEqual('Custom Label:');
316 });
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700317
Matteo Scandolob389f7a2016-04-20 14:59:39 -0700318 it('should use the correct input type', () => {
319 expect($(element).find('[name="age"]')).toHaveAttr('type', 'number');
320 expect($(element).find('[name="birthDate"]')).toHaveAttr('type', 'date');
Matteo Scandolofb667b02016-04-20 16:36:17 -0700321 expect($(element).find('[name="email"]')).toHaveAttr('type', 'email');
322 });
323
324 describe('the boolean field', () => {
325
326 let setFalse, setTrue;
327
328 beforeEach(() => {
329 setFalse= $(element).find('.boolean-field > button:first-child');
330 setTrue = $(element).find('.boolean-field > button:last-child');
331 });
332
333 it('should change value to false', () => {
334 expect(isolatedScope.ngModel.enabled).toEqual(true);
335 setFalse.click()
336 expect(isolatedScope.ngModel.enabled).toEqual(false);
337 });
338
339 it('should change value to false', () => {
340 isolatedScope.ngModel.enabled = false;
341 scope.$apply();
342 expect(isolatedScope.ngModel.enabled).toEqual(false);
343 setTrue.click()
344 expect(isolatedScope.ngModel.enabled).toEqual(true);
345 });
Matteo Scandolob389f7a2016-04-20 14:59:39 -0700346 });
Matteo Scandolo28e49b72016-04-22 14:14:03 -0700347
348 describe('the custom validation options', () => {
349 beforeEach(() => {
350 scope.config.fields.first_name.validators = {
351 minlength: 10,
352 maxlength: 15,
353 required: true
354 };
355
Matteo Scandolo953ddad2016-04-25 08:15:24 -0700356 scope.config.fields.age = {
357 validators: {
358 min: 10,
359 max: 20
360 }
361 };
362
Matteo Scandolo28e49b72016-04-22 14:14:03 -0700363 scope.$digest();
364 });
365
366 it('should validate required', () => {
367 scope.model.first_name = null;
368 scope.$digest();
369
370 expect(isolatedScope.testForm.first_name.$valid).toBeFalsy();
371 expect(isolatedScope.testForm.first_name.$error.required).toBeTruthy();
372 });
373
374 it('should validate minlength', () => {
375 scope.model.first_name = 'short';
376 scope.$digest();
377
378 expect(isolatedScope.testForm.first_name.$valid).toBeFalsy();
379 expect(isolatedScope.testForm.first_name.$error.minlength).toBeTruthy();
380 });
381
382 it('should validate maxlength', () => {
383 scope.model.first_name = 'this is way too long!';
384 scope.$digest();
385
386 expect(isolatedScope.testForm.first_name.$valid).toBeFalsy();
387 expect(isolatedScope.testForm.first_name.$error.maxlength).toBeTruthy();
388 });
Matteo Scandolo953ddad2016-04-25 08:15:24 -0700389
390 xit('should validate min', () => {
391 // not validating min and max for now
392 scope.model.age = 8;
393 scope.$digest();
394
395 expect(isolatedScope.testForm.age.$valid).toBeFalsy();
396 expect(isolatedScope.testForm.age.$error.min).toBeTruthy();
397 });
Matteo Scandolo28e49b72016-04-22 14:14:03 -0700398 });
Matteo Scandolob389f7a2016-04-20 14:59:39 -0700399 });
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700400 });
401 });
402})();