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', |
| 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: 'text', |
| 59 | validators: {} |
| 60 | }, |
| 61 | mail: { |
| 62 | label: 'Mail:', |
| 63 | type: 'email', |
| 64 | validators: {} |
| 65 | }, |
| 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 | |
| 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 | |
| 102 | describe('the _getFieldFormat method', () => { |
| 103 | it('should return text', () => { |
| 104 | expect(service._getFieldFormat('a random text')).toEqual('text'); |
| 105 | expect(service._getFieldFormat(null)).toEqual('text'); |
| 106 | expect(service._getFieldFormat('1')).toEqual('text'); |
| 107 | }); |
| 108 | it('should return mail', () => { |
| 109 | expect(service._getFieldFormat('test@onlab.us')).toEqual('email'); |
| 110 | }); |
| 111 | it('should return number', () => { |
| 112 | expect(service._getFieldFormat(1)).toEqual('number'); |
| 113 | }); |
| 114 | it('should return boolean', () => { |
| 115 | expect(service._getFieldFormat(false)).toEqual('boolean'); |
| 116 | expect(service._getFieldFormat(true)).toEqual('boolean'); |
| 117 | }); |
| 118 | |
| 119 | it('should return date', () => { |
| 120 | expect(service._getFieldFormat('2016-04-19T23:09:1092Z')).toEqual('text'); |
| 121 | expect(service._getFieldFormat(new Date())).toEqual('date'); |
| 122 | expect(service._getFieldFormat('2016-04-19T23:09:10.208092Z')).toEqual('date'); |
| 123 | }); |
| 124 | |
| 125 | it('should return array', () => { |
| 126 | expect(service._getFieldFormat([])).toEqual('array'); |
| 127 | expect(service._getFieldFormat(['a', 'b'])).toEqual('array'); |
| 128 | }); |
| 129 | |
| 130 | it('should return object', () => { |
| 131 | expect(service._getFieldFormat({})).toEqual('object'); |
| 132 | expect(service._getFieldFormat({foo: 'bar'})).toEqual('object'); |
| 133 | }); |
| 134 | }); |
| 135 | |
| 136 | it('should convert the fields array in an empty form object', () => { |
| 137 | expect(service.parseModelField(fields)).toEqual(modelField); |
| 138 | }); |
| 139 | |
| 140 | describe('when modelField are provided', () => { |
| 141 | it('should combine modelField and customField in a form object', () => { |
| 142 | expect(service.buildFormStructure(modelField, customField, model)).toEqual(formObject); |
| 143 | }); |
| 144 | }); |
| 145 | |
| 146 | describe('when model field is an empty array', () => { |
| 147 | let empty_modelField = { |
| 148 | // 5: {} |
| 149 | }; |
| 150 | let empty_customFields = { |
| 151 | id: { |
| 152 | label: 'Id', |
| 153 | type: 'number' |
| 154 | }, |
| 155 | name: { |
| 156 | label: 'Name', |
| 157 | type: 'text' |
| 158 | }, |
| 159 | mail: { |
| 160 | label: 'Mail', |
| 161 | type: 'email' |
| 162 | }, |
| 163 | active: { |
| 164 | label: 'Active', |
| 165 | type: 'boolean' |
| 166 | }, |
| 167 | created: { |
| 168 | label: 'Created', |
| 169 | type: 'date' |
| 170 | }, |
| 171 | custom: { |
| 172 | label: 'Custom Label', |
| 173 | type: 'number' |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | let empty_formObject = { |
| 178 | id: { |
| 179 | label: 'Id:', |
| 180 | type: 'number', |
| 181 | validators: {} |
| 182 | }, |
| 183 | name: { |
| 184 | label: 'Name:', |
| 185 | type: 'text', |
| 186 | validators: {} |
| 187 | }, |
| 188 | mail: { |
| 189 | label: 'Mail:', |
| 190 | type: 'email', |
| 191 | validators: {} |
| 192 | }, |
| 193 | active: { |
| 194 | label: 'Active:', |
| 195 | type: 'boolean', |
| 196 | validators: {} |
| 197 | }, |
| 198 | created: { |
| 199 | label: 'Created:', |
| 200 | type: 'date', |
| 201 | validators: {} |
| 202 | }, |
| 203 | custom: { |
| 204 | label: 'Custom Label:', |
| 205 | type: 'number', |
| 206 | validators: {} |
| 207 | } |
| 208 | }; |
| 209 | |
| 210 | let empty_model = {5: 'Nan'} |
| 211 | |
| 212 | it('should create a form object', () => { |
| 213 | let res = service.buildFormStructure(empty_modelField, empty_customFields, empty_model) |
| 214 | expect(res.id).toEqual(empty_formObject.id); |
| 215 | expect(res.name).toEqual(empty_formObject.name); |
| 216 | expect(res.mail).toEqual(empty_formObject.mail); |
| 217 | expect(res.active).toEqual(empty_formObject.active); |
| 218 | expect(res.created).toEqual(empty_formObject.created); |
| 219 | expect(res.custom).toEqual(empty_formObject.custom); |
| 220 | expect(res).toEqual(empty_formObject); |
| 221 | }); |
| 222 | }); |
| 223 | }); |
| 224 | }); |
| 225 | })(); |