blob: 29d812d6f58e1ff452cbd14d6d1297ce48b011ea [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',
20 'created',
21 'custom'
22 ];
23
24 let modelField = {
25 id: {},
26 name: {},
27 mail: {},
28 active: {},
29 created: {},
30 custom: {}
31 };
32
33 let model = {
34 id: 1,
35 name: 'test',
36 mail: 'test@onlab.us',
37 active: true,
38 created: '2016-04-18T23:44:16.883181Z',
39 custom: 'MyCustomValue'
40 };
41
42 let customField = {
43 custom: {
44 label: 'Custom Label',
45 type: 'number',
Matteo Scandolo62e0c612016-05-31 15:15:00 -070046 validators: {},
47 hint: 'Test Hint'
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070048 }
49 };
50
51 let formObject = {
52 id: {
53 label: 'Id:',
54 type: 'number',
Matteo Scandolo62e0c612016-05-31 15:15:00 -070055 validators: {},
56 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070057 },
58 name: {
59 label: 'Name:',
60 type: 'text',
Matteo Scandolo62e0c612016-05-31 15:15:00 -070061 validators: {},
62 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070063 },
64 mail: {
65 label: 'Mail:',
66 type: 'email',
Matteo Scandolo62e0c612016-05-31 15:15:00 -070067 validators: {},
68 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070069 },
70 active: {
71 label: 'Active:',
72 type: 'boolean',
Matteo Scandolo62e0c612016-05-31 15:15:00 -070073 validators: {},
74 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070075 },
76 created: {
77 label: 'Created:',
78 type: 'date',
Matteo Scandolo62e0c612016-05-31 15:15:00 -070079 validators: {},
80 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070081 },
82 custom: {
83 label: 'Custom Label:',
84 type: 'number',
Matteo Scandolo62e0c612016-05-31 15:15:00 -070085 validators: {},
86 hint: 'Test Hint'
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070087 }
88 };
89
90 // load the application module
91 beforeEach(module('xos.helpers'));
92
93 // inject the cartService
94 beforeEach(inject(function (_XosFormHelpers_) {
95 // The injector unwraps the underscores (_) from around the parameter names when matching
96 service = _XosFormHelpers_;
97 }));
98
99 describe('the _isEmail method', () => {
100 it('should return true', () => {
101 expect(service._isEmail('test@onlab.us')).toEqual(true);
102 });
103 it('should return false', () => {
104 expect(service._isEmail('testonlab.us')).toEqual(false);
105 expect(service._isEmail('test@onlab')).toEqual(false);
106 });
107 });
108
109 describe('the _getFieldFormat method', () => {
110 it('should return text', () => {
111 expect(service._getFieldFormat('a random text')).toEqual('text');
112 expect(service._getFieldFormat(null)).toEqual('text');
113 expect(service._getFieldFormat('1')).toEqual('text');
114 });
115 it('should return mail', () => {
116 expect(service._getFieldFormat('test@onlab.us')).toEqual('email');
117 });
118 it('should return number', () => {
119 expect(service._getFieldFormat(1)).toEqual('number');
120 });
121 it('should return boolean', () => {
122 expect(service._getFieldFormat(false)).toEqual('boolean');
123 expect(service._getFieldFormat(true)).toEqual('boolean');
124 });
125
126 it('should return date', () => {
127 expect(service._getFieldFormat('2016-04-19T23:09:1092Z')).toEqual('text');
128 expect(service._getFieldFormat(new Date())).toEqual('date');
129 expect(service._getFieldFormat('2016-04-19T23:09:10.208092Z')).toEqual('date');
130 });
131
132 it('should return array', () => {
133 expect(service._getFieldFormat([])).toEqual('array');
134 expect(service._getFieldFormat(['a', 'b'])).toEqual('array');
135 });
136
137 it('should return object', () => {
138 expect(service._getFieldFormat({})).toEqual('object');
139 expect(service._getFieldFormat({foo: 'bar'})).toEqual('object');
140 });
141 });
142
Matteo Scandolo2aca9ab2016-05-26 15:09:55 -0700143 describe('the parseModelField mehtod', () => {
144 it('should convert the fields array in an empty form object', () => {
145 expect(service.parseModelField(fields)).toEqual(modelField);
146 });
147
148 xit('should handle nested config', () => {
149
150 });
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700151 });
152
153 describe('when modelField are provided', () => {
154 it('should combine modelField and customField in a form object', () => {
155 expect(service.buildFormStructure(modelField, customField, model)).toEqual(formObject);
156 });
157 });
158
159 describe('when model field is an empty array', () => {
160 let empty_modelField = {
161 // 5: {}
162 };
163 let empty_customFields = {
164 id: {
165 label: 'Id',
166 type: 'number'
167 },
168 name: {
169 label: 'Name',
170 type: 'text'
171 },
172 mail: {
173 label: 'Mail',
174 type: 'email'
175 },
176 active: {
177 label: 'Active',
178 type: 'boolean'
179 },
180 created: {
181 label: 'Created',
182 type: 'date'
183 },
184 custom: {
185 label: 'Custom Label',
Matteo Scandolo62e0c612016-05-31 15:15:00 -0700186 type: 'number',
187 hint: 'Test Hint'
arpiagariu7c1975d2016-06-03 16:41:39 -0700188 },
189 select:{
190 label: 'Select Label',
191 type: 'select',
192 hint: 'Select Hint',
193 options:{}
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700194 }
195 };
196
197 let empty_formObject = {
198 id: {
199 label: 'Id:',
200 type: 'number',
Matteo Scandolo62e0c612016-05-31 15:15:00 -0700201 validators: {},
202 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700203 },
204 name: {
205 label: 'Name:',
206 type: 'text',
Matteo Scandolo62e0c612016-05-31 15:15:00 -0700207 validators: {},
208 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700209 },
210 mail: {
211 label: 'Mail:',
212 type: 'email',
Matteo Scandolo62e0c612016-05-31 15:15:00 -0700213 validators: {},
214 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700215 },
216 active: {
217 label: 'Active:',
218 type: 'boolean',
Matteo Scandolo62e0c612016-05-31 15:15:00 -0700219 validators: {},
220 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700221 },
222 created: {
223 label: 'Created:',
224 type: 'date',
Matteo Scandolo62e0c612016-05-31 15:15:00 -0700225 validators: {},
226 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700227 },
228 custom: {
229 label: 'Custom Label:',
230 type: 'number',
Matteo Scandolo62e0c612016-05-31 15:15:00 -0700231 validators: {},
232 hint: 'Test Hint'
arpiagariu7c1975d2016-06-03 16:41:39 -0700233 },
234 select:{
235 label: 'Select Label:',
236 type: 'select',
237 hint: 'Select Hint',
238 validators: {},
239 options:{}
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700240 }
241 };
242
243 let empty_model = {5: 'Nan'}
244
245 it('should create a form object', () => {
arpiagariu7c1975d2016-06-03 16:41:39 -0700246 let res = service.buildFormStructure(empty_modelField, empty_customFields, empty_model);
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700247 expect(res.id).toEqual(empty_formObject.id);
248 expect(res.name).toEqual(empty_formObject.name);
249 expect(res.mail).toEqual(empty_formObject.mail);
250 expect(res.active).toEqual(empty_formObject.active);
251 expect(res.created).toEqual(empty_formObject.created);
252 expect(res.custom).toEqual(empty_formObject.custom);
arpiagariu7c1975d2016-06-03 16:41:39 -0700253 expect(res.select).toEqual(empty_formObject.select);
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700254 expect(res).toEqual(empty_formObject);
255 });
256 });
257 });
258 });
259})();