blob: 26b8f68ff03c17b34d28b520b9262ae8221810b5 [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 Scandolo824a7cb2016-04-20 12:24:52 -0700131 it('should combine modelField and customField in a form object', () => {
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700132 expect(service.buildFormStructure(modelField, customField, model)).toEqual(formObject);
133 });
134 });
135
Matteo Scandolo824a7cb2016-04-20 12:24:52 -0700136 describe('The xos-form component', () => {
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700137
138 let element, scope, isolatedScope;
139
140 beforeEach(module('xos.helpers'));
141
142 it('should throw an error if no config is specified', inject(($compile, $rootScope) => {
143 function errorFunctionWrapper(){
144 $compile(angular.element('<xos-form></xos-form>'))($rootScope);
145 $rootScope.$digest();
146 }
147 expect(errorFunctionWrapper).toThrow(new Error('[xosForm] Please provide a configuration via the "config" attribute'));
148 }));
149
150 it('should throw an error if no actions is specified', inject(($compile, $rootScope) => {
151 function errorFunctionWrapper(){
152 let scope = $rootScope.$new();
153 scope.config = 'green';
154 $compile(angular.element('<xos-form config="config"></xos-form>'))(scope);
155 $rootScope.$digest();
156 }
157 expect(errorFunctionWrapper).toThrow(new Error('[xosForm] Please provide an action list in the configuration'));
158 }));
159
160 describe('when correctly configured', () => {
161
162 let cb = jasmine.createSpy('callback');
163
164 beforeEach(inject(($compile, $rootScope) => {
165
166
167 scope = $rootScope.$new();
168
169 scope.config = {
170 exclude: ['excludedField'],
Matteo Scandolo28e49b72016-04-22 14:14:03 -0700171 formName: 'testForm',
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700172 actions: [
173 {
174 label: 'Save',
175 icon: 'ok', // refers to bootstraps glyphicon
176 cb: cb,
177 class: 'success'
178 }
179 ],
Matteo Scandolo824a7cb2016-04-20 12:24:52 -0700180 fields: {
181 first_name: {
182 label: 'Custom Label'
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700183 }
Matteo Scandolo824a7cb2016-04-20 12:24:52 -0700184 }
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700185 };
186
187 scope.model = {
188 id: 1,
189 first_name: 'Jhon',
190 last_name: 'Snow',
191 age: 25,
Matteo Scandolofb667b02016-04-20 16:36:17 -0700192 email: 'test@onlab.us',
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700193 birthDate: '2016-04-18T23:44:16.883181Z',
194 enabled: true,
195 role: 'user', //select
196 a_permissions: [
197
198 ],
199 o_permissions: {
200
201 }
202 };
203
204 element = angular.element(`<xos-form config="config" ng-model="model"></xos-form>`);
205 $compile(element)(scope);
206 scope.$digest();
207 isolatedScope = element.isolateScope().vm;
208 }));
209
210 it('should add excluded properties to the list', () => {
211 let expected = ['id', 'validators', 'created', 'updated', 'deleted', 'backend_status', 'excludedField'];
212 expect(isolatedScope.excludedField).toEqual(expected);
213 });
214
Matteo Scandolofb667b02016-04-20 16:36:17 -0700215 it('should render 8 input field', () => {
216 // boolean are in the form model, but are not input
217 expect(Object.keys(isolatedScope.formField).length).toEqual(9);
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700218 var field = element[0].getElementsByTagName('input');
219 expect(field.length).toEqual(8);
220 });
221
Matteo Scandolofb667b02016-04-20 16:36:17 -0700222 it('should render 1 boolean field', () => {
223 expect($(element).find('.boolean-field > button').length).toEqual(2)
224 });
225
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700226 it('when clicking on action should invoke callback', () => {
Matteo Scandolofb667b02016-04-20 16:36:17 -0700227 var link = $(element).find('[role="button"]');
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700228 link.click();
229 expect(cb).toHaveBeenCalledWith(scope.model);
230 });
231
232 it('should set a custom label', () => {
233 let nameField = element[0].getElementsByClassName('form-group')[0];
234 let label = angular.element(nameField.getElementsByTagName('label')[0]).text()
235 expect(label).toEqual('Custom Label:');
236 });
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700237
Matteo Scandolob389f7a2016-04-20 14:59:39 -0700238 it('should use the correct input type', () => {
239 expect($(element).find('[name="age"]')).toHaveAttr('type', 'number');
240 expect($(element).find('[name="birthDate"]')).toHaveAttr('type', 'date');
Matteo Scandolofb667b02016-04-20 16:36:17 -0700241 expect($(element).find('[name="email"]')).toHaveAttr('type', 'email');
242 });
243
244 describe('the boolean field', () => {
245
246 let setFalse, setTrue;
247
248 beforeEach(() => {
249 setFalse= $(element).find('.boolean-field > button:first-child');
250 setTrue = $(element).find('.boolean-field > button:last-child');
251 });
252
253 it('should change value to false', () => {
254 expect(isolatedScope.ngModel.enabled).toEqual(true);
255 setFalse.click()
256 expect(isolatedScope.ngModel.enabled).toEqual(false);
257 });
258
259 it('should change value to false', () => {
260 isolatedScope.ngModel.enabled = false;
261 scope.$apply();
262 expect(isolatedScope.ngModel.enabled).toEqual(false);
263 setTrue.click()
264 expect(isolatedScope.ngModel.enabled).toEqual(true);
265 });
Matteo Scandolob389f7a2016-04-20 14:59:39 -0700266 });
Matteo Scandolo28e49b72016-04-22 14:14:03 -0700267
268 describe('the custom validation options', () => {
269 beforeEach(() => {
270 scope.config.fields.first_name.validators = {
271 minlength: 10,
272 maxlength: 15,
273 required: true
274 };
275
Matteo Scandolo953ddad2016-04-25 08:15:24 -0700276 scope.config.fields.age = {
277 validators: {
278 min: 10,
279 max: 20
280 }
281 };
282
Matteo Scandolo28e49b72016-04-22 14:14:03 -0700283 scope.$digest();
284 });
285
286 it('should validate required', () => {
287 scope.model.first_name = null;
288 scope.$digest();
289
290 expect(isolatedScope.testForm.first_name.$valid).toBeFalsy();
291 expect(isolatedScope.testForm.first_name.$error.required).toBeTruthy();
292 });
293
294 it('should validate minlength', () => {
295 scope.model.first_name = 'short';
296 scope.$digest();
297
298 expect(isolatedScope.testForm.first_name.$valid).toBeFalsy();
299 expect(isolatedScope.testForm.first_name.$error.minlength).toBeTruthy();
300 });
301
302 it('should validate maxlength', () => {
303 scope.model.first_name = 'this is way too long!';
304 scope.$digest();
305
306 expect(isolatedScope.testForm.first_name.$valid).toBeFalsy();
307 expect(isolatedScope.testForm.first_name.$error.maxlength).toBeTruthy();
308 });
Matteo Scandolo953ddad2016-04-25 08:15:24 -0700309
310 xit('should validate min', () => {
311 // not validating min and max for now
312 scope.model.age = 8;
313 scope.$digest();
314
315 expect(isolatedScope.testForm.age.$valid).toBeFalsy();
316 expect(isolatedScope.testForm.age.$error.min).toBeTruthy();
317 });
Matteo Scandolo28e49b72016-04-22 14:14:03 -0700318 });
Matteo Scandolob389f7a2016-04-20 14:59:39 -0700319 });
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700320 });
321 });
322})();