Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 1 | /** |
| 2 | * © OpenCORD |
| 3 | * |
| 4 | * Created by teone on 5/25/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', |
| 18 | 'mail', |
| 19 | 'active', |
Matteo Scandolo | 314e7e1 | 2016-06-06 15:21:21 -0700 | [diff] [blame] | 20 | 'created' |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 21 | ]; |
| 22 | |
| 23 | let modelField = { |
| 24 | id: {}, |
| 25 | name: {}, |
| 26 | mail: {}, |
| 27 | active: {}, |
Matteo Scandolo | 314e7e1 | 2016-06-06 15:21:21 -0700 | [diff] [blame] | 28 | created: {} |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | let model = { |
| 32 | id: 1, |
| 33 | name: 'test', |
| 34 | mail: 'test@onlab.us', |
| 35 | active: true, |
| 36 | created: '2016-04-18T23:44:16.883181Z', |
| 37 | custom: 'MyCustomValue' |
| 38 | }; |
| 39 | |
| 40 | let customField = { |
Matteo Scandolo | 314e7e1 | 2016-06-06 15:21:21 -0700 | [diff] [blame] | 41 | id: { |
| 42 | label: 'Id', |
| 43 | type: 'number', |
| 44 | validators: { |
| 45 | required: true |
| 46 | }, |
| 47 | hint: '' |
| 48 | }, |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 49 | custom: { |
| 50 | label: 'Custom Label', |
| 51 | type: 'number', |
Matteo Scandolo | 5dfcb08 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 52 | validators: {}, |
| 53 | hint: 'Test Hint' |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 54 | } |
| 55 | }; |
| 56 | |
| 57 | let formObject = { |
| 58 | id: { |
| 59 | label: 'Id:', |
| 60 | type: 'number', |
Matteo Scandolo | 314e7e1 | 2016-06-06 15:21:21 -0700 | [diff] [blame] | 61 | validators: { |
| 62 | required: true |
| 63 | }, |
Matteo Scandolo | 5dfcb08 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 64 | hint: '' |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 65 | }, |
| 66 | name: { |
| 67 | label: 'Name:', |
| 68 | type: 'text', |
Matteo Scandolo | 5dfcb08 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 69 | validators: {}, |
| 70 | hint: '' |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 71 | }, |
| 72 | mail: { |
| 73 | label: 'Mail:', |
| 74 | type: 'email', |
Matteo Scandolo | 5dfcb08 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 75 | validators: {}, |
| 76 | hint: '' |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 77 | }, |
| 78 | active: { |
| 79 | label: 'Active:', |
| 80 | type: 'boolean', |
Matteo Scandolo | 5dfcb08 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 81 | validators: {}, |
| 82 | hint: '' |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 83 | }, |
| 84 | created: { |
| 85 | label: 'Created:', |
| 86 | type: 'date', |
Matteo Scandolo | 5dfcb08 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 87 | validators: {}, |
| 88 | hint: '' |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 89 | }, |
| 90 | custom: { |
| 91 | label: 'Custom Label:', |
| 92 | type: 'number', |
Matteo Scandolo | 5dfcb08 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 93 | validators: {}, |
| 94 | hint: 'Test Hint' |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 95 | } |
| 96 | }; |
| 97 | |
| 98 | // load the application module |
| 99 | beforeEach(module('xos.helpers')); |
| 100 | |
| 101 | // inject the cartService |
| 102 | beforeEach(inject(function (_XosFormHelpers_) { |
| 103 | // The injector unwraps the underscores (_) from around the parameter names when matching |
| 104 | service = _XosFormHelpers_; |
| 105 | })); |
| 106 | |
| 107 | describe('the _isEmail method', () => { |
| 108 | it('should return true', () => { |
| 109 | expect(service._isEmail('test@onlab.us')).toEqual(true); |
| 110 | }); |
| 111 | it('should return false', () => { |
| 112 | expect(service._isEmail('testonlab.us')).toEqual(false); |
| 113 | expect(service._isEmail('test@onlab')).toEqual(false); |
| 114 | }); |
| 115 | }); |
| 116 | |
| 117 | describe('the _getFieldFormat method', () => { |
| 118 | it('should return text', () => { |
| 119 | expect(service._getFieldFormat('a random text')).toEqual('text'); |
| 120 | expect(service._getFieldFormat(null)).toEqual('text'); |
| 121 | expect(service._getFieldFormat('1')).toEqual('text'); |
| 122 | }); |
| 123 | it('should return mail', () => { |
| 124 | expect(service._getFieldFormat('test@onlab.us')).toEqual('email'); |
| 125 | }); |
| 126 | it('should return number', () => { |
| 127 | expect(service._getFieldFormat(1)).toEqual('number'); |
| 128 | }); |
| 129 | it('should return boolean', () => { |
| 130 | expect(service._getFieldFormat(false)).toEqual('boolean'); |
| 131 | expect(service._getFieldFormat(true)).toEqual('boolean'); |
| 132 | }); |
| 133 | |
| 134 | it('should return date', () => { |
| 135 | expect(service._getFieldFormat('2016-04-19T23:09:1092Z')).toEqual('text'); |
| 136 | expect(service._getFieldFormat(new Date())).toEqual('date'); |
| 137 | expect(service._getFieldFormat('2016-04-19T23:09:10.208092Z')).toEqual('date'); |
| 138 | }); |
| 139 | |
| 140 | it('should return array', () => { |
| 141 | expect(service._getFieldFormat([])).toEqual('array'); |
| 142 | expect(service._getFieldFormat(['a', 'b'])).toEqual('array'); |
| 143 | }); |
| 144 | |
| 145 | it('should return object', () => { |
| 146 | expect(service._getFieldFormat({})).toEqual('object'); |
| 147 | expect(service._getFieldFormat({foo: 'bar'})).toEqual('object'); |
| 148 | }); |
| 149 | }); |
| 150 | |
Matteo Scandolo | b9fb625 | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 151 | describe('the parseModelField mehtod', () => { |
| 152 | it('should convert the fields array in an empty form object', () => { |
| 153 | expect(service.parseModelField(fields)).toEqual(modelField); |
| 154 | }); |
| 155 | |
| 156 | xit('should handle nested config', () => { |
| 157 | |
| 158 | }); |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 159 | }); |
| 160 | |
| 161 | describe('when modelField are provided', () => { |
| 162 | it('should combine modelField and customField in a form object', () => { |
Matteo Scandolo | 314e7e1 | 2016-06-06 15:21:21 -0700 | [diff] [blame] | 163 | const form = service.buildFormStructure(modelField, customField, model); |
| 164 | expect(form).toEqual(formObject); |
| 165 | }); |
| 166 | |
| 167 | it('should override modelField properties whith customField properties', () => { |
| 168 | const customFieldOverride = { |
| 169 | id: { |
| 170 | hint: 'something', |
| 171 | type: 'select', |
| 172 | options: [ |
| 173 | {id: 1, label: 'one'}, |
| 174 | {id: 2, label: 'two'} |
| 175 | ], |
| 176 | validators: { |
| 177 | required: true |
| 178 | } |
| 179 | } |
| 180 | }; |
| 181 | const form = service.buildFormStructure({id: {}}, customFieldOverride, model); |
| 182 | |
| 183 | expect(form).toEqual({ |
| 184 | id: { |
| 185 | label: 'Id:', |
| 186 | validators: {required: true}, |
| 187 | hint: customFieldOverride.id.hint, |
| 188 | type: customFieldOverride.id.type, |
| 189 | options: customFieldOverride.id.options |
| 190 | } |
| 191 | }); |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 192 | }); |
| 193 | }); |
| 194 | |
| 195 | describe('when model field is an empty array', () => { |
| 196 | let empty_modelField = { |
| 197 | // 5: {} |
| 198 | }; |
| 199 | let empty_customFields = { |
| 200 | id: { |
| 201 | label: 'Id', |
| 202 | type: 'number' |
| 203 | }, |
| 204 | name: { |
| 205 | label: 'Name', |
| 206 | type: 'text' |
| 207 | }, |
| 208 | mail: { |
| 209 | label: 'Mail', |
| 210 | type: 'email' |
| 211 | }, |
| 212 | active: { |
| 213 | label: 'Active', |
| 214 | type: 'boolean' |
| 215 | }, |
| 216 | created: { |
| 217 | label: 'Created', |
| 218 | type: 'date' |
| 219 | }, |
| 220 | custom: { |
| 221 | label: 'Custom Label', |
Matteo Scandolo | 5dfcb08 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 222 | type: 'number', |
| 223 | hint: 'Test Hint' |
arpiagariu | 47cd689 | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 224 | }, |
Matteo Scandolo | b6ca301 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 225 | select: { |
arpiagariu | 47cd689 | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 226 | label: 'Select Label', |
| 227 | type: 'select', |
| 228 | hint: 'Select Hint', |
Matteo Scandolo | b6ca301 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 229 | options: [ |
| 230 | {id: 1, label: 'something'} |
| 231 | ] |
Matteo Scandolo | dee1436 | 2016-06-08 15:11:39 -0700 | [diff] [blame] | 232 | }, |
| 233 | object: { |
| 234 | label: 'Object Label', |
| 235 | type: 'object', |
| 236 | hint: 'Object Hint', |
| 237 | properties: { |
| 238 | foo: { |
| 239 | type: 'string', |
| 240 | label: 'FooLabel', |
| 241 | validators: { |
| 242 | required: true |
| 243 | } |
| 244 | }, |
| 245 | bar: { |
| 246 | type: 'number' |
| 247 | } |
| 248 | } |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 249 | } |
| 250 | }; |
| 251 | |
| 252 | let empty_formObject = { |
| 253 | id: { |
| 254 | label: 'Id:', |
| 255 | type: 'number', |
Matteo Scandolo | 5dfcb08 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 256 | validators: {}, |
| 257 | hint: '' |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 258 | }, |
| 259 | name: { |
| 260 | label: 'Name:', |
| 261 | type: 'text', |
Matteo Scandolo | 5dfcb08 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 262 | validators: {}, |
| 263 | hint: '' |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 264 | }, |
| 265 | mail: { |
| 266 | label: 'Mail:', |
| 267 | type: 'email', |
Matteo Scandolo | 5dfcb08 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 268 | validators: {}, |
| 269 | hint: '' |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 270 | }, |
| 271 | active: { |
| 272 | label: 'Active:', |
| 273 | type: 'boolean', |
Matteo Scandolo | 5dfcb08 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 274 | validators: {}, |
| 275 | hint: '' |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 276 | }, |
| 277 | created: { |
| 278 | label: 'Created:', |
| 279 | type: 'date', |
Matteo Scandolo | 5dfcb08 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 280 | validators: {}, |
| 281 | hint: '' |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 282 | }, |
| 283 | custom: { |
| 284 | label: 'Custom Label:', |
| 285 | type: 'number', |
Matteo Scandolo | 5dfcb08 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 286 | validators: {}, |
| 287 | hint: 'Test Hint' |
arpiagariu | 47cd689 | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 288 | }, |
Matteo Scandolo | b6ca301 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 289 | select: { |
arpiagariu | 47cd689 | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 290 | label: 'Select Label:', |
| 291 | type: 'select', |
| 292 | hint: 'Select Hint', |
| 293 | validators: {}, |
Matteo Scandolo | b6ca301 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 294 | options: [ |
| 295 | {id: 1, label: 'something'} |
| 296 | ] |
Matteo Scandolo | dee1436 | 2016-06-08 15:11:39 -0700 | [diff] [blame] | 297 | }, |
| 298 | object: { |
| 299 | label: 'Object Label:', |
| 300 | type: 'object', |
| 301 | hint: 'Object Hint', |
| 302 | validators: {}, |
| 303 | properties: { |
| 304 | foo: { |
| 305 | type: 'string', |
| 306 | label: 'FooLabel', |
| 307 | validators: { |
| 308 | required: true |
| 309 | } |
| 310 | }, |
| 311 | bar: { |
| 312 | type: 'number' |
| 313 | } |
| 314 | } |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 315 | } |
| 316 | }; |
| 317 | |
| 318 | let empty_model = {5: 'Nan'} |
| 319 | |
| 320 | it('should create a form object', () => { |
arpiagariu | 47cd689 | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 321 | let res = service.buildFormStructure(empty_modelField, empty_customFields, empty_model); |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 322 | expect(res.id).toEqual(empty_formObject.id); |
| 323 | expect(res.name).toEqual(empty_formObject.name); |
| 324 | expect(res.mail).toEqual(empty_formObject.mail); |
| 325 | expect(res.active).toEqual(empty_formObject.active); |
| 326 | expect(res.created).toEqual(empty_formObject.created); |
| 327 | expect(res.custom).toEqual(empty_formObject.custom); |
arpiagariu | 47cd689 | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 328 | expect(res.select).toEqual(empty_formObject.select); |
Matteo Scandolo | dee1436 | 2016-06-08 15:11:39 -0700 | [diff] [blame] | 329 | expect(res.object).toEqual(empty_formObject.object); |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 330 | expect(res).toEqual(empty_formObject); |
| 331 | }); |
| 332 | }); |
| 333 | }); |
| 334 | }); |
| 335 | })(); |