blob: e985cd63d63578e0d5b3b3d9d362efc82fa655db [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 Scandolob9fb6252016-05-26 15:09:55 -070017 if(!angular.isDefined(element)){
18 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;
23 }
24
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
62 it('should throw an error if no field model is passed', inject(($compile, $rootScope) => {
63 function errorFunctionWrapper(){
64 // setup the parent scope
65 scope = $rootScope.$new();
66 scope.name = 'label';
67 scope.field = {
68 label: 'Label',
69 type: 'number',
70 validators: {}
71 };
Matteo Scandolob9fb6252016-05-26 15:09:55 -070072 compileElement(angular.element('<xos-field name="name" field="field"></xos-field>'));
Matteo Scandolo974c0e42016-05-25 16:02:16 -070073 }
74 expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide an ng-model'));
75 }));
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070076
77 describe('when a text input is passed', () => {
78 beforeEach(() => {
79 scope = rootScope.$new();
80 scope.name = 'label';
81 scope.field = {
82 label: 'Label',
83 type: 'text',
84 validators: {}
85 };
86 scope.ngModel = 'label';
87 compileElement();
88 });
89
90 it('should print a text field', () => {
91 expect($(element).find('[name="label"]')).toHaveAttr('type', 'text');
92 });
93 });
94
arpiagariu47cd6892016-06-03 16:41:39 -070095
96
97
98 describe('when a option is selected in dropdown', () => {
99 beforeEach(() => {
100 scope = rootScope.$new();
101 scope.name = 'label';
102 scope.field = {
103 label: 'Label',
104 type: 'select',
105 validators: {},
106 options:[{
107 id:0,
108 label:"---Site---"
109 },{
110 id:1,
111 label:"---Site1---"
112 }]
113 };
114 scope.ngModel = 'label';
115 compileElement();
116 });
117
118 it('No of select elements', () => {
119 expect($(element).find('select').children('option').length).toEqual(3);
120 });
121
122 it('should show a selected value', () => {
123 var elem = angular.element($(element).find('select').children('option')[1]);
124 expect(elem.text()).toEqual('---Site---');
125 });
126 });
127
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700128 describe('when a number input is passed', () => {
129 beforeEach(() => {
130 scope = rootScope.$new();
131 scope.name = 'label';
132 scope.field = {
133 label: 'Label',
134 type: 'number',
135 validators: {}
136 };
137 scope.ngModel = 10;
138 compileElement();
139 });
140
141 it('should print a number field', () => {
142 expect($(element).find('[name="label"]')).toHaveAttr('type', 'number');
143 });
144 });
145
146 describe('when a boolean input is passed', () => {
147 beforeEach(() => {
148 scope = rootScope.$new();
149 scope.name = 'label';
150 scope.field = {
151 label: 'Label',
152 type: 'boolean',
153 validators: {}
154 };
155 scope.ngModel = true;
156 compileElement();
157 });
158
159 let setFalse, setTrue;
160
161 beforeEach(() => {
162 setFalse= $(element).find('.boolean-field > button:first-child');
163 setTrue = $(element).find('.boolean-field > button:last-child');
164 });
165
166 it('should print two buttons', () => {
167 expect($(element).find('.boolean-field > button').length).toEqual(2)
168 });
169
170 it('should change value to false', () => {
171 expect(isolatedScope.ngModel).toEqual(true);
172 setFalse.click()
173 expect(isolatedScope.ngModel).toEqual(false);
174 });
175
176 it('should change value to true', () => {
177 isolatedScope.ngModel = false;
178 scope.$apply();
179 expect(isolatedScope.ngModel).toEqual(false);
180 setTrue.click()
181 expect(isolatedScope.ngModel).toEqual(true);
182 });
183 });
184
185 describe('when an object input is passed', () => {
186 beforeEach(() => {
187 scope = rootScope.$new();
188 scope.name = 'label';
189 scope.field = {
190 label: 'Label',
191 type: 'object',
192 validators: {}
193 };
194 scope.ngModel = {
195 baz: true,
196 foo: 'bar',
197 foz: 1,
198 };
199 compileElement();
200 });
201
202 it('should print a panel to contain object property field', () => {
203 expect($(element).find('.panel.object-field')).toExist()
204 });
205
206 it('should print the right input type for each property', () => {
207 expect($(element).find('input').length).toBe(2);
208 expect($(element).find('.boolean-field > button').length).toEqual(2);
209 });
210
Matteo Scandolob9fb6252016-05-26 15:09:55 -0700211 it('should format labels', () => {
212 expect($(element).find('input[name="foo"]').parent().find('label').text()).toBe('Foo:');
213 });
214
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700215 describe('and the model is empty', () => {
216 beforeEach(() => {
217 scope.ngModel = {
218 };
219 compileElement();
220 });
221
222 it('should not print the panel', () => {
Matteo Scandolob9fb6252016-05-26 15:09:55 -0700223 // console.log($(element).find('.panel.object-field'));
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700224 expect($(element).find('.panel.object-field')).not.toExist()
225 });
226 });
227 });
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700228 });
229 });
230})();