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'); |
| 105 | }); |
Matteo Scandolo | b389f7a | 2016-04-20 14:59:39 -0700 | [diff] [blame] | 106 | it('should return mail', () => { |
| 107 | expect(service._getFieldFormat('test@onlab.us')).toEqual('email'); |
| 108 | }); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 109 | it('should return number', () => { |
| 110 | expect(service._getFieldFormat(1)).toEqual('number'); |
| 111 | expect(service._getFieldFormat('1')).toEqual('number'); |
| 112 | }); |
Matteo Scandolo | 824a7cb | 2016-04-20 12:24:52 -0700 | [diff] [blame] | 113 | it('should return boolean', () => { |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 114 | expect(service._getFieldFormat(false)).toEqual('boolean'); |
| 115 | expect(service._getFieldFormat(true)).toEqual('boolean'); |
| 116 | }); |
| 117 | |
| 118 | it('should return date', () => { |
| 119 | expect(service._getFieldFormat('2016-04-19T23:09:1092Z')).toEqual('string'); |
| 120 | expect(service._getFieldFormat(new Date())).toEqual('date'); |
| 121 | expect(service._getFieldFormat('2016-04-19T23:09:10.208092Z')).toEqual('date'); |
| 122 | }); |
| 123 | }); |
| 124 | |
Matteo Scandolo | 824a7cb | 2016-04-20 12:24:52 -0700 | [diff] [blame] | 125 | it('should convert the fields array in an empty form object', () => { |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 126 | expect(service.parseModelField(fields)).toEqual(modelField); |
| 127 | }); |
| 128 | |
Matteo Scandolo | 824a7cb | 2016-04-20 12:24:52 -0700 | [diff] [blame] | 129 | it('should combine modelField and customField in a form object', () => { |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 130 | expect(service.buildFormStructure(modelField, customField, model)).toEqual(formObject); |
| 131 | }); |
| 132 | }); |
| 133 | |
Matteo Scandolo | 824a7cb | 2016-04-20 12:24:52 -0700 | [diff] [blame] | 134 | describe('The xos-form component', () => { |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 135 | |
| 136 | let element, scope, isolatedScope; |
| 137 | |
| 138 | beforeEach(module('xos.helpers')); |
| 139 | |
| 140 | it('should throw an error if no config is specified', inject(($compile, $rootScope) => { |
| 141 | function errorFunctionWrapper(){ |
| 142 | $compile(angular.element('<xos-form></xos-form>'))($rootScope); |
| 143 | $rootScope.$digest(); |
| 144 | } |
| 145 | expect(errorFunctionWrapper).toThrow(new Error('[xosForm] Please provide a configuration via the "config" attribute')); |
| 146 | })); |
| 147 | |
| 148 | it('should throw an error if no actions is specified', inject(($compile, $rootScope) => { |
| 149 | function errorFunctionWrapper(){ |
| 150 | let scope = $rootScope.$new(); |
| 151 | scope.config = 'green'; |
| 152 | $compile(angular.element('<xos-form config="config"></xos-form>'))(scope); |
| 153 | $rootScope.$digest(); |
| 154 | } |
| 155 | expect(errorFunctionWrapper).toThrow(new Error('[xosForm] Please provide an action list in the configuration')); |
| 156 | })); |
| 157 | |
| 158 | describe('when correctly configured', () => { |
| 159 | |
| 160 | let cb = jasmine.createSpy('callback'); |
| 161 | |
| 162 | beforeEach(inject(($compile, $rootScope) => { |
| 163 | |
| 164 | |
| 165 | scope = $rootScope.$new(); |
| 166 | |
| 167 | scope.config = { |
| 168 | exclude: ['excludedField'], |
| 169 | actions: [ |
| 170 | { |
| 171 | label: 'Save', |
| 172 | icon: 'ok', // refers to bootstraps glyphicon |
| 173 | cb: cb, |
| 174 | class: 'success' |
| 175 | } |
| 176 | ], |
Matteo Scandolo | 824a7cb | 2016-04-20 12:24:52 -0700 | [diff] [blame] | 177 | fields: { |
| 178 | first_name: { |
| 179 | label: 'Custom Label' |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 180 | } |
Matteo Scandolo | 824a7cb | 2016-04-20 12:24:52 -0700 | [diff] [blame] | 181 | } |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | scope.model = { |
| 185 | id: 1, |
| 186 | first_name: 'Jhon', |
| 187 | last_name: 'Snow', |
| 188 | age: 25, |
Matteo Scandolo | fb667b0 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 189 | email: 'test@onlab.us', |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 190 | birthDate: '2016-04-18T23:44:16.883181Z', |
| 191 | enabled: true, |
| 192 | role: 'user', //select |
| 193 | a_permissions: [ |
| 194 | |
| 195 | ], |
| 196 | o_permissions: { |
| 197 | |
| 198 | } |
| 199 | }; |
| 200 | |
| 201 | element = angular.element(`<xos-form config="config" ng-model="model"></xos-form>`); |
| 202 | $compile(element)(scope); |
| 203 | scope.$digest(); |
| 204 | isolatedScope = element.isolateScope().vm; |
| 205 | })); |
| 206 | |
| 207 | it('should add excluded properties to the list', () => { |
| 208 | let expected = ['id', 'validators', 'created', 'updated', 'deleted', 'backend_status', 'excludedField']; |
| 209 | expect(isolatedScope.excludedField).toEqual(expected); |
| 210 | }); |
| 211 | |
Matteo Scandolo | fb667b0 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 212 | it('should render 8 input field', () => { |
| 213 | // boolean are in the form model, but are not input |
| 214 | expect(Object.keys(isolatedScope.formField).length).toEqual(9); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 215 | var field = element[0].getElementsByTagName('input'); |
| 216 | expect(field.length).toEqual(8); |
| 217 | }); |
| 218 | |
Matteo Scandolo | fb667b0 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 219 | it('should render 1 boolean field', () => { |
| 220 | expect($(element).find('.boolean-field > button').length).toEqual(2) |
| 221 | }); |
| 222 | |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 223 | it('when clicking on action should invoke callback', () => { |
Matteo Scandolo | fb667b0 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 224 | var link = $(element).find('[role="button"]'); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 225 | link.click(); |
| 226 | expect(cb).toHaveBeenCalledWith(scope.model); |
| 227 | }); |
| 228 | |
| 229 | it('should set a custom label', () => { |
| 230 | let nameField = element[0].getElementsByClassName('form-group')[0]; |
| 231 | let label = angular.element(nameField.getElementsByTagName('label')[0]).text() |
| 232 | expect(label).toEqual('Custom Label:'); |
| 233 | }); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 234 | |
Matteo Scandolo | b389f7a | 2016-04-20 14:59:39 -0700 | [diff] [blame] | 235 | it('should use the correct input type', () => { |
| 236 | expect($(element).find('[name="age"]')).toHaveAttr('type', 'number'); |
| 237 | expect($(element).find('[name="birthDate"]')).toHaveAttr('type', 'date'); |
Matteo Scandolo | fb667b0 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 238 | expect($(element).find('[name="email"]')).toHaveAttr('type', 'email'); |
| 239 | }); |
| 240 | |
| 241 | describe('the boolean field', () => { |
| 242 | |
| 243 | let setFalse, setTrue; |
| 244 | |
| 245 | beforeEach(() => { |
| 246 | setFalse= $(element).find('.boolean-field > button:first-child'); |
| 247 | setTrue = $(element).find('.boolean-field > button:last-child'); |
| 248 | }); |
| 249 | |
| 250 | it('should change value to false', () => { |
| 251 | expect(isolatedScope.ngModel.enabled).toEqual(true); |
| 252 | setFalse.click() |
| 253 | expect(isolatedScope.ngModel.enabled).toEqual(false); |
| 254 | }); |
| 255 | |
| 256 | it('should change value to false', () => { |
| 257 | isolatedScope.ngModel.enabled = false; |
| 258 | scope.$apply(); |
| 259 | expect(isolatedScope.ngModel.enabled).toEqual(false); |
| 260 | setTrue.click() |
| 261 | expect(isolatedScope.ngModel.enabled).toEqual(true); |
| 262 | }); |
Matteo Scandolo | b389f7a | 2016-04-20 14:59:39 -0700 | [diff] [blame] | 263 | }); |
| 264 | }); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 265 | }); |
| 266 | }); |
| 267 | })(); |