blob: 308d503acde878cfd81c66dd238a5608b408b4b3 [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', () => {
Matteo Scandoloe57712f2016-09-21 15:27:36 -0700119 expect(service._getFieldFormat('cordSubscriber-1')).toEqual('text');
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700120 expect(service._getFieldFormat('a random text')).toEqual('text');
121 expect(service._getFieldFormat(null)).toEqual('text');
122 expect(service._getFieldFormat('1')).toEqual('text');
123 });
124 it('should return mail', () => {
125 expect(service._getFieldFormat('test@onlab.us')).toEqual('email');
126 });
127 it('should return number', () => {
128 expect(service._getFieldFormat(1)).toEqual('number');
129 });
130 it('should return boolean', () => {
131 expect(service._getFieldFormat(false)).toEqual('boolean');
132 expect(service._getFieldFormat(true)).toEqual('boolean');
133 });
134
135 it('should return date', () => {
136 expect(service._getFieldFormat('2016-04-19T23:09:1092Z')).toEqual('text');
137 expect(service._getFieldFormat(new Date())).toEqual('date');
138 expect(service._getFieldFormat('2016-04-19T23:09:10.208092Z')).toEqual('date');
139 });
140
141 it('should return array', () => {
142 expect(service._getFieldFormat([])).toEqual('array');
143 expect(service._getFieldFormat(['a', 'b'])).toEqual('array');
144 });
145
146 it('should return object', () => {
147 expect(service._getFieldFormat({})).toEqual('object');
148 expect(service._getFieldFormat({foo: 'bar'})).toEqual('object');
149 });
150 });
151
152 describe('the parseModelField mehtod', () => {
153 it('should convert the fields array in an empty form object', () => {
154 expect(service.parseModelField(fields)).toEqual(modelField);
155 });
156
157 xit('should handle nested config', () => {
158
159 });
160 });
161
162 describe('when modelField are provided', () => {
163 it('should combine modelField and customField in a form object', () => {
164 const form = service.buildFormStructure(modelField, customField, model);
165 expect(form).toEqual(formObject);
166 });
167
168 it('should override modelField properties whith customField properties', () => {
169 const customFieldOverride = {
170 id: {
171 hint: 'something',
172 type: 'select',
173 options: [
174 {id: 1, label: 'one'},
175 {id: 2, label: 'two'}
176 ],
177 validators: {
178 required: true
179 }
180 }
181 };
182 const form = service.buildFormStructure({id: {}}, customFieldOverride, model);
183
184 expect(form).toEqual({
185 id: {
186 label: 'Id:',
187 validators: {required: true},
188 hint: customFieldOverride.id.hint,
189 type: customFieldOverride.id.type,
190 options: customFieldOverride.id.options
191 }
192 });
193 });
194 });
195
196 describe('when model field is an empty array', () => {
197 let empty_modelField = {
198 // 5: {}
199 };
200 let empty_customFields = {
201 id: {
202 label: 'Id',
203 type: 'number'
204 },
205 name: {
206 label: 'Name',
207 type: 'text'
208 },
209 mail: {
210 label: 'Mail',
211 type: 'email'
212 },
213 active: {
214 label: 'Active',
215 type: 'boolean'
216 },
217 created: {
218 label: 'Created',
219 type: 'date'
220 },
221 custom: {
222 label: 'Custom Label',
223 type: 'number',
224 hint: 'Test Hint'
225 },
226 select: {
227 label: 'Select Label',
228 type: 'select',
229 hint: 'Select Hint',
230 options: [
231 {id: 1, label: 'something'}
232 ]
233 },
234 object: {
235 label: 'Object Label',
236 type: 'object',
237 hint: 'Object Hint',
238 properties: {
239 foo: {
240 type: 'string',
241 label: 'FooLabel',
242 validators: {
243 required: true
244 }
245 },
246 bar: {
247 type: 'number'
248 }
249 }
250 }
251 };
252
253 let empty_formObject = {
254 id: {
255 label: 'Id:',
256 type: 'number',
257 validators: {},
258 hint: ''
259 },
260 name: {
261 label: 'Name:',
262 type: 'text',
263 validators: {},
264 hint: ''
265 },
266 mail: {
267 label: 'Mail:',
268 type: 'email',
269 validators: {},
270 hint: ''
271 },
272 active: {
273 label: 'Active:',
274 type: 'boolean',
275 validators: {},
276 hint: ''
277 },
278 created: {
279 label: 'Created:',
280 type: 'date',
281 validators: {},
282 hint: ''
283 },
284 custom: {
285 label: 'Custom Label:',
286 type: 'number',
287 validators: {},
288 hint: 'Test Hint'
289 },
290 select: {
291 label: 'Select Label:',
292 type: 'select',
293 hint: 'Select Hint',
294 validators: {},
295 options: [
296 {id: 1, label: 'something'}
297 ]
298 },
299 object: {
300 label: 'Object Label:',
301 type: 'object',
302 hint: 'Object Hint',
303 validators: {},
304 properties: {
305 foo: {
306 type: 'string',
307 label: 'FooLabel',
308 validators: {
309 required: true
310 }
311 },
312 bar: {
313 type: 'number'
314 }
315 }
316 }
317 };
318
319 let empty_model = {5: 'Nan'}
320
321 it('should create a form object', () => {
322 let res = service.buildFormStructure(empty_modelField, empty_customFields, empty_model);
323 expect(res.id).toEqual(empty_formObject.id);
324 expect(res.name).toEqual(empty_formObject.name);
325 expect(res.mail).toEqual(empty_formObject.mail);
326 expect(res.active).toEqual(empty_formObject.active);
327 expect(res.created).toEqual(empty_formObject.created);
328 expect(res.custom).toEqual(empty_formObject.custom);
329 expect(res.select).toEqual(empty_formObject.select);
330 expect(res.object).toEqual(empty_formObject.object);
331 expect(res).toEqual(empty_formObject);
332 });
333 });
334 });
335 });
336})();