Matteo Scandolo | df23cdb | 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 | c00865d | 2016-06-06 15:21:21 -0700 | [diff] [blame] | 20 | 'created' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 21 | ]; |
| 22 | |
| 23 | let modelField = { |
| 24 | id: {}, |
| 25 | name: {}, |
| 26 | mail: {}, |
| 27 | active: {}, |
Matteo Scandolo | c00865d | 2016-06-06 15:21:21 -0700 | [diff] [blame] | 28 | created: {} |
Matteo Scandolo | df23cdb | 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 | c00865d | 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 | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 49 | custom: { |
| 50 | label: 'Custom Label', |
| 51 | type: 'number', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 52 | validators: {}, |
| 53 | hint: 'Test Hint' |
Matteo Scandolo | df23cdb | 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 | c00865d | 2016-06-06 15:21:21 -0700 | [diff] [blame] | 61 | validators: { |
| 62 | required: true |
| 63 | }, |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 64 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 65 | }, |
| 66 | name: { |
| 67 | label: 'Name:', |
| 68 | type: 'text', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 69 | validators: {}, |
| 70 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 71 | }, |
| 72 | mail: { |
| 73 | label: 'Mail:', |
| 74 | type: 'email', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 75 | validators: {}, |
| 76 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 77 | }, |
| 78 | active: { |
| 79 | label: 'Active:', |
| 80 | type: 'boolean', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 81 | validators: {}, |
| 82 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 83 | }, |
| 84 | created: { |
| 85 | label: 'Created:', |
| 86 | type: 'date', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 87 | validators: {}, |
| 88 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 89 | }, |
| 90 | custom: { |
| 91 | label: 'Custom Label:', |
| 92 | type: 'number', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 93 | validators: {}, |
| 94 | hint: 'Test Hint' |
Matteo Scandolo | df23cdb | 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 | 2aca9ab | 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 | df23cdb | 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 | c00865d | 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 | df23cdb | 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 | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 222 | type: 'number', |
| 223 | hint: 'Test Hint' |
arpiagariu | 7c1975d | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 224 | }, |
Matteo Scandolo | be13607 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 225 | select: { |
arpiagariu | 7c1975d | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 226 | label: 'Select Label', |
| 227 | type: 'select', |
| 228 | hint: 'Select Hint', |
Matteo Scandolo | be13607 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 229 | options: [ |
| 230 | {id: 1, label: 'something'} |
| 231 | ] |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 232 | } |
| 233 | }; |
| 234 | |
| 235 | let empty_formObject = { |
| 236 | id: { |
| 237 | label: 'Id:', |
| 238 | type: 'number', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 239 | validators: {}, |
| 240 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 241 | }, |
| 242 | name: { |
| 243 | label: 'Name:', |
| 244 | type: 'text', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 245 | validators: {}, |
| 246 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 247 | }, |
| 248 | mail: { |
| 249 | label: 'Mail:', |
| 250 | type: 'email', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 251 | validators: {}, |
| 252 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 253 | }, |
| 254 | active: { |
| 255 | label: 'Active:', |
| 256 | type: 'boolean', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 257 | validators: {}, |
| 258 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 259 | }, |
| 260 | created: { |
| 261 | label: 'Created:', |
| 262 | type: 'date', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 263 | validators: {}, |
| 264 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 265 | }, |
| 266 | custom: { |
| 267 | label: 'Custom Label:', |
| 268 | type: 'number', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 269 | validators: {}, |
| 270 | hint: 'Test Hint' |
arpiagariu | 7c1975d | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 271 | }, |
Matteo Scandolo | be13607 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 272 | select: { |
arpiagariu | 7c1975d | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 273 | label: 'Select Label:', |
| 274 | type: 'select', |
| 275 | hint: 'Select Hint', |
| 276 | validators: {}, |
Matteo Scandolo | be13607 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 277 | options: [ |
| 278 | {id: 1, label: 'something'} |
| 279 | ] |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 280 | } |
| 281 | }; |
| 282 | |
| 283 | let empty_model = {5: 'Nan'} |
| 284 | |
| 285 | it('should create a form object', () => { |
arpiagariu | 7c1975d | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 286 | let res = service.buildFormStructure(empty_modelField, empty_customFields, empty_model); |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 287 | expect(res.id).toEqual(empty_formObject.id); |
| 288 | expect(res.name).toEqual(empty_formObject.name); |
| 289 | expect(res.mail).toEqual(empty_formObject.mail); |
| 290 | expect(res.active).toEqual(empty_formObject.active); |
| 291 | expect(res.created).toEqual(empty_formObject.created); |
| 292 | expect(res.custom).toEqual(empty_formObject.custom); |
arpiagariu | 7c1975d | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 293 | expect(res.select).toEqual(empty_formObject.select); |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 294 | expect(res).toEqual(empty_formObject); |
| 295 | }); |
| 296 | }); |
| 297 | }); |
| 298 | }); |
| 299 | })(); |