blob: f933a01558cb08cefd8e29830b1c9497eaf36f8d [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
95 describe('when a number input is passed', () => {
96 beforeEach(() => {
97 scope = rootScope.$new();
98 scope.name = 'label';
99 scope.field = {
100 label: 'Label',
101 type: 'number',
102 validators: {}
103 };
104 scope.ngModel = 10;
105 compileElement();
106 });
107
108 it('should print a number field', () => {
109 expect($(element).find('[name="label"]')).toHaveAttr('type', 'number');
110 });
111 });
112
113 describe('when a boolean input is passed', () => {
114 beforeEach(() => {
115 scope = rootScope.$new();
116 scope.name = 'label';
117 scope.field = {
118 label: 'Label',
119 type: 'boolean',
120 validators: {}
121 };
122 scope.ngModel = true;
123 compileElement();
124 });
125
126 let setFalse, setTrue;
127
128 beforeEach(() => {
129 setFalse= $(element).find('.boolean-field > button:first-child');
130 setTrue = $(element).find('.boolean-field > button:last-child');
131 });
132
133 it('should print two buttons', () => {
134 expect($(element).find('.boolean-field > button').length).toEqual(2)
135 });
136
137 it('should change value to false', () => {
138 expect(isolatedScope.ngModel).toEqual(true);
139 setFalse.click()
140 expect(isolatedScope.ngModel).toEqual(false);
141 });
142
143 it('should change value to true', () => {
144 isolatedScope.ngModel = false;
145 scope.$apply();
146 expect(isolatedScope.ngModel).toEqual(false);
147 setTrue.click()
148 expect(isolatedScope.ngModel).toEqual(true);
149 });
150 });
151
152 describe('when an object input is passed', () => {
153 beforeEach(() => {
154 scope = rootScope.$new();
155 scope.name = 'label';
156 scope.field = {
157 label: 'Label',
158 type: 'object',
159 validators: {}
160 };
161 scope.ngModel = {
162 baz: true,
163 foo: 'bar',
164 foz: 1,
165 };
166 compileElement();
167 });
168
169 it('should print a panel to contain object property field', () => {
170 expect($(element).find('.panel.object-field')).toExist()
171 });
172
173 it('should print the right input type for each property', () => {
174 expect($(element).find('input').length).toBe(2);
175 expect($(element).find('.boolean-field > button').length).toEqual(2);
176 });
177
Matteo Scandolob9fb6252016-05-26 15:09:55 -0700178 it('should format labels', () => {
179 expect($(element).find('input[name="foo"]').parent().find('label').text()).toBe('Foo:');
180 });
181
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700182 describe('and the model is empty', () => {
183 beforeEach(() => {
184 scope.ngModel = {
185 };
186 compileElement();
187 });
188
189 it('should not print the panel', () => {
Matteo Scandolob9fb6252016-05-26 15:09:55 -0700190 // console.log($(element).find('.panel.object-field'));
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700191 expect($(element).find('.panel.object-field')).not.toExist()
192 });
193 });
194 });
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700195 });
196 });
197})();