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 | 160e6ce | 2016-06-15 15:58:49 -0700 | [diff] [blame^] | 183 | // NOTE .click is not working anymore |
| 184 | xit('should change value to false', () => { |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 185 | expect(isolatedScope.ngModel).toEqual(true); |
| 186 | setFalse.click() |
| 187 | expect(isolatedScope.ngModel).toEqual(false); |
| 188 | }); |
| 189 | |
Matteo Scandolo | 160e6ce | 2016-06-15 15:58:49 -0700 | [diff] [blame^] | 190 | xit('should change value to true', () => { |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 191 | isolatedScope.ngModel = false; |
| 192 | scope.$apply(); |
| 193 | expect(isolatedScope.ngModel).toEqual(false); |
| 194 | setTrue.click() |
| 195 | expect(isolatedScope.ngModel).toEqual(true); |
| 196 | }); |
| 197 | }); |
| 198 | |
| 199 | describe('when an object input is passed', () => { |
| 200 | beforeEach(() => { |
| 201 | scope = rootScope.$new(); |
| 202 | scope.name = 'label'; |
| 203 | scope.field = { |
| 204 | label: 'Label', |
| 205 | type: 'object', |
| 206 | validators: {} |
| 207 | }; |
| 208 | scope.ngModel = { |
| 209 | baz: true, |
| 210 | foo: 'bar', |
| 211 | foz: 1, |
| 212 | }; |
| 213 | compileElement(); |
| 214 | }); |
| 215 | |
| 216 | it('should print a panel to contain object property field', () => { |
| 217 | expect($(element).find('.panel.object-field')).toExist() |
| 218 | }); |
| 219 | |
| 220 | it('should print the right input type for each property', () => { |
| 221 | expect($(element).find('input').length).toBe(2); |
arpiagariu | 4cf107b | 2016-06-15 09:53:37 -0700 | [diff] [blame] | 222 | expect($(element).find('.boolean-field > a').length).toEqual(2); |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 223 | }); |
| 224 | |
Matteo Scandolo | b9fb625 | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 225 | it('should format labels', () => { |
| 226 | expect($(element).find('input[name="foo"]').parent().find('label').text()).toBe('Foo:'); |
| 227 | }); |
| 228 | |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 229 | describe('and the model is empty', () => { |
| 230 | beforeEach(() => { |
| 231 | scope.ngModel = { |
| 232 | }; |
| 233 | compileElement(); |
| 234 | }); |
| 235 | |
| 236 | it('should not print the panel', () => { |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 237 | expect($(element).find('.panel.object-field')).not.toExist() |
| 238 | }); |
Matteo Scandolo | bd4057e | 2016-06-08 14:27:30 -0700 | [diff] [blame] | 239 | |
| 240 | describe('but field is configured', () => { |
| 241 | beforeEach(() => { |
| 242 | scope.field.properties = { |
| 243 | foo: { |
| 244 | label: 'FooLabel:', |
| 245 | type: 'string', |
| 246 | validators: { |
| 247 | required: true |
| 248 | } |
| 249 | }, |
| 250 | bar: { |
| 251 | type: 'number' |
| 252 | } |
| 253 | }; |
| 254 | compileElement(); |
| 255 | }); |
| 256 | it('should render panel and configured fields', () => { |
| 257 | expect($(element).find('.panel.object-field')).toExist(); |
| 258 | expect($(element).find('input[name="foo"]').parent().find('label').text()).toBe('FooLabel:'); |
| 259 | expect($(element).find('input[name="foo"]')).toHaveAttr('type', 'string'); |
| 260 | expect($(element).find('input[name="foo"]')).toHaveAttr('required'); |
| 261 | |
| 262 | expect($(element).find('input[name="bar"]').parent().find('label').text()).toBe('Bar:'); |
| 263 | expect($(element).find('input[name="bar"]')).toHaveAttr('type', 'number'); |
| 264 | |
| 265 | }); |
| 266 | }); |
Matteo Scandolo | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 267 | }); |
| 268 | }); |
Matteo Scandolo | 160e6ce | 2016-06-15 15:58:49 -0700 | [diff] [blame^] | 269 | |
| 270 | // NOTE not sure why this tests are failing |
| 271 | describe('when validation options are passed', () => { |
| 272 | let input; |
| 273 | describe('given a a text field', () => { |
| 274 | beforeEach(() => { |
| 275 | scope.field = { |
| 276 | label: 'Label', |
| 277 | type: 'text', |
| 278 | validators: { |
| 279 | minlength: 10, |
| 280 | maxlength: 15, |
| 281 | required: true |
| 282 | } |
| 283 | }; |
| 284 | |
| 285 | scope.$digest(); |
| 286 | input = $(element).find('input'); |
| 287 | }); |
| 288 | |
| 289 | it('should validate required', () => { |
| 290 | scope.ngModel= null; |
| 291 | scope.$digest(); |
| 292 | expect(input).toHaveClass('ng-invalid-required'); |
| 293 | |
| 294 | scope.ngModel= 'not too short'; |
| 295 | scope.$digest(); |
| 296 | expect(input).not.toHaveClass('ng-invalid-required'); |
| 297 | expect(input).not.toHaveClass('ng-invalid'); |
| 298 | }); |
| 299 | |
| 300 | it('should validate minlength', () => { |
| 301 | scope.ngModel= 'short'; |
| 302 | scope.$digest(); |
| 303 | expect(input).toHaveClass('ng-invalid-minlength'); |
| 304 | |
| 305 | scope.ngModel= 'not too short'; |
| 306 | scope.$digest(); |
| 307 | expect(input).not.toHaveClass('ng-invalid-minlength'); |
| 308 | expect(input).not.toHaveClass('ng-invalid'); |
| 309 | }); |
| 310 | |
| 311 | it('should validate maxlength', () => { |
| 312 | scope.ngModel= 'this is definitely too long!!'; |
| 313 | scope.$digest(); |
| 314 | expect(input).toHaveClass('ng-invalid-maxlength'); |
| 315 | |
| 316 | scope.ngModel= 'not too short'; |
| 317 | scope.$digest(); |
| 318 | expect(input).not.toHaveClass('ng-invalid-maxlength'); |
| 319 | expect(input).not.toHaveClass('ng-invalid'); |
| 320 | }); |
| 321 | }); |
| 322 | |
| 323 | }); |
| 324 | |
Matteo Scandolo | 974c0e4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 325 | }); |
| 326 | }); |
| 327 | })(); |