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', |
| 20 | 'created', |
| 21 | 'custom' |
| 22 | ]; |
| 23 | |
| 24 | let modelField = { |
| 25 | id: {}, |
| 26 | name: {}, |
| 27 | mail: {}, |
| 28 | active: {}, |
| 29 | created: {}, |
| 30 | custom: {} |
| 31 | }; |
| 32 | |
| 33 | let model = { |
| 34 | id: 1, |
| 35 | name: 'test', |
| 36 | mail: 'test@onlab.us', |
| 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', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 46 | validators: {}, |
| 47 | hint: 'Test Hint' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 48 | } |
| 49 | }; |
| 50 | |
| 51 | let formObject = { |
| 52 | id: { |
| 53 | label: 'Id:', |
| 54 | type: 'number', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 55 | validators: {}, |
| 56 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 57 | }, |
| 58 | name: { |
| 59 | label: 'Name:', |
| 60 | type: 'text', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 61 | validators: {}, |
| 62 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 63 | }, |
| 64 | mail: { |
| 65 | label: 'Mail:', |
| 66 | type: 'email', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 67 | validators: {}, |
| 68 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 69 | }, |
| 70 | active: { |
| 71 | label: 'Active:', |
| 72 | type: 'boolean', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 73 | validators: {}, |
| 74 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 75 | }, |
| 76 | created: { |
| 77 | label: 'Created:', |
| 78 | type: 'date', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 79 | validators: {}, |
| 80 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 81 | }, |
| 82 | custom: { |
| 83 | label: 'Custom Label:', |
| 84 | type: 'number', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 85 | validators: {}, |
| 86 | hint: 'Test Hint' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 87 | } |
| 88 | }; |
| 89 | |
| 90 | // load the application module |
| 91 | beforeEach(module('xos.helpers')); |
| 92 | |
| 93 | // inject the cartService |
| 94 | beforeEach(inject(function (_XosFormHelpers_) { |
| 95 | // The injector unwraps the underscores (_) from around the parameter names when matching |
| 96 | service = _XosFormHelpers_; |
| 97 | })); |
| 98 | |
| 99 | describe('the _isEmail method', () => { |
| 100 | it('should return true', () => { |
| 101 | expect(service._isEmail('test@onlab.us')).toEqual(true); |
| 102 | }); |
| 103 | it('should return false', () => { |
| 104 | expect(service._isEmail('testonlab.us')).toEqual(false); |
| 105 | expect(service._isEmail('test@onlab')).toEqual(false); |
| 106 | }); |
| 107 | }); |
| 108 | |
| 109 | describe('the _getFieldFormat method', () => { |
| 110 | it('should return text', () => { |
| 111 | expect(service._getFieldFormat('a random text')).toEqual('text'); |
| 112 | expect(service._getFieldFormat(null)).toEqual('text'); |
| 113 | expect(service._getFieldFormat('1')).toEqual('text'); |
| 114 | }); |
| 115 | it('should return mail', () => { |
| 116 | expect(service._getFieldFormat('test@onlab.us')).toEqual('email'); |
| 117 | }); |
| 118 | it('should return number', () => { |
| 119 | expect(service._getFieldFormat(1)).toEqual('number'); |
| 120 | }); |
| 121 | it('should return boolean', () => { |
| 122 | expect(service._getFieldFormat(false)).toEqual('boolean'); |
| 123 | expect(service._getFieldFormat(true)).toEqual('boolean'); |
| 124 | }); |
| 125 | |
| 126 | it('should return date', () => { |
| 127 | expect(service._getFieldFormat('2016-04-19T23:09:1092Z')).toEqual('text'); |
| 128 | expect(service._getFieldFormat(new Date())).toEqual('date'); |
| 129 | expect(service._getFieldFormat('2016-04-19T23:09:10.208092Z')).toEqual('date'); |
| 130 | }); |
| 131 | |
| 132 | it('should return array', () => { |
| 133 | expect(service._getFieldFormat([])).toEqual('array'); |
| 134 | expect(service._getFieldFormat(['a', 'b'])).toEqual('array'); |
| 135 | }); |
| 136 | |
| 137 | it('should return object', () => { |
| 138 | expect(service._getFieldFormat({})).toEqual('object'); |
| 139 | expect(service._getFieldFormat({foo: 'bar'})).toEqual('object'); |
| 140 | }); |
| 141 | }); |
| 142 | |
Matteo Scandolo | 2aca9ab | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 143 | describe('the parseModelField mehtod', () => { |
| 144 | it('should convert the fields array in an empty form object', () => { |
| 145 | expect(service.parseModelField(fields)).toEqual(modelField); |
| 146 | }); |
| 147 | |
| 148 | xit('should handle nested config', () => { |
| 149 | |
| 150 | }); |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 151 | }); |
| 152 | |
| 153 | describe('when modelField are provided', () => { |
| 154 | it('should combine modelField and customField in a form object', () => { |
| 155 | expect(service.buildFormStructure(modelField, customField, model)).toEqual(formObject); |
| 156 | }); |
| 157 | }); |
| 158 | |
| 159 | describe('when model field is an empty array', () => { |
| 160 | let empty_modelField = { |
| 161 | // 5: {} |
| 162 | }; |
| 163 | let empty_customFields = { |
| 164 | id: { |
| 165 | label: 'Id', |
| 166 | type: 'number' |
| 167 | }, |
| 168 | name: { |
| 169 | label: 'Name', |
| 170 | type: 'text' |
| 171 | }, |
| 172 | mail: { |
| 173 | label: 'Mail', |
| 174 | type: 'email' |
| 175 | }, |
| 176 | active: { |
| 177 | label: 'Active', |
| 178 | type: 'boolean' |
| 179 | }, |
| 180 | created: { |
| 181 | label: 'Created', |
| 182 | type: 'date' |
| 183 | }, |
| 184 | custom: { |
| 185 | label: 'Custom Label', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 186 | type: 'number', |
| 187 | hint: 'Test Hint' |
arpiagariu | 7c1975d | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 188 | }, |
| 189 | select:{ |
| 190 | label: 'Select Label', |
| 191 | type: 'select', |
| 192 | hint: 'Select Hint', |
| 193 | options:{} |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 194 | } |
| 195 | }; |
| 196 | |
| 197 | let empty_formObject = { |
| 198 | id: { |
| 199 | label: 'Id:', |
| 200 | type: 'number', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 201 | validators: {}, |
| 202 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 203 | }, |
| 204 | name: { |
| 205 | label: 'Name:', |
| 206 | type: 'text', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 207 | validators: {}, |
| 208 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 209 | }, |
| 210 | mail: { |
| 211 | label: 'Mail:', |
| 212 | type: 'email', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 213 | validators: {}, |
| 214 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 215 | }, |
| 216 | active: { |
| 217 | label: 'Active:', |
| 218 | type: 'boolean', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 219 | validators: {}, |
| 220 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 221 | }, |
| 222 | created: { |
| 223 | label: 'Created:', |
| 224 | type: 'date', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 225 | validators: {}, |
| 226 | hint: '' |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 227 | }, |
| 228 | custom: { |
| 229 | label: 'Custom Label:', |
| 230 | type: 'number', |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 231 | validators: {}, |
| 232 | hint: 'Test Hint' |
arpiagariu | 7c1975d | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 233 | }, |
| 234 | select:{ |
| 235 | label: 'Select Label:', |
| 236 | type: 'select', |
| 237 | hint: 'Select Hint', |
| 238 | validators: {}, |
| 239 | options:{} |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 240 | } |
| 241 | }; |
| 242 | |
| 243 | let empty_model = {5: 'Nan'} |
| 244 | |
| 245 | it('should create a form object', () => { |
arpiagariu | 7c1975d | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 246 | let res = service.buildFormStructure(empty_modelField, empty_customFields, empty_model); |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 247 | expect(res.id).toEqual(empty_formObject.id); |
| 248 | expect(res.name).toEqual(empty_formObject.name); |
| 249 | expect(res.mail).toEqual(empty_formObject.mail); |
| 250 | expect(res.active).toEqual(empty_formObject.active); |
| 251 | expect(res.created).toEqual(empty_formObject.created); |
| 252 | expect(res.custom).toEqual(empty_formObject.custom); |
arpiagariu | 7c1975d | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 253 | expect(res.select).toEqual(empty_formObject.select); |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 254 | expect(res).toEqual(empty_formObject); |
| 255 | }); |
| 256 | }); |
| 257 | }); |
| 258 | }); |
| 259 | })(); |