blob: 7673fc8737fe71c5dfff42e5041d6758671c4bc9 [file] [log] [blame]
Matteo Scandolo7bc39c42016-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
Matteo Scandolo974c0e42016-05-25 16:02:16 -070012 // TODO move in separate file
Matteo Scandolo7bc39c42016-04-20 11:38:42 -070013 describe('The XosFormHelper service', () => {
14 let service;
15
16 let fields = [
17 'id',
18 'name',
Matteo Scandolo6e2e6ff2016-04-20 14:59:39 -070019 'mail',
Matteo Scandolo7bc39c42016-04-20 11:38:42 -070020 'active',
21 'created',
22 'custom'
23 ];
24
25 let modelField = {
26 id: {},
27 name: {},
Matteo Scandolo6e2e6ff2016-04-20 14:59:39 -070028 mail: {},
Matteo Scandolo7bc39c42016-04-20 11:38:42 -070029 active: {},
30 created: {},
31 custom: {}
32 };
33
34 let model = {
35 id: 1,
36 name: 'test',
Matteo Scandolo6e2e6ff2016-04-20 14:59:39 -070037 mail: 'test@onlab.us',
Matteo Scandolo7bc39c42016-04-20 11:38:42 -070038 active: true,
39 created: '2016-04-18T23:44:16.883181Z',
40 custom: 'MyCustomValue'
41 };
42
43 let customField = {
44 custom: {
45 label: 'Custom Label',
46 type: 'number',
47 validators: {}
48 }
49 };
50
51 let formObject = {
52 id: {
53 label: 'Id:',
54 type: 'number',
55 validators: {}
56 },
57 name: {
58 label: 'Name:',
59 type: 'string',
60 validators: {}
61 },
Matteo Scandolo6e2e6ff2016-04-20 14:59:39 -070062 mail: {
63 label: 'Mail:',
64 type: 'email',
65 validators: {}
66 },
Matteo Scandolo7bc39c42016-04-20 11:38:42 -070067 active: {
68 label: 'Active:',
69 type: 'boolean',
70 validators: {}
71 },
72 created: {
73 label: 'Created:',
74 type: 'date',
75 validators: {}
76 },
77 custom: {
78 label: 'Custom Label:',
79 type: 'number',
80 validators: {}
81 }
82 };
83
84 // load the application module
85 beforeEach(module('xos.helpers'));
86
87 // inject the cartService
88 beforeEach(inject(function (_XosFormHelpers_) {
89 // The injector unwraps the underscores (_) from around the parameter names when matching
90 service = _XosFormHelpers_;
91 }));
92
Matteo Scandolo6e2e6ff2016-04-20 14:59:39 -070093 describe('the _isEmail method', () => {
94 it('should return true', () => {
95 expect(service._isEmail('test@onlab.us')).toEqual(true);
96 });
97 it('should return false', () => {
98 expect(service._isEmail('testonlab.us')).toEqual(false);
99 expect(service._isEmail('test@onlab')).toEqual(false);
100 });
101 });
102
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700103 describe('the _getFieldFormat method', () => {
Matteo Scandolo9f0e5ae2016-04-20 12:24:52 -0700104 it('should return string', () => {
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700105 expect(service._getFieldFormat('string')).toEqual('string');
Matteo Scandolob3d686f2016-04-22 14:14:03 -0700106 expect(service._getFieldFormat(null)).toEqual('string');
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700107 });
Matteo Scandolo6e2e6ff2016-04-20 14:59:39 -0700108 it('should return mail', () => {
109 expect(service._getFieldFormat('test@onlab.us')).toEqual('email');
110 });
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700111 it('should return number', () => {
112 expect(service._getFieldFormat(1)).toEqual('number');
Matteo Scandoloa9524672016-04-26 16:34:56 -0700113 // this is skipped because not realistic and js Date sucks
114 // expect(service._getFieldFormat('1')).toEqual('number');
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700115 });
Matteo Scandolo9f0e5ae2016-04-20 12:24:52 -0700116 it('should return boolean', () => {
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700117 expect(service._getFieldFormat(false)).toEqual('boolean');
118 expect(service._getFieldFormat(true)).toEqual('boolean');
119 });
120
121 it('should return date', () => {
122 expect(service._getFieldFormat('2016-04-19T23:09:1092Z')).toEqual('string');
123 expect(service._getFieldFormat(new Date())).toEqual('date');
124 expect(service._getFieldFormat('2016-04-19T23:09:10.208092Z')).toEqual('date');
125 });
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700126
127 it('should return array', () => {
128 expect(service._getFieldFormat([])).toEqual('array');
129 expect(service._getFieldFormat(['a', 'b'])).toEqual('array');
130 });
131
132 it('should return object', () => {
133 expect(service._getFieldFormat({})).toEqual('object');
134 expect(service._getFieldFormat({foo: 'bar'})).toEqual('object');
135 });
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700136 });
137
Matteo Scandolo9f0e5ae2016-04-20 12:24:52 -0700138 it('should convert the fields array in an empty form object', () => {
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700139 expect(service.parseModelField(fields)).toEqual(modelField);
140 });
141
Matteo Scandolo6ba12872016-04-27 16:29:33 -0700142 describe('when modelField are provided', () => {
Matteo Scandoloe2ee2d92016-04-27 15:58:16 -0700143 it('should combine modelField and customField in a form object', () => {
144 expect(service.buildFormStructure(modelField, customField, model)).toEqual(formObject);
145 });
146 });
147
148 describe('when model field is an empty array', () => {
Matteo Scandolo6ba12872016-04-27 16:29:33 -0700149 let empty_modelField = {
Matteo Scandoloe2ee2d92016-04-27 15:58:16 -0700150 // 5: {}
151 };
Matteo Scandolo6ba12872016-04-27 16:29:33 -0700152 let empty_customFields = {
Matteo Scandoloe2ee2d92016-04-27 15:58:16 -0700153 id: {
154 label: 'Id',
155 type: 'number'
156 },
157 name: {
158 label: 'Name',
159 type: 'string'
160 },
161 mail: {
162 label: 'Mail',
163 type: 'email'
164 },
165 active: {
166 label: 'Active',
167 type: 'boolean'
168 },
169 created: {
170 label: 'Created',
171 type: 'date'
172 },
173 custom: {
174 label: 'Custom Label',
175 type: 'number'
176 }
177 };
178
Matteo Scandolo6ba12872016-04-27 16:29:33 -0700179 let empty_formObject = {
Matteo Scandoloe2ee2d92016-04-27 15:58:16 -0700180 id: {
181 label: 'Id:',
182 type: 'number',
183 validators: {}
184 },
185 name: {
186 label: 'Name:',
187 type: 'string',
188 validators: {}
189 },
190 mail: {
191 label: 'Mail:',
192 type: 'email',
193 validators: {}
194 },
195 active: {
196 label: 'Active:',
197 type: 'boolean',
198 validators: {}
199 },
200 created: {
201 label: 'Created:',
202 type: 'date',
203 validators: {}
204 },
205 custom: {
206 label: 'Custom Label:',
207 type: 'number',
208 validators: {}
209 }
210 };
211
Matteo Scandolo6ba12872016-04-27 16:29:33 -0700212 let empty_model = {5: 'Nan'}
Matteo Scandoloe2ee2d92016-04-27 15:58:16 -0700213
214 it('should create a form object', () => {
Matteo Scandolo6ba12872016-04-27 16:29:33 -0700215 let res = service.buildFormStructure(empty_modelField, empty_customFields, empty_model)
216 expect(res.id).toEqual(empty_formObject.id);
217 expect(res.name).toEqual(empty_formObject.name);
218 expect(res.mail).toEqual(empty_formObject.mail);
219 expect(res.active).toEqual(empty_formObject.active);
220 expect(res.created).toEqual(empty_formObject.created);
221 expect(res.custom).toEqual(empty_formObject.custom);
222 expect(res).toEqual(empty_formObject);
Matteo Scandoloe2ee2d92016-04-27 15:58:16 -0700223 });
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700224 });
225 });
226
Matteo Scandolo6ba12872016-04-27 16:29:33 -0700227 describe('The xos-form component', () => {
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700228
229 let element, scope, isolatedScope;
230
231 beforeEach(module('xos.helpers'));
232
233 it('should throw an error if no config is specified', inject(($compile, $rootScope) => {
234 function errorFunctionWrapper(){
235 $compile(angular.element('<xos-form></xos-form>'))($rootScope);
236 $rootScope.$digest();
237 }
238 expect(errorFunctionWrapper).toThrow(new Error('[xosForm] Please provide a configuration via the "config" attribute'));
239 }));
240
241 it('should throw an error if no actions is specified', inject(($compile, $rootScope) => {
242 function errorFunctionWrapper(){
243 let scope = $rootScope.$new();
244 scope.config = 'green';
245 $compile(angular.element('<xos-form config="config"></xos-form>'))(scope);
246 $rootScope.$digest();
247 }
248 expect(errorFunctionWrapper).toThrow(new Error('[xosForm] Please provide an action list in the configuration'));
249 }));
250
251 describe('when correctly configured', () => {
252
253 let cb = jasmine.createSpy('callback');
254
255 beforeEach(inject(($compile, $rootScope) => {
256
257
258 scope = $rootScope.$new();
259
260 scope.config = {
261 exclude: ['excludedField'],
Matteo Scandolob3d686f2016-04-22 14:14:03 -0700262 formName: 'testForm',
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700263 actions: [
264 {
265 label: 'Save',
266 icon: 'ok', // refers to bootstraps glyphicon
267 cb: cb,
268 class: 'success'
269 }
270 ],
Matteo Scandolo9f0e5ae2016-04-20 12:24:52 -0700271 fields: {
272 first_name: {
273 label: 'Custom Label'
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700274 }
Matteo Scandolo9f0e5ae2016-04-20 12:24:52 -0700275 }
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700276 };
277
278 scope.model = {
279 id: 1,
280 first_name: 'Jhon',
281 last_name: 'Snow',
282 age: 25,
Matteo Scandolo4ba4cf12016-04-20 16:36:17 -0700283 email: 'test@onlab.us',
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700284 birthDate: '2016-04-18T23:44:16.883181Z',
285 enabled: true,
286 role: 'user', //select
287 a_permissions: [
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700288 ],
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700289 object_field: {
290 string: 'bar',
291 number: 1,
292 email: 'teo@onlab.us'
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700293 }
294 };
295
296 element = angular.element(`<xos-form config="config" ng-model="model"></xos-form>`);
297 $compile(element)(scope);
298 scope.$digest();
299 isolatedScope = element.isolateScope().vm;
300 }));
301
302 it('should add excluded properties to the list', () => {
303 let expected = ['id', 'validators', 'created', 'updated', 'deleted', 'backend_status', 'excludedField'];
304 expect(isolatedScope.excludedField).toEqual(expected);
305 });
306
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700307 xit('should render 8 input field', () => {
Matteo Scandolo4ba4cf12016-04-20 16:36:17 -0700308 // boolean are in the form model, but are not input
309 expect(Object.keys(isolatedScope.formField).length).toEqual(9);
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700310 var field = element[0].getElementsByTagName('input');
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700311 expect(field.length).toEqual(10);
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700312 });
313
Matteo Scandolo4ba4cf12016-04-20 16:36:17 -0700314 it('should render 1 boolean field', () => {
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700315 // console.log($(element).find('.boolean-field'));
Matteo Scandolo4ba4cf12016-04-20 16:36:17 -0700316 expect($(element).find('.boolean-field > button').length).toEqual(2)
317 });
318
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700319 it('when clicking on action should invoke callback', () => {
Matteo Scandolo4ba4cf12016-04-20 16:36:17 -0700320 var link = $(element).find('[role="button"]');
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700321 link.click();
322 expect(cb).toHaveBeenCalledWith(scope.model);
323 });
324
325 it('should set a custom label', () => {
326 let nameField = element[0].getElementsByClassName('form-group')[0];
327 let label = angular.element(nameField.getElementsByTagName('label')[0]).text()
328 expect(label).toEqual('Custom Label:');
329 });
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700330
Matteo Scandolo6e2e6ff2016-04-20 14:59:39 -0700331 it('should use the correct input type', () => {
332 expect($(element).find('[name="age"]')).toHaveAttr('type', 'number');
333 expect($(element).find('[name="birthDate"]')).toHaveAttr('type', 'date');
Matteo Scandolo4ba4cf12016-04-20 16:36:17 -0700334 expect($(element).find('[name="email"]')).toHaveAttr('type', 'email');
335 });
336
337 describe('the boolean field', () => {
338
339 let setFalse, setTrue;
340
341 beforeEach(() => {
342 setFalse= $(element).find('.boolean-field > button:first-child');
343 setTrue = $(element).find('.boolean-field > button:last-child');
344 });
345
346 it('should change value to false', () => {
347 expect(isolatedScope.ngModel.enabled).toEqual(true);
348 setFalse.click()
349 expect(isolatedScope.ngModel.enabled).toEqual(false);
350 });
351
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700352 it('should change value to true', () => {
Matteo Scandolo4ba4cf12016-04-20 16:36:17 -0700353 isolatedScope.ngModel.enabled = false;
354 scope.$apply();
355 expect(isolatedScope.ngModel.enabled).toEqual(false);
356 setTrue.click()
357 expect(isolatedScope.ngModel.enabled).toEqual(true);
358 });
Matteo Scandolo6e2e6ff2016-04-20 14:59:39 -0700359 });
Matteo Scandolob3d686f2016-04-22 14:14:03 -0700360
Matteo Scandolo6ba12872016-04-27 16:29:33 -0700361 // NOTE not sure why this tests are failing
362 xdescribe('the custom validation options', () => {
Matteo Scandolob3d686f2016-04-22 14:14:03 -0700363 beforeEach(() => {
364 scope.config.fields.first_name.validators = {
365 minlength: 10,
366 maxlength: 15,
367 required: true
368 };
369
Matteo Scandolo01c87572016-04-25 08:15:24 -0700370 scope.config.fields.age = {
371 validators: {
372 min: 10,
373 max: 20
374 }
375 };
376
Matteo Scandolob3d686f2016-04-22 14:14:03 -0700377 scope.$digest();
378 });
379
380 it('should validate required', () => {
381 scope.model.first_name = null;
382 scope.$digest();
383
384 expect(isolatedScope.testForm.first_name.$valid).toBeFalsy();
385 expect(isolatedScope.testForm.first_name.$error.required).toBeTruthy();
386 });
387
388 it('should validate minlength', () => {
389 scope.model.first_name = 'short';
390 scope.$digest();
391
392 expect(isolatedScope.testForm.first_name.$valid).toBeFalsy();
393 expect(isolatedScope.testForm.first_name.$error.minlength).toBeTruthy();
394 });
395
396 it('should validate maxlength', () => {
397 scope.model.first_name = 'this is way too long!';
398 scope.$digest();
399
400 expect(isolatedScope.testForm.first_name.$valid).toBeFalsy();
401 expect(isolatedScope.testForm.first_name.$error.maxlength).toBeTruthy();
402 });
Matteo Scandolo01c87572016-04-25 08:15:24 -0700403
Matteo Scandolo6ba12872016-04-27 16:29:33 -0700404 it('should validate min', () => {
Matteo Scandolo01c87572016-04-25 08:15:24 -0700405 // not validating min and max for now
406 scope.model.age = 8;
407 scope.$digest();
408
409 expect(isolatedScope.testForm.age.$valid).toBeFalsy();
410 expect(isolatedScope.testForm.age.$error.min).toBeTruthy();
411 });
Matteo Scandolob3d686f2016-04-22 14:14:03 -0700412 });
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700413
414 describe('the object field', () => {
415
416 });
Matteo Scandolo6e2e6ff2016-04-20 14:59:39 -0700417 });
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700418 });
419 });
420})();