Matteo Scandolo | 974c0e4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 1 | /** |
| 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 Scandolo | b9fb625 | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 11 | const compileElement = (el) => { |
| 12 | element = el; |
Matteo Scandolo | 974c0e4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 13 | |
| 14 | if(!scope){ |
| 15 | scope = rootScope.$new(); |
| 16 | } |
Matteo Scandolo | b585d38 | 2016-06-15 14:53:33 -0700 | [diff] [blame] | 17 | if(angular.isUndefined(element)){ |
Matteo Scandolo | b9fb625 | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 18 | element = angular.element('<xos-field name="name" field="field" ng-model="ngModel"></xos-field>'); |
| 19 | } |
Matteo Scandolo | 974c0e4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 20 | compile(element)(scope); |
| 21 | scope.$digest(); |
| 22 | isolatedScope = element.isolateScope().vm; |
Matteo Scandolo | b585d38 | 2016-06-15 14:53:33 -0700 | [diff] [blame] | 23 | }; |
Matteo Scandolo | 974c0e4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 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 Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 45 | scope.ngModel = 1; |
Matteo Scandolo | 974c0e4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 46 | 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 Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 56 | scope.ngModel = 1; |
Matteo Scandolo | 974c0e4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 57 | compileElement(); |
| 58 | } |
| 59 | expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide a field definition')); |
| 60 | })); |
| 61 | |
Matteo Scandolo | bd4057e | 2016-06-08 14:27:30 -0700 | [diff] [blame] | 62 | 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 Scandolo | 974c0e4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 74 | 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 Scandolo | b9fb625 | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 84 | compileElement(angular.element('<xos-field name="name" field="field"></xos-field>')); |
Matteo Scandolo | 974c0e4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 85 | } |
| 86 | expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide an ng-model')); |
| 87 | })); |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 88 | |
| 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 | |
arpiagariu | 47cd689 | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 107 | 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 Scandolo | b6ca301 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 115 | options: [ |
| 116 | { |
| 117 | id: 0, |
| 118 | label: '---Site---' |
| 119 | }, |
| 120 | { |
| 121 | id: 1, |
| 122 | label: '---Site1---' |
| 123 | } |
| 124 | ] |
arpiagariu | 47cd689 | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 125 | }; |
Matteo Scandolo | b585d38 | 2016-06-15 14:53:33 -0700 | [diff] [blame] | 126 | scope.ngModel = 0; |
arpiagariu | 47cd689 | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 127 | compileElement(); |
| 128 | }); |
| 129 | |
| 130 | it('No of select elements', () => { |
Matteo Scandolo | b585d38 | 2016-06-15 14:53:33 -0700 | [diff] [blame] | 131 | expect($(element).find('select').children('option').length).toEqual(2); |
arpiagariu | 47cd689 | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 132 | }); |
| 133 | |
Matteo Scandolo | b585d38 | 2016-06-15 14:53:33 -0700 | [diff] [blame] | 134 | it('should show the selected value', () => { |
| 135 | var elem = angular.element($(element).find('select').children('option')[0]); |
arpiagariu | 47cd689 | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 136 | expect(elem.text()).toEqual('---Site---'); |
Matteo Scandolo | b585d38 | 2016-06-15 14:53:33 -0700 | [diff] [blame] | 137 | expect(elem).toHaveAttr('selected'); |
arpiagariu | 47cd689 | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 138 | }); |
| 139 | }); |
| 140 | |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 141 | 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 Scandolo | 160e6ce | 2016-06-15 15:58:49 -0700 | [diff] [blame] | 159 | describe('when a boolean input is passed', () => { |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 160 | 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 Scandolo | 160e6ce | 2016-06-15 15:58:49 -0700 | [diff] [blame] | 175 | setFalse= $(element).find('.boolean-field > a:first-child'); |
| 176 | setTrue = $(element).find('.boolean-field > a:last-child'); |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 177 | }); |
| 178 | |
| 179 | it('should print two buttons', () => { |
Matteo Scandolo | 160e6ce | 2016-06-15 15:58:49 -0700 | [diff] [blame] | 180 | expect($(element).find('.boolean-field > a').length).toEqual(2) |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 181 | }); |
| 182 | |
Matteo Scandolo | 3dfbced | 2016-06-15 16:42:12 -0700 | [diff] [blame^] | 183 | it('should change value to false', () => { |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 184 | expect(isolatedScope.ngModel).toEqual(true); |
Matteo Scandolo | 3dfbced | 2016-06-15 16:42:12 -0700 | [diff] [blame^] | 185 | clickElement(setFalse[0]); |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 186 | expect(isolatedScope.ngModel).toEqual(false); |
| 187 | }); |
| 188 | |
Matteo Scandolo | 3dfbced | 2016-06-15 16:42:12 -0700 | [diff] [blame^] | 189 | it('should change value to true', () => { |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 190 | isolatedScope.ngModel = false; |
| 191 | scope.$apply(); |
| 192 | expect(isolatedScope.ngModel).toEqual(false); |
Matteo Scandolo | 3dfbced | 2016-06-15 16:42:12 -0700 | [diff] [blame^] | 193 | clickElement(setTrue[0]); |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 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); |
arpiagariu | 4cf107b | 2016-06-15 09:53:37 -0700 | [diff] [blame] | 221 | expect($(element).find('.boolean-field > a').length).toEqual(2); |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 222 | }); |
| 223 | |
Matteo Scandolo | b9fb625 | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 224 | it('should format labels', () => { |
| 225 | expect($(element).find('input[name="foo"]').parent().find('label').text()).toBe('Foo:'); |
| 226 | }); |
| 227 | |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 228 | describe('and the model is empty', () => { |
| 229 | beforeEach(() => { |
| 230 | scope.ngModel = { |
| 231 | }; |
| 232 | compileElement(); |
| 233 | }); |
| 234 | |
| 235 | it('should not print the panel', () => { |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 236 | expect($(element).find('.panel.object-field')).not.toExist() |
| 237 | }); |
Matteo Scandolo | bd4057e | 2016-06-08 14:27:30 -0700 | [diff] [blame] | 238 | |
| 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 Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 266 | }); |
| 267 | }); |
Matteo Scandolo | 160e6ce | 2016-06-15 15:58:49 -0700 | [diff] [blame] | 268 | |
| 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 Scandolo | 160e6ce | 2016-06-15 15:58:49 -0700 | [diff] [blame] | 321 | }); |
Matteo Scandolo | 974c0e4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 322 | }); |
| 323 | }); |
| 324 | })(); |