blob: 8b32585426ae5cff67b8546ee184970d2f577d76 [file] [log] [blame]
Matteo Scandolo974c0e42016-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 Scandolob9fb6252016-05-26 15:09:55 -070011 const compileElement = (el) => {
12 element = el;
Matteo Scandolo974c0e42016-05-25 16:02:16 -070013
14 if(!scope){
15 scope = rootScope.$new();
16 }
Matteo Scandolob585d382016-06-15 14:53:33 -070017 if(angular.isUndefined(element)){
Matteo Scandolob9fb6252016-05-26 15:09:55 -070018 element = angular.element('<xos-field name="name" field="field" ng-model="ngModel"></xos-field>');
19 }
Matteo Scandolo974c0e42016-05-25 16:02:16 -070020 compile(element)(scope);
21 scope.$digest();
22 isolatedScope = element.isolateScope().vm;
Matteo Scandolob585d382016-06-15 14:53:33 -070023 };
Matteo Scandolo974c0e42016-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 Scandolo03d8b8e2016-05-25 17:37:37 -070045 scope.ngModel = 1;
Matteo Scandolo974c0e42016-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 Scandolo03d8b8e2016-05-25 17:37:37 -070056 scope.ngModel = 1;
Matteo Scandolo974c0e42016-05-25 16:02:16 -070057 compileElement();
58 }
59 expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide a field definition'));
60 }));
61
Matteo Scandolobd4057e2016-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 Scandolo974c0e42016-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 Scandolob9fb6252016-05-26 15:09:55 -070084 compileElement(angular.element('<xos-field name="name" field="field"></xos-field>'));
Matteo Scandolo974c0e42016-05-25 16:02:16 -070085 }
86 expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide an ng-model'));
87 }));
Matteo Scandolo03d8b8e2016-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
arpiagariu47cd6892016-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 Scandolob6ca3012016-06-03 16:58:43 -0700115 options: [
116 {
117 id: 0,
118 label: '---Site---'
119 },
120 {
121 id: 1,
122 label: '---Site1---'
123 }
124 ]
arpiagariu47cd6892016-06-03 16:41:39 -0700125 };
Matteo Scandolob585d382016-06-15 14:53:33 -0700126 scope.ngModel = 0;
arpiagariu47cd6892016-06-03 16:41:39 -0700127 compileElement();
128 });
129
130 it('No of select elements', () => {
Matteo Scandolob585d382016-06-15 14:53:33 -0700131 expect($(element).find('select').children('option').length).toEqual(2);
arpiagariu47cd6892016-06-03 16:41:39 -0700132 });
133
Matteo Scandolob585d382016-06-15 14:53:33 -0700134 it('should show the selected value', () => {
135 var elem = angular.element($(element).find('select').children('option')[0]);
arpiagariu47cd6892016-06-03 16:41:39 -0700136 expect(elem.text()).toEqual('---Site---');
Matteo Scandolob585d382016-06-15 14:53:33 -0700137 expect(elem).toHaveAttr('selected');
arpiagariu47cd6892016-06-03 16:41:39 -0700138 });
139 });
140
Matteo Scandolo03d8b8e2016-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 Scandolo160e6ce2016-06-15 15:58:49 -0700159 describe('when a boolean input is passed', () => {
Matteo Scandolo03d8b8e2016-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 Scandolo160e6ce2016-06-15 15:58:49 -0700175 setFalse= $(element).find('.boolean-field > a:first-child');
176 setTrue = $(element).find('.boolean-field > a:last-child');
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700177 });
178
179 it('should print two buttons', () => {
Matteo Scandolo160e6ce2016-06-15 15:58:49 -0700180 expect($(element).find('.boolean-field > a').length).toEqual(2)
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700181 });
182
Matteo Scandolo3dfbced2016-06-15 16:42:12 -0700183 it('should change value to false', () => {
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700184 expect(isolatedScope.ngModel).toEqual(true);
Matteo Scandolo3dfbced2016-06-15 16:42:12 -0700185 clickElement(setFalse[0]);
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700186 expect(isolatedScope.ngModel).toEqual(false);
187 });
188
Matteo Scandolo3dfbced2016-06-15 16:42:12 -0700189 it('should change value to true', () => {
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700190 isolatedScope.ngModel = false;
191 scope.$apply();
192 expect(isolatedScope.ngModel).toEqual(false);
Matteo Scandolo3dfbced2016-06-15 16:42:12 -0700193 clickElement(setTrue[0]);
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700194 expect(isolatedScope.ngModel).toEqual(true);
195 });
196 });
197
198 describe('when an object input is passed', () => {
199 beforeEach(() => {
200 scope = rootScope.$new();
201 scope.name = 'label';
202 scope.field = {
203 label: 'Label',
204 type: 'object',
205 validators: {}
206 };
207 scope.ngModel = {
208 baz: true,
209 foo: 'bar',
210 foz: 1,
211 };
212 compileElement();
213 });
214
215 it('should print a panel to contain object property field', () => {
216 expect($(element).find('.panel.object-field')).toExist()
217 });
218
219 it('should print the right input type for each property', () => {
220 expect($(element).find('input').length).toBe(2);
arpiagariu4cf107b2016-06-15 09:53:37 -0700221 expect($(element).find('.boolean-field > a').length).toEqual(2);
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700222 });
223
Matteo Scandolob9fb6252016-05-26 15:09:55 -0700224 it('should format labels', () => {
225 expect($(element).find('input[name="foo"]').parent().find('label').text()).toBe('Foo:');
226 });
227
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700228 describe('and the model is empty', () => {
229 beforeEach(() => {
230 scope.ngModel = {
231 };
232 compileElement();
233 });
234
235 it('should not print the panel', () => {
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700236 expect($(element).find('.panel.object-field')).not.toExist()
237 });
Matteo Scandolobd4057e2016-06-08 14:27:30 -0700238
239 describe('but field is configured', () => {
240 beforeEach(() => {
241 scope.field.properties = {
242 foo: {
243 label: 'FooLabel:',
244 type: 'string',
245 validators: {
246 required: true
247 }
248 },
249 bar: {
250 type: 'number'
251 }
252 };
253 compileElement();
254 });
255 it('should render panel and configured fields', () => {
256 expect($(element).find('.panel.object-field')).toExist();
257 expect($(element).find('input[name="foo"]').parent().find('label').text()).toBe('FooLabel:');
258 expect($(element).find('input[name="foo"]')).toHaveAttr('type', 'string');
259 expect($(element).find('input[name="foo"]')).toHaveAttr('required');
260
261 expect($(element).find('input[name="bar"]').parent().find('label').text()).toBe('Bar:');
262 expect($(element).find('input[name="bar"]')).toHaveAttr('type', 'number');
263
264 });
265 });
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700266 });
267 });
Matteo Scandolo160e6ce2016-06-15 15:58:49 -0700268
269 // NOTE not sure why this tests are failing
270 describe('when validation options are passed', () => {
271 let input;
272 describe('given a a text field', () => {
273 beforeEach(() => {
274 scope.field = {
275 label: 'Label',
276 type: 'text',
277 validators: {
278 minlength: 10,
279 maxlength: 15,
280 required: true
281 }
282 };
283
284 scope.$digest();
285 input = $(element).find('input');
286 });
287
288 it('should validate required', () => {
289 scope.ngModel= null;
290 scope.$digest();
291 expect(input).toHaveClass('ng-invalid-required');
292
293 scope.ngModel= 'not too short';
294 scope.$digest();
295 expect(input).not.toHaveClass('ng-invalid-required');
296 expect(input).not.toHaveClass('ng-invalid');
297 });
298
299 it('should validate minlength', () => {
300 scope.ngModel= 'short';
301 scope.$digest();
302 expect(input).toHaveClass('ng-invalid-minlength');
303
304 scope.ngModel= 'not too short';
305 scope.$digest();
306 expect(input).not.toHaveClass('ng-invalid-minlength');
307 expect(input).not.toHaveClass('ng-invalid');
308 });
309
310 it('should validate maxlength', () => {
311 scope.ngModel= 'this is definitely too long!!';
312 scope.$digest();
313 expect(input).toHaveClass('ng-invalid-maxlength');
314
315 scope.ngModel= 'not too short';
316 scope.$digest();
317 expect(input).not.toHaveClass('ng-invalid-maxlength');
318 expect(input).not.toHaveClass('ng-invalid');
319 });
320 });
Matteo Scandolo160e6ce2016-06-15 15:58:49 -0700321 });
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700322 });
323 });
324})();