blob: 404f63a18425d2fdb7957cdaf6a6d05db5e1db42 [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',
46 validators: {}
47 }
48 };
49
50 let formObject = {
51 id: {
52 label: 'Id:',
53 type: 'number',
54 validators: {}
55 },
56 name: {
57 label: 'Name:',
58 type: 'text',
59 validators: {}
60 },
61 mail: {
62 label: 'Mail:',
63 type: 'email',
64 validators: {}
65 },
66 active: {
67 label: 'Active:',
68 type: 'boolean',
69 validators: {}
70 },
71 created: {
72 label: 'Created:',
73 type: 'date',
74 validators: {}
75 },
76 custom: {
77 label: 'Custom Label:',
78 type: 'number',
79 validators: {}
80 }
81 };
82
83 // load the application module
84 beforeEach(module('xos.helpers'));
85
86 // inject the cartService
87 beforeEach(inject(function (_XosFormHelpers_) {
88 // The injector unwraps the underscores (_) from around the parameter names when matching
89 service = _XosFormHelpers_;
90 }));
91
92 describe('the _isEmail method', () => {
93 it('should return true', () => {
94 expect(service._isEmail('test@onlab.us')).toEqual(true);
95 });
96 it('should return false', () => {
97 expect(service._isEmail('testonlab.us')).toEqual(false);
98 expect(service._isEmail('test@onlab')).toEqual(false);
99 });
100 });
101
102 describe('the _getFieldFormat method', () => {
103 it('should return text', () => {
104 expect(service._getFieldFormat('a random text')).toEqual('text');
105 expect(service._getFieldFormat(null)).toEqual('text');
106 expect(service._getFieldFormat('1')).toEqual('text');
107 });
108 it('should return mail', () => {
109 expect(service._getFieldFormat('test@onlab.us')).toEqual('email');
110 });
111 it('should return number', () => {
112 expect(service._getFieldFormat(1)).toEqual('number');
113 });
114 it('should return boolean', () => {
115 expect(service._getFieldFormat(false)).toEqual('boolean');
116 expect(service._getFieldFormat(true)).toEqual('boolean');
117 });
118
119 it('should return date', () => {
120 expect(service._getFieldFormat('2016-04-19T23:09:1092Z')).toEqual('text');
121 expect(service._getFieldFormat(new Date())).toEqual('date');
122 expect(service._getFieldFormat('2016-04-19T23:09:10.208092Z')).toEqual('date');
123 });
124
125 it('should return array', () => {
126 expect(service._getFieldFormat([])).toEqual('array');
127 expect(service._getFieldFormat(['a', 'b'])).toEqual('array');
128 });
129
130 it('should return object', () => {
131 expect(service._getFieldFormat({})).toEqual('object');
132 expect(service._getFieldFormat({foo: 'bar'})).toEqual('object');
133 });
134 });
135
Matteo Scandolo2aca9ab2016-05-26 15:09:55 -0700136 describe('the parseModelField mehtod', () => {
137 it('should convert the fields array in an empty form object', () => {
138 expect(service.parseModelField(fields)).toEqual(modelField);
139 });
140
141 xit('should handle nested config', () => {
142
143 });
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700144 });
145
146 describe('when modelField are provided', () => {
147 it('should combine modelField and customField in a form object', () => {
148 expect(service.buildFormStructure(modelField, customField, model)).toEqual(formObject);
149 });
150 });
151
152 describe('when model field is an empty array', () => {
153 let empty_modelField = {
154 // 5: {}
155 };
156 let empty_customFields = {
157 id: {
158 label: 'Id',
159 type: 'number'
160 },
161 name: {
162 label: 'Name',
163 type: 'text'
164 },
165 mail: {
166 label: 'Mail',
167 type: 'email'
168 },
169 active: {
170 label: 'Active',
171 type: 'boolean'
172 },
173 created: {
174 label: 'Created',
175 type: 'date'
176 },
177 custom: {
178 label: 'Custom Label',
179 type: 'number'
180 }
181 };
182
183 let empty_formObject = {
184 id: {
185 label: 'Id:',
186 type: 'number',
187 validators: {}
188 },
189 name: {
190 label: 'Name:',
191 type: 'text',
192 validators: {}
193 },
194 mail: {
195 label: 'Mail:',
196 type: 'email',
197 validators: {}
198 },
199 active: {
200 label: 'Active:',
201 type: 'boolean',
202 validators: {}
203 },
204 created: {
205 label: 'Created:',
206 type: 'date',
207 validators: {}
208 },
209 custom: {
210 label: 'Custom Label:',
211 type: 'number',
212 validators: {}
213 }
214 };
215
216 let empty_model = {5: 'Nan'}
217
218 it('should create a form object', () => {
219 let res = service.buildFormStructure(empty_modelField, empty_customFields, empty_model)
220 expect(res.id).toEqual(empty_formObject.id);
221 expect(res.name).toEqual(empty_formObject.name);
222 expect(res.mail).toEqual(empty_formObject.mail);
223 expect(res.active).toEqual(empty_formObject.active);
224 expect(res.created).toEqual(empty_formObject.created);
225 expect(res.custom).toEqual(empty_formObject.custom);
226 expect(res).toEqual(empty_formObject);
227 });
228 });
229 });
230 });
231})();