blob: aa37c25702e0836370987763e3b58a3ea4913262 [file] [log] [blame]
Matteo Scandolo686547a2017-08-08 13:05:25 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloa5d03d52016-07-21 11:35:46 -070019/**
20 * © OpenCORD
21 *
22 * Created by teone on 5/25/16.
23 */
24
25(function () {
26 'use strict';
27
28 describe('The xos.helper module', function(){
29
30 describe('The XosFormHelper service', () => {
31 let service;
32
33 let fields = [
34 'id',
35 'name',
36 'mail',
37 'active',
38 'created'
39 ];
40
41 let modelField = {
42 id: {},
43 name: {},
44 mail: {},
45 active: {},
46 created: {}
47 };
48
49 let model = {
50 id: 1,
51 name: 'test',
52 mail: 'test@onlab.us',
53 active: true,
54 created: '2016-04-18T23:44:16.883181Z',
55 custom: 'MyCustomValue'
56 };
57
58 let customField = {
59 id: {
60 label: 'Id',
61 type: 'number',
62 validators: {
63 required: true
64 },
65 hint: ''
66 },
67 custom: {
68 label: 'Custom Label',
69 type: 'number',
70 validators: {},
71 hint: 'Test Hint'
72 }
73 };
74
75 let formObject = {
76 id: {
77 label: 'Id:',
78 type: 'number',
79 validators: {
80 required: true
81 },
82 hint: ''
83 },
84 name: {
85 label: 'Name:',
86 type: 'text',
87 validators: {},
88 hint: ''
89 },
90 mail: {
91 label: 'Mail:',
92 type: 'email',
93 validators: {},
94 hint: ''
95 },
96 active: {
97 label: 'Active:',
98 type: 'boolean',
99 validators: {},
100 hint: ''
101 },
102 created: {
103 label: 'Created:',
104 type: 'date',
105 validators: {},
106 hint: ''
107 },
108 custom: {
109 label: 'Custom Label:',
110 type: 'number',
111 validators: {},
112 hint: 'Test Hint'
113 }
114 };
115
116 // load the application module
117 beforeEach(module('xos.helpers'));
118
119 // inject the cartService
120 beforeEach(inject(function (_XosFormHelpers_) {
121 // The injector unwraps the underscores (_) from around the parameter names when matching
122 service = _XosFormHelpers_;
123 }));
124
125 describe('the _isEmail method', () => {
126 it('should return true', () => {
127 expect(service._isEmail('test@onlab.us')).toEqual(true);
128 });
129 it('should return false', () => {
130 expect(service._isEmail('testonlab.us')).toEqual(false);
131 expect(service._isEmail('test@onlab')).toEqual(false);
132 });
133 });
134
135 describe('the _getFieldFormat method', () => {
136 it('should return text', () => {
Matteo Scandoloe57712f2016-09-21 15:27:36 -0700137 expect(service._getFieldFormat('cordSubscriber-1')).toEqual('text');
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700138 expect(service._getFieldFormat('a random text')).toEqual('text');
139 expect(service._getFieldFormat(null)).toEqual('text');
140 expect(service._getFieldFormat('1')).toEqual('text');
141 });
142 it('should return mail', () => {
143 expect(service._getFieldFormat('test@onlab.us')).toEqual('email');
144 });
145 it('should return number', () => {
146 expect(service._getFieldFormat(1)).toEqual('number');
147 });
148 it('should return boolean', () => {
149 expect(service._getFieldFormat(false)).toEqual('boolean');
150 expect(service._getFieldFormat(true)).toEqual('boolean');
151 });
152
153 it('should return date', () => {
154 expect(service._getFieldFormat('2016-04-19T23:09:1092Z')).toEqual('text');
155 expect(service._getFieldFormat(new Date())).toEqual('date');
156 expect(service._getFieldFormat('2016-04-19T23:09:10.208092Z')).toEqual('date');
157 });
158
159 it('should return array', () => {
160 expect(service._getFieldFormat([])).toEqual('array');
161 expect(service._getFieldFormat(['a', 'b'])).toEqual('array');
162 });
163
164 it('should return object', () => {
165 expect(service._getFieldFormat({})).toEqual('object');
166 expect(service._getFieldFormat({foo: 'bar'})).toEqual('object');
167 });
168 });
169
170 describe('the parseModelField mehtod', () => {
171 it('should convert the fields array in an empty form object', () => {
172 expect(service.parseModelField(fields)).toEqual(modelField);
173 });
174
175 xit('should handle nested config', () => {
176
177 });
178 });
179
180 describe('when modelField are provided', () => {
181 it('should combine modelField and customField in a form object', () => {
182 const form = service.buildFormStructure(modelField, customField, model);
183 expect(form).toEqual(formObject);
184 });
185
186 it('should override modelField properties whith customField properties', () => {
187 const customFieldOverride = {
188 id: {
189 hint: 'something',
190 type: 'select',
191 options: [
192 {id: 1, label: 'one'},
193 {id: 2, label: 'two'}
194 ],
195 validators: {
196 required: true
197 }
198 }
199 };
200 const form = service.buildFormStructure({id: {}}, customFieldOverride, model);
201
202 expect(form).toEqual({
203 id: {
204 label: 'Id:',
205 validators: {required: true},
206 hint: customFieldOverride.id.hint,
207 type: customFieldOverride.id.type,
208 options: customFieldOverride.id.options
209 }
210 });
211 });
212 });
213
214 describe('when model field is an empty array', () => {
215 let empty_modelField = {
216 // 5: {}
217 };
218 let empty_customFields = {
219 id: {
220 label: 'Id',
221 type: 'number'
222 },
223 name: {
224 label: 'Name',
225 type: 'text'
226 },
227 mail: {
228 label: 'Mail',
229 type: 'email'
230 },
231 active: {
232 label: 'Active',
233 type: 'boolean'
234 },
235 created: {
236 label: 'Created',
237 type: 'date'
238 },
239 custom: {
240 label: 'Custom Label',
241 type: 'number',
242 hint: 'Test Hint'
243 },
244 select: {
245 label: 'Select Label',
246 type: 'select',
247 hint: 'Select Hint',
248 options: [
249 {id: 1, label: 'something'}
250 ]
251 },
252 object: {
253 label: 'Object Label',
254 type: 'object',
255 hint: 'Object Hint',
256 properties: {
257 foo: {
258 type: 'string',
259 label: 'FooLabel',
260 validators: {
261 required: true
262 }
263 },
264 bar: {
265 type: 'number'
266 }
267 }
268 }
269 };
270
271 let empty_formObject = {
272 id: {
273 label: 'Id:',
274 type: 'number',
275 validators: {},
276 hint: ''
277 },
278 name: {
279 label: 'Name:',
280 type: 'text',
281 validators: {},
282 hint: ''
283 },
284 mail: {
285 label: 'Mail:',
286 type: 'email',
287 validators: {},
288 hint: ''
289 },
290 active: {
291 label: 'Active:',
292 type: 'boolean',
293 validators: {},
294 hint: ''
295 },
296 created: {
297 label: 'Created:',
298 type: 'date',
299 validators: {},
300 hint: ''
301 },
302 custom: {
303 label: 'Custom Label:',
304 type: 'number',
305 validators: {},
306 hint: 'Test Hint'
307 },
308 select: {
309 label: 'Select Label:',
310 type: 'select',
311 hint: 'Select Hint',
312 validators: {},
313 options: [
314 {id: 1, label: 'something'}
315 ]
316 },
317 object: {
318 label: 'Object Label:',
319 type: 'object',
320 hint: 'Object Hint',
321 validators: {},
322 properties: {
323 foo: {
324 type: 'string',
325 label: 'FooLabel',
326 validators: {
327 required: true
328 }
329 },
330 bar: {
331 type: 'number'
332 }
333 }
334 }
335 };
336
337 let empty_model = {5: 'Nan'}
338
339 it('should create a form object', () => {
340 let res = service.buildFormStructure(empty_modelField, empty_customFields, empty_model);
341 expect(res.id).toEqual(empty_formObject.id);
342 expect(res.name).toEqual(empty_formObject.name);
343 expect(res.mail).toEqual(empty_formObject.mail);
344 expect(res.active).toEqual(empty_formObject.active);
345 expect(res.created).toEqual(empty_formObject.created);
346 expect(res.custom).toEqual(empty_formObject.custom);
347 expect(res.select).toEqual(empty_formObject.select);
348 expect(res.object).toEqual(empty_formObject.object);
349 expect(res).toEqual(empty_formObject);
350 });
351 });
352 });
353 });
354})();