blob: f6bfbb25a5c24b6b238098c533509fd34c50fceb [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'
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700188 }
189 };
190
191 let empty_formObject = {
192 id: {
193 label: 'Id:',
194 type: 'number',
Matteo Scandolo62e0c612016-05-31 15:15:00 -0700195 validators: {},
196 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700197 },
198 name: {
199 label: 'Name:',
200 type: 'text',
Matteo Scandolo62e0c612016-05-31 15:15:00 -0700201 validators: {},
202 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700203 },
204 mail: {
205 label: 'Mail:',
206 type: 'email',
Matteo Scandolo62e0c612016-05-31 15:15:00 -0700207 validators: {},
208 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700209 },
210 active: {
211 label: 'Active:',
212 type: 'boolean',
Matteo Scandolo62e0c612016-05-31 15:15:00 -0700213 validators: {},
214 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700215 },
216 created: {
217 label: 'Created:',
218 type: 'date',
Matteo Scandolo62e0c612016-05-31 15:15:00 -0700219 validators: {},
220 hint: ''
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700221 },
222 custom: {
223 label: 'Custom Label:',
224 type: 'number',
Matteo Scandolo62e0c612016-05-31 15:15:00 -0700225 validators: {},
226 hint: 'Test Hint'
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700227 }
228 };
229
230 let empty_model = {5: 'Nan'}
231
232 it('should create a form object', () => {
233 let res = service.buildFormStructure(empty_modelField, empty_customFields, empty_model)
234 expect(res.id).toEqual(empty_formObject.id);
235 expect(res.name).toEqual(empty_formObject.name);
236 expect(res.mail).toEqual(empty_formObject.mail);
237 expect(res.active).toEqual(empty_formObject.active);
238 expect(res.created).toEqual(empty_formObject.created);
239 expect(res.custom).toEqual(empty_formObject.custom);
240 expect(res).toEqual(empty_formObject);
241 });
242 });
243 });
244 });
245})();