blob: fbf347a5441fff4ecccd848f2a449d9df906fa75 [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
10 let element, scope, isolatedScope, rootScope, compile;
Matteo Scandolo2aca9ab2016-05-26 15:09:55 -070011 const compileElement = (el) => {
12 element = el;
Matteo Scandolo741d6b42016-05-25 16:02:16 -070013
14 if(!scope){
15 scope = rootScope.$new();
16 }
Matteo Scandolob2a1a182016-06-15 14:53:33 -070017 if(angular.isUndefined(element)){
Matteo Scandolo2aca9ab2016-05-26 15:09:55 -070018 element = angular.element('<xos-field name="name" field="field" ng-model="ngModel"></xos-field>');
19 }
Matteo Scandolo741d6b42016-05-25 16:02:16 -070020 compile(element)(scope);
21 scope.$digest();
22 isolatedScope = element.isolateScope().vm;
Matteo Scandolob2a1a182016-06-15 14:53:33 -070023 };
Matteo Scandolo741d6b42016-05-25 16:02:16 -070024
25 describe('The xos.helper module', function(){
26
27 describe('The xosField component', () => {
28
29 beforeEach(module('xos.helpers'));
30
31 beforeEach(inject(function ($compile, $rootScope) {
32 compile = $compile;
33 rootScope = $rootScope;
34 }));
35
36 it('should throw an error if no name is passed', inject(($compile, $rootScope) => {
37 function errorFunctionWrapper(){
38 // setup the parent scope
39 scope = $rootScope.$new();
40 scope.field = {
41 label: 'Label',
42 type: 'number',
43 validators: {}
44 };
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070045 scope.ngModel = 1;
Matteo Scandolo741d6b42016-05-25 16:02:16 -070046 compileElement();
47 }
48 expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide a field name'));
49 }));
50
51 it('should throw an error if no field definition is passed', inject(($compile, $rootScope) => {
52 function errorFunctionWrapper(){
53 // setup the parent scope
54 scope = $rootScope.$new();
55 scope.name = 'label';
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070056 scope.ngModel = 1;
Matteo Scandolo741d6b42016-05-25 16:02:16 -070057 compileElement();
58 }
59 expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide a field definition'));
60 }));
61
Matteo Scandoloe59cd4c2016-06-08 14:27:30 -070062 it('should throw an error if no field type is passed', inject(($compile, $rootScope) => {
63 function errorFunctionWrapper(){
64 // setup the parent scope
65 scope = $rootScope.$new();
66 scope.name = 'label';
67 scope.ngModel = 1;
68 scope.field = {label: 'Label:'}
69 compileElement();
70 }
71 expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide a type in the field definition'));
72 }));
73
Matteo Scandolo741d6b42016-05-25 16:02:16 -070074 it('should throw an error if no field model is passed', inject(($compile, $rootScope) => {
75 function errorFunctionWrapper(){
76 // setup the parent scope
77 scope = $rootScope.$new();
78 scope.name = 'label';
79 scope.field = {
80 label: 'Label',
81 type: 'number',
82 validators: {}
83 };
Matteo Scandolo2aca9ab2016-05-26 15:09:55 -070084 compileElement(angular.element('<xos-field name="name" field="field"></xos-field>'));
Matteo Scandolo741d6b42016-05-25 16:02:16 -070085 }
86 expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide an ng-model'));
87 }));
Matteo Scandolodf23cdb2016-05-25 17:37:37 -070088
89 describe('when a text input is passed', () => {
90 beforeEach(() => {
91 scope = rootScope.$new();
92 scope.name = 'label';
93 scope.field = {
94 label: 'Label',
95 type: 'text',
96 validators: {}
97 };
98 scope.ngModel = 'label';
99 compileElement();
100 });
101
102 it('should print a text field', () => {
103 expect($(element).find('[name="label"]')).toHaveAttr('type', 'text');
104 });
105 });
106
arpiagariu7c1975d2016-06-03 16:41:39 -0700107 describe('when a option is selected in dropdown', () => {
108 beforeEach(() => {
109 scope = rootScope.$new();
110 scope.name = 'label';
111 scope.field = {
112 label: 'Label',
113 type: 'select',
114 validators: {},
Matteo Scandolobe136072016-06-03 16:58:43 -0700115 options: [
116 {
117 id: 0,
118 label: '---Site---'
119 },
120 {
121 id: 1,
122 label: '---Site1---'
123 }
124 ]
arpiagariu7c1975d2016-06-03 16:41:39 -0700125 };
Matteo Scandolob2a1a182016-06-15 14:53:33 -0700126 scope.ngModel = 0;
arpiagariu7c1975d2016-06-03 16:41:39 -0700127 compileElement();
128 });
129
130 it('No of select elements', () => {
Matteo Scandolob2a1a182016-06-15 14:53:33 -0700131 expect($(element).find('select').children('option').length).toEqual(2);
arpiagariu7c1975d2016-06-03 16:41:39 -0700132 });
133
Matteo Scandolob2a1a182016-06-15 14:53:33 -0700134 it('should show the selected value', () => {
135 var elem = angular.element($(element).find('select').children('option')[0]);
arpiagariu7c1975d2016-06-03 16:41:39 -0700136 expect(elem.text()).toEqual('---Site---');
Matteo Scandolob2a1a182016-06-15 14:53:33 -0700137 expect(elem).toHaveAttr('selected');
arpiagariu7c1975d2016-06-03 16:41:39 -0700138 });
139 });
140
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700141 describe('when a number input is passed', () => {
142 beforeEach(() => {
143 scope = rootScope.$new();
144 scope.name = 'label';
145 scope.field = {
146 label: 'Label',
147 type: 'number',
148 validators: {}
149 };
150 scope.ngModel = 10;
151 compileElement();
152 });
153
154 it('should print a number field', () => {
155 expect($(element).find('[name="label"]')).toHaveAttr('type', 'number');
156 });
157 });
158
Matteo Scandolo85aef772016-06-15 15:58:49 -0700159 describe('when a boolean input is passed', () => {
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700160 beforeEach(() => {
161 scope = rootScope.$new();
162 scope.name = 'label';
163 scope.field = {
164 label: 'Label',
165 type: 'boolean',
166 validators: {}
167 };
168 scope.ngModel = true;
169 compileElement();
170 });
171
172 let setFalse, setTrue;
173
174 beforeEach(() => {
Matteo Scandolo85aef772016-06-15 15:58:49 -0700175 setFalse= $(element).find('.boolean-field > a:first-child');
176 setTrue = $(element).find('.boolean-field > a:last-child');
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700177 });
178
179 it('should print two buttons', () => {
Matteo Scandolo85aef772016-06-15 15:58:49 -0700180 expect($(element).find('.boolean-field > a').length).toEqual(2)
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700181 });
182
Matteo Scandolo85aef772016-06-15 15:58:49 -0700183 // NOTE .click is not working anymore
184 xit('should change value to false', () => {
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700185 expect(isolatedScope.ngModel).toEqual(true);
186 setFalse.click()
187 expect(isolatedScope.ngModel).toEqual(false);
188 });
189
Matteo Scandolo85aef772016-06-15 15:58:49 -0700190 xit('should change value to true', () => {
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700191 isolatedScope.ngModel = false;
192 scope.$apply();
193 expect(isolatedScope.ngModel).toEqual(false);
194 setTrue.click()
195 expect(isolatedScope.ngModel).toEqual(true);
196 });
197 });
198
199 describe('when an object input is passed', () => {
200 beforeEach(() => {
201 scope = rootScope.$new();
202 scope.name = 'label';
203 scope.field = {
204 label: 'Label',
205 type: 'object',
206 validators: {}
207 };
208 scope.ngModel = {
209 baz: true,
210 foo: 'bar',
211 foz: 1,
212 };
213 compileElement();
214 });
215
216 it('should print a panel to contain object property field', () => {
217 expect($(element).find('.panel.object-field')).toExist()
218 });
219
220 it('should print the right input type for each property', () => {
221 expect($(element).find('input').length).toBe(2);
arpiagariue81ed6f2016-06-15 09:53:37 -0700222 expect($(element).find('.boolean-field > a').length).toEqual(2);
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700223 });
224
Matteo Scandolo2aca9ab2016-05-26 15:09:55 -0700225 it('should format labels', () => {
226 expect($(element).find('input[name="foo"]').parent().find('label').text()).toBe('Foo:');
227 });
228
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700229 describe('and the model is empty', () => {
230 beforeEach(() => {
231 scope.ngModel = {
232 };
233 compileElement();
234 });
235
236 it('should not print the panel', () => {
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700237 expect($(element).find('.panel.object-field')).not.toExist()
238 });
Matteo Scandoloe59cd4c2016-06-08 14:27:30 -0700239
240 describe('but field is configured', () => {
241 beforeEach(() => {
242 scope.field.properties = {
243 foo: {
244 label: 'FooLabel:',
245 type: 'string',
246 validators: {
247 required: true
248 }
249 },
250 bar: {
251 type: 'number'
252 }
253 };
254 compileElement();
255 });
256 it('should render panel and configured fields', () => {
257 expect($(element).find('.panel.object-field')).toExist();
258 expect($(element).find('input[name="foo"]').parent().find('label').text()).toBe('FooLabel:');
259 expect($(element).find('input[name="foo"]')).toHaveAttr('type', 'string');
260 expect($(element).find('input[name="foo"]')).toHaveAttr('required');
261
262 expect($(element).find('input[name="bar"]').parent().find('label').text()).toBe('Bar:');
263 expect($(element).find('input[name="bar"]')).toHaveAttr('type', 'number');
264
265 });
266 });
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700267 });
268 });
Matteo Scandolo85aef772016-06-15 15:58:49 -0700269
270 // NOTE not sure why this tests are failing
271 describe('when validation options are passed', () => {
272 let input;
273 describe('given a a text field', () => {
274 beforeEach(() => {
275 scope.field = {
276 label: 'Label',
277 type: 'text',
278 validators: {
279 minlength: 10,
280 maxlength: 15,
281 required: true
282 }
283 };
284
285 scope.$digest();
286 input = $(element).find('input');
287 });
288
289 it('should validate required', () => {
290 scope.ngModel= null;
291 scope.$digest();
292 expect(input).toHaveClass('ng-invalid-required');
293
294 scope.ngModel= 'not too short';
295 scope.$digest();
296 expect(input).not.toHaveClass('ng-invalid-required');
297 expect(input).not.toHaveClass('ng-invalid');
298 });
299
300 it('should validate minlength', () => {
301 scope.ngModel= 'short';
302 scope.$digest();
303 expect(input).toHaveClass('ng-invalid-minlength');
304
305 scope.ngModel= 'not too short';
306 scope.$digest();
307 expect(input).not.toHaveClass('ng-invalid-minlength');
308 expect(input).not.toHaveClass('ng-invalid');
309 });
310
311 it('should validate maxlength', () => {
312 scope.ngModel= 'this is definitely too long!!';
313 scope.$digest();
314 expect(input).toHaveClass('ng-invalid-maxlength');
315
316 scope.ngModel= 'not too short';
317 scope.$digest();
318 expect(input).not.toHaveClass('ng-invalid-maxlength');
319 expect(input).not.toHaveClass('ng-invalid');
320 });
321 });
322
323 });
324
Matteo Scandolo741d6b42016-05-25 16:02:16 -0700325 });
326 });
327})();