Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 1 | /** |
| 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 Scandolo | b389f7a | 2016-04-20 14:59:39 -0700 | [diff] [blame] | 18 | 'mail', |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 19 | 'active', |
| 20 | 'created', |
| 21 | 'custom' |
| 22 | ]; |
| 23 | |
| 24 | let modelField = { |
| 25 | id: {}, |
| 26 | name: {}, |
Matteo Scandolo | b389f7a | 2016-04-20 14:59:39 -0700 | [diff] [blame] | 27 | mail: {}, |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 28 | active: {}, |
| 29 | created: {}, |
| 30 | custom: {} |
| 31 | }; |
| 32 | |
| 33 | let model = { |
| 34 | id: 1, |
| 35 | name: 'test', |
Matteo Scandolo | b389f7a | 2016-04-20 14:59:39 -0700 | [diff] [blame] | 36 | mail: 'test@onlab.us', |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 37 | 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 Scandolo | b389f7a | 2016-04-20 14:59:39 -0700 | [diff] [blame] | 61 | mail: { |
| 62 | label: 'Mail:', |
| 63 | type: 'email', |
| 64 | validators: {} |
| 65 | }, |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 66 | 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 Scandolo | b389f7a | 2016-04-20 14:59:39 -0700 | [diff] [blame] | 92 | 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 Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 102 | describe('the _getFieldFormat method', () => { |
Matteo Scandolo | 824a7cb | 2016-04-20 12:24:52 -0700 | [diff] [blame] | 103 | it('should return string', () => { |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 104 | expect(service._getFieldFormat('string')).toEqual('string'); |
Matteo Scandolo | 28e49b7 | 2016-04-22 14:14:03 -0700 | [diff] [blame] | 105 | expect(service._getFieldFormat(null)).toEqual('string'); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 106 | }); |
Matteo Scandolo | b389f7a | 2016-04-20 14:59:39 -0700 | [diff] [blame] | 107 | it('should return mail', () => { |
| 108 | expect(service._getFieldFormat('test@onlab.us')).toEqual('email'); |
| 109 | }); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 110 | it('should return number', () => { |
| 111 | expect(service._getFieldFormat(1)).toEqual('number'); |
Matteo Scandolo | 5fe58df | 2016-04-26 16:34:56 -0700 | [diff] [blame] | 112 | // this is skipped because not realistic and js Date sucks |
| 113 | // expect(service._getFieldFormat('1')).toEqual('number'); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 114 | }); |
Matteo Scandolo | 824a7cb | 2016-04-20 12:24:52 -0700 | [diff] [blame] | 115 | it('should return boolean', () => { |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 116 | 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 Scandolo | 824a7cb | 2016-04-20 12:24:52 -0700 | [diff] [blame] | 127 | it('should convert the fields array in an empty form object', () => { |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 128 | expect(service.parseModelField(fields)).toEqual(modelField); |
| 129 | }); |
| 130 | |
Matteo Scandolo | 2e804cb | 2016-04-27 16:29:33 -0700 | [diff] [blame] | 131 | describe('when modelField are provided', () => { |
Matteo Scandolo | d02ef50 | 2016-04-27 15:58:16 -0700 | [diff] [blame] | 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', () => { |
Matteo Scandolo | 2e804cb | 2016-04-27 16:29:33 -0700 | [diff] [blame] | 138 | let empty_modelField = { |
Matteo Scandolo | d02ef50 | 2016-04-27 15:58:16 -0700 | [diff] [blame] | 139 | // 5: {} |
| 140 | }; |
Matteo Scandolo | 2e804cb | 2016-04-27 16:29:33 -0700 | [diff] [blame] | 141 | let empty_customFields = { |
Matteo Scandolo | d02ef50 | 2016-04-27 15:58:16 -0700 | [diff] [blame] | 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 | |
Matteo Scandolo | 2e804cb | 2016-04-27 16:29:33 -0700 | [diff] [blame] | 168 | let empty_formObject = { |
Matteo Scandolo | d02ef50 | 2016-04-27 15:58:16 -0700 | [diff] [blame] | 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 | |
Matteo Scandolo | 2e804cb | 2016-04-27 16:29:33 -0700 | [diff] [blame] | 201 | let empty_model = {5: 'Nan'} |
Matteo Scandolo | d02ef50 | 2016-04-27 15:58:16 -0700 | [diff] [blame] | 202 | |
| 203 | it('should create a form object', () => { |
Matteo Scandolo | 2e804cb | 2016-04-27 16:29:33 -0700 | [diff] [blame] | 204 | 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 Scandolo | d02ef50 | 2016-04-27 15:58:16 -0700 | [diff] [blame] | 212 | }); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 213 | }); |
| 214 | }); |
| 215 | |
Matteo Scandolo | 2e804cb | 2016-04-27 16:29:33 -0700 | [diff] [blame] | 216 | describe('The xos-form component', () => { |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 217 | |
| 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 Scandolo | 28e49b7 | 2016-04-22 14:14:03 -0700 | [diff] [blame] | 251 | formName: 'testForm', |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 252 | actions: [ |
| 253 | { |
| 254 | label: 'Save', |
| 255 | icon: 'ok', // refers to bootstraps glyphicon |
| 256 | cb: cb, |
| 257 | class: 'success' |
| 258 | } |
| 259 | ], |
Matteo Scandolo | 824a7cb | 2016-04-20 12:24:52 -0700 | [diff] [blame] | 260 | fields: { |
| 261 | first_name: { |
| 262 | label: 'Custom Label' |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 263 | } |
Matteo Scandolo | 824a7cb | 2016-04-20 12:24:52 -0700 | [diff] [blame] | 264 | } |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | scope.model = { |
| 268 | id: 1, |
| 269 | first_name: 'Jhon', |
| 270 | last_name: 'Snow', |
| 271 | age: 25, |
Matteo Scandolo | fb667b0 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 272 | email: 'test@onlab.us', |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 273 | 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 Scandolo | fb667b0 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 295 | 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 Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 298 | var field = element[0].getElementsByTagName('input'); |
| 299 | expect(field.length).toEqual(8); |
| 300 | }); |
| 301 | |
Matteo Scandolo | fb667b0 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 302 | it('should render 1 boolean field', () => { |
| 303 | expect($(element).find('.boolean-field > button').length).toEqual(2) |
| 304 | }); |
| 305 | |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 306 | it('when clicking on action should invoke callback', () => { |
Matteo Scandolo | fb667b0 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 307 | var link = $(element).find('[role="button"]'); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 308 | 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 Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 317 | |
Matteo Scandolo | b389f7a | 2016-04-20 14:59:39 -0700 | [diff] [blame] | 318 | 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 Scandolo | fb667b0 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 321 | 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 Scandolo | b389f7a | 2016-04-20 14:59:39 -0700 | [diff] [blame] | 346 | }); |
Matteo Scandolo | 28e49b7 | 2016-04-22 14:14:03 -0700 | [diff] [blame] | 347 | |
Matteo Scandolo | 2e804cb | 2016-04-27 16:29:33 -0700 | [diff] [blame] | 348 | // NOTE not sure why this tests are failing |
| 349 | xdescribe('the custom validation options', () => { |
Matteo Scandolo | 28e49b7 | 2016-04-22 14:14:03 -0700 | [diff] [blame] | 350 | beforeEach(() => { |
| 351 | scope.config.fields.first_name.validators = { |
| 352 | minlength: 10, |
| 353 | maxlength: 15, |
| 354 | required: true |
| 355 | }; |
| 356 | |
Matteo Scandolo | 953ddad | 2016-04-25 08:15:24 -0700 | [diff] [blame] | 357 | scope.config.fields.age = { |
| 358 | validators: { |
| 359 | min: 10, |
| 360 | max: 20 |
| 361 | } |
| 362 | }; |
| 363 | |
Matteo Scandolo | 28e49b7 | 2016-04-22 14:14:03 -0700 | [diff] [blame] | 364 | 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 Scandolo | 953ddad | 2016-04-25 08:15:24 -0700 | [diff] [blame] | 390 | |
Matteo Scandolo | 2e804cb | 2016-04-27 16:29:33 -0700 | [diff] [blame] | 391 | it('should validate min', () => { |
Matteo Scandolo | 953ddad | 2016-04-25 08:15:24 -0700 | [diff] [blame] | 392 | // 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 Scandolo | 28e49b7 | 2016-04-22 14:14:03 -0700 | [diff] [blame] | 399 | }); |
Matteo Scandolo | b389f7a | 2016-04-20 14:59:39 -0700 | [diff] [blame] | 400 | }); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 401 | }); |
| 402 | }); |
| 403 | })(); |