blob: f294daa4505e1204341aa8185a0dfbbdfe0f57d6 [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
arpiagariue81ed6f2016-06-15 09:53:37 -0700159 xdescribe('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(() => {
175 setFalse= $(element).find('.boolean-field > button:first-child');
176 setTrue = $(element).find('.boolean-field > button:last-child');
177 });
178
179 it('should print two buttons', () => {
180 expect($(element).find('.boolean-field > button').length).toEqual(2)
181 });
182
183 it('should change value to false', () => {
184 expect(isolatedScope.ngModel).toEqual(true);
185 setFalse.click()
186 expect(isolatedScope.ngModel).toEqual(false);
187 });
188
189 it('should change value to true', () => {
190 isolatedScope.ngModel = false;
191 scope.$apply();
192 expect(isolatedScope.ngModel).toEqual(false);
193 setTrue.click()
194 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);
arpiagariue81ed6f2016-06-15 09:53:37 -0700221 expect($(element).find('.boolean-field > a').length).toEqual(2);
Matteo Scandolodf23cdb2016-05-25 17:37:37 -0700222 });
223
Matteo Scandolo2aca9ab2016-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 Scandolodf23cdb2016-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 Scandolodf23cdb2016-05-25 17:37:37 -0700236 expect($(element).find('.panel.object-field')).not.toExist()
237 });
Matteo Scandoloe59cd4c2016-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 Scandolodf23cdb2016-05-25 17:37:37 -0700266 });
267 });
Matteo Scandolo741d6b42016-05-25 16:02:16 -0700268 });
269 });
270})();