blob: 3c2405dedca4335012968868d784e7da49e02f55 [file] [log] [blame]
Matteo Scandolodf23cdb2016-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 Scandoloc00865d2016-06-06 15:21:21 -070020 'created'
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070021 ];
22
23 let modelField = {
24 id: {},
25 name: {},
26 mail: {},
27 active: {},
Matteo Scandoloc00865d2016-06-06 15:21:21 -070028 created: {}
Matteo Scandolodf23cdb2016-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 Scandoloc00865d2016-06-06 15:21:21 -070041 id: {
42 label: 'Id',
43 type: 'number',
44 validators: {
45 required: true
46 },
47 hint: ''
48 },
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070049 custom: {
50 label: 'Custom Label',
51 type: 'number',
Matteo Scandolo62e0c612016-05-31 15:15:00 -070052 validators: {},
53 hint: 'Test Hint'
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070054 }
55 };
56
57 let formObject = {
58 id: {
59 label: 'Id:',
60 type: 'number',
Matteo Scandoloc00865d2016-06-06 15:21:21 -070061 validators: {
62 required: true
63 },
Matteo Scandolo62e0c612016-05-31 15:15:00 -070064 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070065 },
66 name: {
67 label: 'Name:',
68 type: 'text',
Matteo Scandolo62e0c612016-05-31 15:15:00 -070069 validators: {},
70 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070071 },
72 mail: {
73 label: 'Mail:',
74 type: 'email',
Matteo Scandolo62e0c612016-05-31 15:15:00 -070075 validators: {},
76 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070077 },
78 active: {
79 label: 'Active:',
80 type: 'boolean',
Matteo Scandolo62e0c612016-05-31 15:15:00 -070081 validators: {},
82 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070083 },
84 created: {
85 label: 'Created:',
86 type: 'date',
Matteo Scandolo62e0c612016-05-31 15:15:00 -070087 validators: {},
88 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070089 },
90 custom: {
91 label: 'Custom Label:',
92 type: 'number',
Matteo Scandolo62e0c612016-05-31 15:15:00 -070093 validators: {},
94 hint: 'Test Hint'
Matteo Scandolodf23cdb2016-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 Scandolo2aca9ab2016-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 Scandolodf23cdb2016-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 Scandoloc00865d2016-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 Scandolodf23cdb2016-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 Scandolo62e0c612016-05-31 15:15:00 -0700222 type: 'number',
223 hint: 'Test Hint'
arpiagariu7c1975d2016-06-03 16:41:39 -0700224 },
Matteo Scandolobe136072016-06-03 16:58:43 -0700225 select: {
arpiagariu7c1975d2016-06-03 16:41:39 -0700226 label: 'Select Label',
227 type: 'select',
228 hint: 'Select Hint',
Matteo Scandolobe136072016-06-03 16:58:43 -0700229 options: [
230 {id: 1, label: 'something'}
231 ]
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700232 }
233 };
234
235 let empty_formObject = {
236 id: {
237 label: 'Id:',
238 type: 'number',
Matteo Scandolo62e0c612016-05-31 15:15:00 -0700239 validators: {},
240 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700241 },
242 name: {
243 label: 'Name:',
244 type: 'text',
Matteo Scandolo62e0c612016-05-31 15:15:00 -0700245 validators: {},
246 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700247 },
248 mail: {
249 label: 'Mail:',
250 type: 'email',
Matteo Scandolo62e0c612016-05-31 15:15:00 -0700251 validators: {},
252 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700253 },
254 active: {
255 label: 'Active:',
256 type: 'boolean',
Matteo Scandolo62e0c612016-05-31 15:15:00 -0700257 validators: {},
258 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700259 },
260 created: {
261 label: 'Created:',
262 type: 'date',
Matteo Scandolo62e0c612016-05-31 15:15:00 -0700263 validators: {},
264 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700265 },
266 custom: {
267 label: 'Custom Label:',
268 type: 'number',
Matteo Scandolo62e0c612016-05-31 15:15:00 -0700269 validators: {},
270 hint: 'Test Hint'
arpiagariu7c1975d2016-06-03 16:41:39 -0700271 },
Matteo Scandolobe136072016-06-03 16:58:43 -0700272 select: {
arpiagariu7c1975d2016-06-03 16:41:39 -0700273 label: 'Select Label:',
274 type: 'select',
275 hint: 'Select Hint',
276 validators: {},
Matteo Scandolobe136072016-06-03 16:58:43 -0700277 options: [
278 {id: 1, label: 'something'}
279 ]
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700280 }
281 };
282
283 let empty_model = {5: 'Nan'}
284
285 it('should create a form object', () => {
arpiagariu7c1975d2016-06-03 16:41:39 -0700286 let res = service.buildFormStructure(empty_modelField, empty_customFields, empty_model);
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700287 expect(res.id).toEqual(empty_formObject.id);
288 expect(res.name).toEqual(empty_formObject.name);
289 expect(res.mail).toEqual(empty_formObject.mail);
290 expect(res.active).toEqual(empty_formObject.active);
291 expect(res.created).toEqual(empty_formObject.created);
292 expect(res.custom).toEqual(empty_formObject.custom);
arpiagariu7c1975d2016-06-03 16:41:39 -0700293 expect(res.select).toEqual(empty_formObject.select);
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700294 expect(res).toEqual(empty_formObject);
295 });
296 });
297 });
298 });
299})();