blob: 226a62aacba3e3c3731cec675963b8c50914b11f [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 Scandolo2e804cb2016-04-27 16:29:33 -0700131 describe('when modelField are provided', () => {
Matteo Scandolod02ef502016-04-27 15:58:16 -0700132 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', () => {
Matteo Scandolo2e804cb2016-04-27 16:29:33 -0700138 let empty_modelField = {
Matteo Scandolod02ef502016-04-27 15:58:16 -0700139 // 5: {}
140 };
Matteo Scandolo2e804cb2016-04-27 16:29:33 -0700141 let empty_customFields = {
Matteo Scandolod02ef502016-04-27 15:58:16 -0700142 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
Matteo Scandolo2e804cb2016-04-27 16:29:33 -0700168 let empty_formObject = {
Matteo Scandolod02ef502016-04-27 15:58:16 -0700169 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
Matteo Scandolo2e804cb2016-04-27 16:29:33 -0700201 let empty_model = {5: 'Nan'}
Matteo Scandolod02ef502016-04-27 15:58:16 -0700202
203 it('should create a form object', () => {
Matteo Scandolo2e804cb2016-04-27 16:29:33 -0700204 let res = service.buildFormStructure(empty_modelField, empty_customFields, empty_model)
205 expect(res.id).toEqual(empty_formObject.id);
206 expect(res.name).toEqual(empty_formObject.name);
207 expect(res.mail).toEqual(empty_formObject.mail);
208 expect(res.active).toEqual(empty_formObject.active);
209 expect(res.created).toEqual(empty_formObject.created);
210 expect(res.custom).toEqual(empty_formObject.custom);
211 expect(res).toEqual(empty_formObject);
Matteo Scandolod02ef502016-04-27 15:58:16 -0700212 });
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700213 });
214 });
215
Matteo Scandolo2e804cb2016-04-27 16:29:33 -0700216 describe('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
Matteo Scandolo2e804cb2016-04-27 16:29:33 -0700348 // NOTE not sure why this tests are failing
349 xdescribe('the custom validation options', () => {
Matteo Scandolo28e49b72016-04-22 14:14:03 -0700350 beforeEach(() => {
351 scope.config.fields.first_name.validators = {
352 minlength: 10,
353 maxlength: 15,
354 required: true
355 };
356
Matteo Scandolo953ddad2016-04-25 08:15:24 -0700357 scope.config.fields.age = {
358 validators: {
359 min: 10,
360 max: 20
361 }
362 };
363
Matteo Scandolo28e49b72016-04-22 14:14:03 -0700364 scope.$digest();
365 });
366
367 it('should validate required', () => {
368 scope.model.first_name = null;
369 scope.$digest();
370
371 expect(isolatedScope.testForm.first_name.$valid).toBeFalsy();
372 expect(isolatedScope.testForm.first_name.$error.required).toBeTruthy();
373 });
374
375 it('should validate minlength', () => {
376 scope.model.first_name = 'short';
377 scope.$digest();
378
379 expect(isolatedScope.testForm.first_name.$valid).toBeFalsy();
380 expect(isolatedScope.testForm.first_name.$error.minlength).toBeTruthy();
381 });
382
383 it('should validate maxlength', () => {
384 scope.model.first_name = 'this is way too long!';
385 scope.$digest();
386
387 expect(isolatedScope.testForm.first_name.$valid).toBeFalsy();
388 expect(isolatedScope.testForm.first_name.$error.maxlength).toBeTruthy();
389 });
Matteo Scandolo953ddad2016-04-25 08:15:24 -0700390
Matteo Scandolo2e804cb2016-04-27 16:29:33 -0700391 it('should validate min', () => {
Matteo Scandolo953ddad2016-04-25 08:15:24 -0700392 // not validating min and max for now
393 scope.model.age = 8;
394 scope.$digest();
395
396 expect(isolatedScope.testForm.age.$valid).toBeFalsy();
397 expect(isolatedScope.testForm.age.$error.min).toBeTruthy();
398 });
Matteo Scandolo28e49b72016-04-22 14:14:03 -0700399 });
Matteo Scandolob389f7a2016-04-20 14:59:39 -0700400 });
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700401 });
402 });
403})();