blob: 21e2649c45f313a93e9608e9637e11db5d55d9fe [file] [log] [blame]
Matteo Scandolo03d8b8e2016-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 Scandolo5dfcb082016-05-31 15:15:00 -070046 validators: {},
47 hint: 'Test Hint'
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070048 }
49 };
50
51 let formObject = {
52 id: {
53 label: 'Id:',
54 type: 'number',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070055 validators: {},
56 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070057 },
58 name: {
59 label: 'Name:',
60 type: 'text',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070061 validators: {},
62 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070063 },
64 mail: {
65 label: 'Mail:',
66 type: 'email',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070067 validators: {},
68 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070069 },
70 active: {
71 label: 'Active:',
72 type: 'boolean',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070073 validators: {},
74 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070075 },
76 created: {
77 label: 'Created:',
78 type: 'date',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070079 validators: {},
80 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070081 },
82 custom: {
83 label: 'Custom Label:',
84 type: 'number',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070085 validators: {},
86 hint: 'Test Hint'
Matteo Scandolo03d8b8e2016-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 Scandolob9fb6252016-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 Scandolo03d8b8e2016-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 Scandolo5dfcb082016-05-31 15:15:00 -0700186 type: 'number',
187 hint: 'Test Hint'
arpiagariu47cd6892016-06-03 16:41:39 -0700188 },
Matteo Scandolob6ca3012016-06-03 16:58:43 -0700189 select: {
arpiagariu47cd6892016-06-03 16:41:39 -0700190 label: 'Select Label',
191 type: 'select',
192 hint: 'Select Hint',
Matteo Scandolob6ca3012016-06-03 16:58:43 -0700193 options: [
194 {id: 1, label: 'something'}
195 ]
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700196 }
197 };
198
199 let empty_formObject = {
200 id: {
201 label: 'Id:',
202 type: 'number',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -0700203 validators: {},
204 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700205 },
206 name: {
207 label: 'Name:',
208 type: 'text',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -0700209 validators: {},
210 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700211 },
212 mail: {
213 label: 'Mail:',
214 type: 'email',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -0700215 validators: {},
216 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700217 },
218 active: {
219 label: 'Active:',
220 type: 'boolean',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -0700221 validators: {},
222 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700223 },
224 created: {
225 label: 'Created:',
226 type: 'date',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -0700227 validators: {},
228 hint: ''
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700229 },
230 custom: {
231 label: 'Custom Label:',
232 type: 'number',
Matteo Scandolo5dfcb082016-05-31 15:15:00 -0700233 validators: {},
234 hint: 'Test Hint'
arpiagariu47cd6892016-06-03 16:41:39 -0700235 },
Matteo Scandolob6ca3012016-06-03 16:58:43 -0700236 select: {
arpiagariu47cd6892016-06-03 16:41:39 -0700237 label: 'Select Label:',
238 type: 'select',
239 hint: 'Select Hint',
240 validators: {},
Matteo Scandolob6ca3012016-06-03 16:58:43 -0700241 options: [
242 {id: 1, label: 'something'}
243 ]
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700244 }
245 };
246
247 let empty_model = {5: 'Nan'}
248
249 it('should create a form object', () => {
arpiagariu47cd6892016-06-03 16:41:39 -0700250 let res = service.buildFormStructure(empty_modelField, empty_customFields, empty_model);
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700251 expect(res.id).toEqual(empty_formObject.id);
252 expect(res.name).toEqual(empty_formObject.name);
253 expect(res.mail).toEqual(empty_formObject.mail);
254 expect(res.active).toEqual(empty_formObject.active);
255 expect(res.created).toEqual(empty_formObject.created);
256 expect(res.custom).toEqual(empty_formObject.custom);
arpiagariu47cd6892016-06-03 16:41:39 -0700257 expect(res.select).toEqual(empty_formObject.select);
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700258 expect(res).toEqual(empty_formObject);
259 });
260 });
261 });
262 });
263})();