blob: a126db5cfb9a776a7f60e1829085c3c82d13b660 [file] [log] [blame]
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -07001/**
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 Scandolo314e7e12016-06-06 15:21:21 -070020 'created'
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070021 ];
22
23 let modelField = {
24 id: {},
25 name: {},
26 mail: {},
27 active: {},
Matteo Scandolo314e7e12016-06-06 15:21:21 -070028 created: {}
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070029 };
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 Scandolo314e7e12016-06-06 15:21:21 -070041 id: {
42 label: 'Id',
43 type: 'number',
44 validators: {
45 required: true
46 },
47 hint: ''
48 },
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070049 custom: {
50 label: 'Custom Label',
51 type: 'number',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070052 validators: {},
53 hint: 'Test Hint'
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070054 }
55 };
56
57 let formObject = {
58 id: {
59 label: 'Id:',
60 type: 'number',
Matteo Scandolo314e7e12016-06-06 15:21:21 -070061 validators: {
62 required: true
63 },
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070064 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070065 },
66 name: {
67 label: 'Name:',
68 type: 'text',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070069 validators: {},
70 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070071 },
72 mail: {
73 label: 'Mail:',
74 type: 'email',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070075 validators: {},
76 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070077 },
78 active: {
79 label: 'Active:',
80 type: 'boolean',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070081 validators: {},
82 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070083 },
84 created: {
85 label: 'Created:',
86 type: 'date',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070087 validators: {},
88 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070089 },
90 custom: {
91 label: 'Custom Label:',
92 type: 'number',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070093 validators: {},
94 hint: 'Test Hint'
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070095 }
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 Scandolob9fb6252016-05-26 15:09:55 -0700151 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 Scandolo03d8b8e2016-05-25 17:37:37 -0700159 });
160
161 describe('when modelField are provided', () => {
162 it('should combine modelField and customField in a form object', () => {
Matteo Scandolo314e7e12016-06-06 15:21:21 -0700163 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 Scandolo03d8b8e2016-05-25 17:37:37 -0700192 });
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 Scandolo5dfcb082016-05-31 15:15:00 -0700222 type: 'number',
223 hint: 'Test Hint'
arpiagariu47cd6892016-06-03 16:41:39 -0700224 },
Matteo Scandolob6ca3012016-06-03 16:58:43 -0700225 select: {
arpiagariu47cd6892016-06-03 16:41:39 -0700226 label: 'Select Label',
227 type: 'select',
228 hint: 'Select Hint',
Matteo Scandolob6ca3012016-06-03 16:58:43 -0700229 options: [
230 {id: 1, label: 'something'}
231 ]
Matteo Scandolodee14362016-06-08 15:11:39 -0700232 },
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 Scandolo03d8b8e2016-05-25 17:37:37 -0700249 }
250 };
251
252 let empty_formObject = {
253 id: {
254 label: 'Id:',
255 type: 'number',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -0700256 validators: {},
257 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700258 },
259 name: {
260 label: 'Name:',
261 type: 'text',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -0700262 validators: {},
263 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700264 },
265 mail: {
266 label: 'Mail:',
267 type: 'email',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -0700268 validators: {},
269 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700270 },
271 active: {
272 label: 'Active:',
273 type: 'boolean',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -0700274 validators: {},
275 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700276 },
277 created: {
278 label: 'Created:',
279 type: 'date',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -0700280 validators: {},
281 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700282 },
283 custom: {
284 label: 'Custom Label:',
285 type: 'number',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -0700286 validators: {},
287 hint: 'Test Hint'
arpiagariu47cd6892016-06-03 16:41:39 -0700288 },
Matteo Scandolob6ca3012016-06-03 16:58:43 -0700289 select: {
arpiagariu47cd6892016-06-03 16:41:39 -0700290 label: 'Select Label:',
291 type: 'select',
292 hint: 'Select Hint',
293 validators: {},
Matteo Scandolob6ca3012016-06-03 16:58:43 -0700294 options: [
295 {id: 1, label: 'something'}
296 ]
Matteo Scandolodee14362016-06-08 15:11:39 -0700297 },
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 Scandolo03d8b8e2016-05-25 17:37:37 -0700315 }
316 };
317
318 let empty_model = {5: 'Nan'}
319
320 it('should create a form object', () => {
arpiagariu47cd6892016-06-03 16:41:39 -0700321 let res = service.buildFormStructure(empty_modelField, empty_customFields, empty_model);
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700322 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);
arpiagariu47cd6892016-06-03 16:41:39 -0700328 expect(res.select).toEqual(empty_formObject.select);
Matteo Scandolodee14362016-06-08 15:11:39 -0700329 expect(res.object).toEqual(empty_formObject.object);
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700330 expect(res).toEqual(empty_formObject);
331 });
332 });
333 });
334 });
335})();