blob: a126db5cfb9a776a7f60e1829085c3c82d13b660 [file] [log] [blame]
Matteo Scandoloa5d03d52016-07-21 11:35:46 -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',
20 'created'
21 ];
22
23 let modelField = {
24 id: {},
25 name: {},
26 mail: {},
27 active: {},
28 created: {}
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 = {
41 id: {
42 label: 'Id',
43 type: 'number',
44 validators: {
45 required: true
46 },
47 hint: ''
48 },
49 custom: {
50 label: 'Custom Label',
51 type: 'number',
52 validators: {},
53 hint: 'Test Hint'
54 }
55 };
56
57 let formObject = {
58 id: {
59 label: 'Id:',
60 type: 'number',
61 validators: {
62 required: true
63 },
64 hint: ''
65 },
66 name: {
67 label: 'Name:',
68 type: 'text',
69 validators: {},
70 hint: ''
71 },
72 mail: {
73 label: 'Mail:',
74 type: 'email',
75 validators: {},
76 hint: ''
77 },
78 active: {
79 label: 'Active:',
80 type: 'boolean',
81 validators: {},
82 hint: ''
83 },
84 created: {
85 label: 'Created:',
86 type: 'date',
87 validators: {},
88 hint: ''
89 },
90 custom: {
91 label: 'Custom Label:',
92 type: 'number',
93 validators: {},
94 hint: 'Test Hint'
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
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 });
159 });
160
161 describe('when modelField are provided', () => {
162 it('should combine modelField and customField in a form object', () => {
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 });
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',
222 type: 'number',
223 hint: 'Test Hint'
224 },
225 select: {
226 label: 'Select Label',
227 type: 'select',
228 hint: 'Select Hint',
229 options: [
230 {id: 1, label: 'something'}
231 ]
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 }
249 }
250 };
251
252 let empty_formObject = {
253 id: {
254 label: 'Id:',
255 type: 'number',
256 validators: {},
257 hint: ''
258 },
259 name: {
260 label: 'Name:',
261 type: 'text',
262 validators: {},
263 hint: ''
264 },
265 mail: {
266 label: 'Mail:',
267 type: 'email',
268 validators: {},
269 hint: ''
270 },
271 active: {
272 label: 'Active:',
273 type: 'boolean',
274 validators: {},
275 hint: ''
276 },
277 created: {
278 label: 'Created:',
279 type: 'date',
280 validators: {},
281 hint: ''
282 },
283 custom: {
284 label: 'Custom Label:',
285 type: 'number',
286 validators: {},
287 hint: 'Test Hint'
288 },
289 select: {
290 label: 'Select Label:',
291 type: 'select',
292 hint: 'Select Hint',
293 validators: {},
294 options: [
295 {id: 1, label: 'something'}
296 ]
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 }
315 }
316 };
317
318 let empty_model = {5: 'Nan'}
319
320 it('should create a form object', () => {
321 let res = service.buildFormStructure(empty_modelField, empty_customFields, empty_model);
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);
328 expect(res.select).toEqual(empty_formObject.select);
329 expect(res.object).toEqual(empty_formObject.object);
330 expect(res).toEqual(empty_formObject);
331 });
332 });
333 });
334 });
335})();