blob: 8a02d4d3212e66d29e3d8bde611380b25b7ebf5d [file] [log] [blame]
Matteo Scandolo741d6b42016-05-25 16:02:16 -07001/**
2 * © OpenCORD
3 *
4 * Created by teone on 5/25/16.
5 */
6
7(function () {
8 'use strict';
9
Matteo Scandolo741d6b42016-05-25 16:02:16 -070010 describe('The xos.helper module', function(){
11
12 describe('The xosField component', () => {
Matteo Scandolo019bd0b2016-06-15 18:03:43 -070013 let element, scope, isolatedScope, rootScope, compile;
14 const compileElement = (el) => {
15 element = el;
16
17 if(!scope){
18 scope = rootScope.$new();
19 }
20 if(angular.isUndefined(element)){
21 element = angular.element('<xos-field name="name" field="field" ng-model="ngModel"></xos-field>');
22 }
23 compile(element)(scope);
24 scope.$digest();
25 isolatedScope = element.isolateScope().vm;
26 };
Matteo Scandolo741d6b42016-05-25 16:02:16 -070027
28 beforeEach(module('xos.helpers'));
29
30 beforeEach(inject(function ($compile, $rootScope) {
31 compile = $compile;
32 rootScope = $rootScope;
33 }));
34
35 it('should throw an error if no name is passed', inject(($compile, $rootScope) => {
36 function errorFunctionWrapper(){
37 // setup the parent scope
38 scope = $rootScope.$new();
39 scope.field = {
40 label: 'Label',
41 type: 'number',
42 validators: {}
43 };
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070044 scope.ngModel = 1;
Matteo Scandolo741d6b42016-05-25 16:02:16 -070045 compileElement();
46 }
47 expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide a field name'));
48 }));
49
50 it('should throw an error if no field definition is passed', inject(($compile, $rootScope) => {
51 function errorFunctionWrapper(){
52 // setup the parent scope
53 scope = $rootScope.$new();
54 scope.name = 'label';
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070055 scope.ngModel = 1;
Matteo Scandolo741d6b42016-05-25 16:02:16 -070056 compileElement();
57 }
58 expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide a field definition'));
59 }));
60
Matteo Scandoloe59cd4c2016-06-08 14:27:30 -070061 it('should throw an error if no field type is passed', inject(($compile, $rootScope) => {
62 function errorFunctionWrapper(){
63 // setup the parent scope
64 scope = $rootScope.$new();
65 scope.name = 'label';
66 scope.ngModel = 1;
67 scope.field = {label: 'Label:'}
68 compileElement();
69 }
70 expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide a type in the field definition'));
71 }));
72
Matteo Scandolo741d6b42016-05-25 16:02:16 -070073 it('should throw an error if no field model is passed', inject(($compile, $rootScope) => {
74 function errorFunctionWrapper(){
75 // setup the parent scope
76 scope = $rootScope.$new();
77 scope.name = 'label';
78 scope.field = {
79 label: 'Label',
80 type: 'number',
81 validators: {}
82 };
Matteo Scandolo2aca9ab2016-05-26 15:09:55 -070083 compileElement(angular.element('<xos-field name="name" field="field"></xos-field>'));
Matteo Scandolo741d6b42016-05-25 16:02:16 -070084 }
85 expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide an ng-model'));
86 }));
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070087
88 describe('when a text input is passed', () => {
89 beforeEach(() => {
90 scope = rootScope.$new();
91 scope.name = 'label';
92 scope.field = {
93 label: 'Label',
94 type: 'text',
Matteo Scandolo019bd0b2016-06-15 18:03:43 -070095 validators: {
96 custom: 'fake'
97 }
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070098 };
99 scope.ngModel = 'label';
100 compileElement();
101 });
102
103 it('should print a text field', () => {
104 expect($(element).find('[name="label"]')).toHaveAttr('type', 'text');
105 });
Matteo Scandolo019bd0b2016-06-15 18:03:43 -0700106
107 it('should attach the custom validator directive', () => {
108 let input = $(element).find('[name="label"]');
109 expect(input).toHaveAttr('xos-custom-validator');
110 expect(input).toHaveAttr('custom-validator', 'vm.field.validators.custom || null');
111 });
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700112 });
113
arpiagariu7c1975d2016-06-03 16:41:39 -0700114 describe('when a option is selected in dropdown', () => {
115 beforeEach(() => {
116 scope = rootScope.$new();
117 scope.name = 'label';
118 scope.field = {
119 label: 'Label',
120 type: 'select',
121 validators: {},
Matteo Scandolobe136072016-06-03 16:58:43 -0700122 options: [
123 {
124 id: 0,
125 label: '---Site---'
126 },
127 {
128 id: 1,
129 label: '---Site1---'
130 }
131 ]
arpiagariu7c1975d2016-06-03 16:41:39 -0700132 };
Matteo Scandolob2a1a182016-06-15 14:53:33 -0700133 scope.ngModel = 0;
arpiagariu7c1975d2016-06-03 16:41:39 -0700134 compileElement();
135 });
136
137 it('No of select elements', () => {
Matteo Scandolob2a1a182016-06-15 14:53:33 -0700138 expect($(element).find('select').children('option').length).toEqual(2);
arpiagariu7c1975d2016-06-03 16:41:39 -0700139 });
140
Matteo Scandolob2a1a182016-06-15 14:53:33 -0700141 it('should show the selected value', () => {
142 var elem = angular.element($(element).find('select').children('option')[0]);
arpiagariu7c1975d2016-06-03 16:41:39 -0700143 expect(elem.text()).toEqual('---Site---');
Matteo Scandolob2a1a182016-06-15 14:53:33 -0700144 expect(elem).toHaveAttr('selected');
arpiagariu7c1975d2016-06-03 16:41:39 -0700145 });
146 });
147
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700148 describe('when a number input is passed', () => {
149 beforeEach(() => {
150 scope = rootScope.$new();
151 scope.name = 'label';
152 scope.field = {
153 label: 'Label',
154 type: 'number',
155 validators: {}
156 };
157 scope.ngModel = 10;
158 compileElement();
159 });
160
161 it('should print a number field', () => {
162 expect($(element).find('[name="label"]')).toHaveAttr('type', 'number');
163 });
164 });
165
Matteo Scandolo85aef772016-06-15 15:58:49 -0700166 describe('when a boolean input is passed', () => {
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700167 beforeEach(() => {
168 scope = rootScope.$new();
169 scope.name = 'label';
170 scope.field = {
171 label: 'Label',
172 type: 'boolean',
173 validators: {}
174 };
175 scope.ngModel = true;
176 compileElement();
177 });
178
179 let setFalse, setTrue;
180
181 beforeEach(() => {
Matteo Scandolo85aef772016-06-15 15:58:49 -0700182 setFalse= $(element).find('.boolean-field > a:first-child');
183 setTrue = $(element).find('.boolean-field > a:last-child');
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700184 });
185
186 it('should print two buttons', () => {
Matteo Scandolo85aef772016-06-15 15:58:49 -0700187 expect($(element).find('.boolean-field > a').length).toEqual(2)
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700188 });
189
Matteo Scandolo79e86d62016-06-15 16:42:12 -0700190 it('should change value to false', () => {
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700191 expect(isolatedScope.ngModel).toEqual(true);
Matteo Scandolo79e86d62016-06-15 16:42:12 -0700192 clickElement(setFalse[0]);
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700193 expect(isolatedScope.ngModel).toEqual(false);
194 });
195
Matteo Scandolo79e86d62016-06-15 16:42:12 -0700196 it('should change value to true', () => {
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700197 isolatedScope.ngModel = false;
198 scope.$apply();
199 expect(isolatedScope.ngModel).toEqual(false);
Matteo Scandolo79e86d62016-06-15 16:42:12 -0700200 clickElement(setTrue[0]);
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700201 expect(isolatedScope.ngModel).toEqual(true);
202 });
203 });
204
205 describe('when an object input is passed', () => {
206 beforeEach(() => {
207 scope = rootScope.$new();
208 scope.name = 'label';
209 scope.field = {
210 label: 'Label',
211 type: 'object',
212 validators: {}
213 };
214 scope.ngModel = {
215 baz: true,
216 foo: 'bar',
217 foz: 1,
218 };
219 compileElement();
220 });
221
222 it('should print a panel to contain object property field', () => {
223 expect($(element).find('.panel.object-field')).toExist()
224 });
225
226 it('should print the right input type for each property', () => {
227 expect($(element).find('input').length).toBe(2);
arpiagariue81ed6f2016-06-15 09:53:37 -0700228 expect($(element).find('.boolean-field > a').length).toEqual(2);
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700229 });
230
Matteo Scandolo2aca9ab2016-05-26 15:09:55 -0700231 it('should format labels', () => {
232 expect($(element).find('input[name="foo"]').parent().find('label').text()).toBe('Foo:');
233 });
234
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700235 describe('and the model is empty', () => {
236 beforeEach(() => {
237 scope.ngModel = {
238 };
239 compileElement();
240 });
241
242 it('should not print the panel', () => {
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700243 expect($(element).find('.panel.object-field')).not.toExist()
244 });
Matteo Scandoloe59cd4c2016-06-08 14:27:30 -0700245
246 describe('but field is configured', () => {
247 beforeEach(() => {
248 scope.field.properties = {
249 foo: {
250 label: 'FooLabel:',
251 type: 'string',
252 validators: {
253 required: true
254 }
255 },
256 bar: {
257 type: 'number'
258 }
259 };
260 compileElement();
261 });
262 it('should render panel and configured fields', () => {
263 expect($(element).find('.panel.object-field')).toExist();
264 expect($(element).find('input[name="foo"]').parent().find('label').text()).toBe('FooLabel:');
265 expect($(element).find('input[name="foo"]')).toHaveAttr('type', 'string');
266 expect($(element).find('input[name="foo"]')).toHaveAttr('required');
267
268 expect($(element).find('input[name="bar"]').parent().find('label').text()).toBe('Bar:');
269 expect($(element).find('input[name="bar"]')).toHaveAttr('type', 'number');
270
271 });
272 });
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700273 });
274 });
Matteo Scandolo85aef772016-06-15 15:58:49 -0700275
Matteo Scandolo85aef772016-06-15 15:58:49 -0700276 describe('when validation options are passed', () => {
277 let input;
278 describe('given a a text field', () => {
279 beforeEach(() => {
280 scope.field = {
281 label: 'Label',
282 type: 'text',
283 validators: {
284 minlength: 10,
285 maxlength: 15,
286 required: true
287 }
288 };
289
290 scope.$digest();
291 input = $(element).find('input');
292 });
293
294 it('should validate required', () => {
295 scope.ngModel= null;
296 scope.$digest();
297 expect(input).toHaveClass('ng-invalid-required');
298
299 scope.ngModel= 'not too short';
300 scope.$digest();
301 expect(input).not.toHaveClass('ng-invalid-required');
302 expect(input).not.toHaveClass('ng-invalid');
303 });
304
305 it('should validate minlength', () => {
306 scope.ngModel= 'short';
307 scope.$digest();
308 expect(input).toHaveClass('ng-invalid-minlength');
309
310 scope.ngModel= 'not too short';
311 scope.$digest();
312 expect(input).not.toHaveClass('ng-invalid-minlength');
313 expect(input).not.toHaveClass('ng-invalid');
314 });
315
316 it('should validate maxlength', () => {
317 scope.ngModel= 'this is definitely too long!!';
318 scope.$digest();
319 expect(input).toHaveClass('ng-invalid-maxlength');
320
321 scope.ngModel= 'not too short';
322 scope.$digest();
323 expect(input).not.toHaveClass('ng-invalid-maxlength');
324 expect(input).not.toHaveClass('ng-invalid');
325 });
326 });
Matteo Scandolo85aef772016-06-15 15:58:49 -0700327 });
Matteo Scandolo741d6b42016-05-25 16:02:16 -0700328 });
329 });
330})();