|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +function expectAriaAttr(elem, ariaAttr, expected){ |
| 4 | + angular.forEach(elem, function(val){ |
| 5 | + expect(angular.element(val).attr(ariaAttr)).toBe(expected); |
| 6 | + }); |
| 7 | +} |
| 8 | + |
| 9 | +describe('$aria', function(){ |
| 10 | + |
| 11 | + describe('aria-hidden', function(){ |
| 12 | + beforeEach(module('ngAria')); |
| 13 | + |
| 14 | + it('should attach aria-hidden to ng-show', inject(function($compile, $rootScope){ |
| 15 | + var element = $compile("<div ng-show='val'></div>")($rootScope); |
| 16 | + $rootScope.$apply('val=false'); |
| 17 | + expectAriaAttr(element, 'aria-hidden', 'true'); |
| 18 | + $rootScope.$apply('val=true'); |
| 19 | + expectAriaAttr(element, 'aria-hidden', 'false'); |
| 20 | + })); |
| 21 | + |
| 22 | + it('should attach aria-hidden to ng-hide', inject(function($compile, $rootScope){ |
| 23 | + var element = $compile("<div ng-hide='val'></div>")($rootScope); |
| 24 | + $rootScope.$apply('val=false'); |
| 25 | + expectAriaAttr(element, 'aria-hidden', 'false'); |
| 26 | + $rootScope.$apply('val=true'); |
| 27 | + expectAriaAttr(element, 'aria-hidden', 'true'); |
| 28 | + })); |
| 29 | + |
| 30 | + it('should not attach if an aria-hidden is already present', inject(function($compile, $rootScope){ |
| 31 | + var element = [ |
| 32 | + $compile('<div ng-show="val" aria-hidden="userSetValue"></div>')($rootScope), |
| 33 | + $compile('<div ng-hide="val" aria-hidden="userSetValue"></div>')($rootScope) |
| 34 | + ]; |
| 35 | + $rootScope.$apply('val=true'); |
| 36 | + expectAriaAttr(element, 'aria-hidden', 'userSetValue'); |
| 37 | + })); |
| 38 | + |
| 39 | + describe('disabled', function(){ |
| 40 | + beforeEach(function(){ |
| 41 | + angular.module('ariaTest', ['ngAria']).config(function($ariaProvider){ |
| 42 | + $ariaProvider.config({ |
| 43 | + ariaHidden : false |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + module('ariaTest'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should not attach aria-hidden if the option is disabled', inject(function($compile, $rootScope){ |
| 51 | + var element = [ |
| 52 | + $compile("<div ng-show='val'></div>")($rootScope), |
| 53 | + $compile("<div ng-hide='val'></div>")($rootScope) |
| 54 | + ]; |
| 55 | + $rootScope.$apply('val=false'); |
| 56 | + expectAriaAttr(element, 'aria-hidden', undefined); |
| 57 | + })); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('aria-checked', function(){ |
| 62 | + beforeEach(module('ngAria')); |
| 63 | + |
| 64 | + it('should attach itself to input type=checkbox', inject(function($compile, $rootScope){ |
| 65 | + var element = $compile("<input type='checkbox' ng-model='val' ng-init='val = true'>")($rootScope); |
| 66 | + $rootScope.$apply('val=true'); |
| 67 | + expectAriaAttr(element, 'aria-checked', 'true'); |
| 68 | + $rootScope.$apply('val=false'); |
| 69 | + expectAriaAttr(element, 'aria-checked', 'false'); |
| 70 | + })); |
| 71 | + |
| 72 | + it('should attach itself to input type=radio', inject(function($compile, $rootScope){ |
| 73 | + var element = $compile("<input type='radio' ng-model='val' value='one'><input type='radio' ng-model='val' value='two'>")($rootScope); |
| 74 | + $rootScope.$apply("val='one'"); |
| 75 | + expect(angular.element(element).eq(0).attr('aria-checked')).toBe('true'); |
| 76 | + expect(angular.element(element).eq(1).attr('aria-checked')).toBe('false'); |
| 77 | + |
| 78 | + $rootScope.$apply("val='two'"); |
| 79 | + expect(angular.element(element).eq(0).attr('aria-checked')).toBe('false'); |
| 80 | + expect(angular.element(element).eq(1).attr('aria-checked')).toBe('true'); |
| 81 | + })); |
| 82 | + |
| 83 | + it('should attach itself to role="radio", role="checkbox", role="menuitemradio" and role="menuitemcheckbox"', inject(function($compile, $rootScope){ |
| 84 | + var element = [ |
| 85 | + $compile("<div role='radio' ng-model='val' value='{{val}}'></div>")($rootScope), |
| 86 | + $compile("<div role='menuitemradio' ng-model='val' value='{{val}}'></div>")($rootScope), |
| 87 | + $compile("<div role='checkbox' checked='checked'></div>")($rootScope), |
| 88 | + $compile("<div role='menuitemcheckbox' checked='checked'></div>")($rootScope) |
| 89 | + ]; |
| 90 | + $rootScope.$apply("val='one'"); |
| 91 | + expectAriaAttr(element, 'aria-checked', 'true'); |
| 92 | + })); |
| 93 | + |
| 94 | + it('should not attach itself if an aria-checked value is already present', inject(function($compile, $rootScope){ |
| 95 | + var element = [ |
| 96 | + $compile("<input type='checkbox' ng-model='val1' aria-checked='userSetValue'>")($rootScope), |
| 97 | + $compile("<input type='radio' ng-model='val2' value='one' aria-checked='userSetValue'><input type='radio' ng-model='val2' value='two'>")($rootScope), |
| 98 | + $compile("<div role='radio' ng-model='val' value='{{val3}}' aria-checked='userSetValue'></div>")($rootScope), |
| 99 | + $compile("<div role='menuitemradio' ng-model='val' value='{{val3}}' aria-checked='userSetValue'></div>")($rootScope), |
| 100 | + $compile("<div role='checkbox' checked='checked' aria-checked='userSetValue'></div>")($rootScope), |
| 101 | + $compile("<div role='menuitemcheckbox' checked='checked' aria-checked='userSetValue'></div>")($rootScope) |
| 102 | + ]; |
| 103 | + $rootScope.$apply("val1=true;val2='one';val3='1'"); |
| 104 | + expectAriaAttr(element, 'aria-checked', 'userSetValue'); |
| 105 | + })); |
| 106 | + |
| 107 | + describe('disabled', function(){ |
| 108 | + beforeEach(function(){ |
| 109 | + angular.module('ariaTest', ['ngAria']).config(function($ariaProvider){ |
| 110 | + $ariaProvider.config({ |
| 111 | + ariaChecked : false |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + module('ariaTest'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should not attach aria-checked if the option is disabled', inject(function($compile, $rootScope){ |
| 119 | + var element = [ |
| 120 | + $compile("<div role='radio' ng-model='val' value='{{val}}'></div>")($rootScope), |
| 121 | + $compile("<div role='menuitemradio' ng-model='val' value='{{val}}'></div>")($rootScope), |
| 122 | + $compile("<div role='checkbox' checked='checked'></div>")($rootScope), |
| 123 | + $compile("<div role='menuitemcheckbox' checked='checked'></div>")($rootScope) |
| 124 | + ]; |
| 125 | + $rootScope.$digest(); |
| 126 | + expectAriaAttr(element, 'aria-checked', undefined); |
| 127 | + })); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + describe('aria-disabled', function(){ |
| 132 | + beforeEach(module('ngAria')); |
| 133 | + |
| 134 | + it('should attach itself to input, textarea, button and select', inject(function($compile, $rootScope){ |
| 135 | + var element = [ |
| 136 | + $compile("<input ng-disabled='val'>")($rootScope), |
| 137 | + $compile("<textarea ng-disabled='val'></textarea>")($rootScope), |
| 138 | + $compile("<button ng-disabled='val'></button>")($rootScope), |
| 139 | + $compile("<select ng-disabled='val'></select>")($rootScope) |
| 140 | + ]; |
| 141 | + $rootScope.$apply('val=false'); |
| 142 | + expectAriaAttr(element, 'aria-disabled', 'false'); |
| 143 | + |
| 144 | + $rootScope.$apply('val=true'); |
| 145 | + expectAriaAttr(element, 'aria-disabled', 'true'); |
| 146 | + })); |
| 147 | + |
| 148 | + it('should not attach itself if an aria tag is already present', inject(function($compile, $rootScope){ |
| 149 | + var element = [ |
| 150 | + $compile("<input aria-disabled='userSetValue' ng-disabled='val'>")($rootScope), |
| 151 | + $compile("<textarea aria-disabled='userSetValue' ng-disabled='val'></textarea>")($rootScope), |
| 152 | + $compile("<button aria-disabled='userSetValue' ng-disabled='val'></button>")($rootScope), |
| 153 | + $compile("<select aria-disabled='userSetValue' ng-disabled='val'></select>")($rootScope) |
| 154 | + ]; |
| 155 | + |
| 156 | + $rootScope.$apply('val=true'); |
| 157 | + expectAriaAttr(element, 'aria-disabled', 'userSetValue'); |
| 158 | + })); |
| 159 | + |
| 160 | + describe('disabled', function(){ |
| 161 | + beforeEach(function(){ |
| 162 | + angular.module('ariaTest', ['ngAria']).config(function($ariaProvider){ |
| 163 | + $ariaProvider.config({ |
| 164 | + ariaDisabled : false |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + module('ariaTest'); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should not attach aria-disabled if the option is disabled', inject(function($compile, $rootScope){ |
| 172 | + var element = [ |
| 173 | + $compile("<input ng-disabled='val'>")($rootScope), |
| 174 | + $compile("<textarea ng-disabled='val'></textarea>")($rootScope), |
| 175 | + $compile("<button ng-disabled='val'></button>")($rootScope), |
| 176 | + $compile("<select ng-disabled='val'></select>")($rootScope) |
| 177 | + ]; |
| 178 | + |
| 179 | + $rootScope.$apply('val=false'); |
| 180 | + expectAriaAttr(element, 'aria-disabled', undefined); |
| 181 | + })); |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
| 185 | + describe('aria-invalid', function(){ |
| 186 | + beforeEach(module('ngAria')); |
| 187 | + |
| 188 | + it('should attach aria-invalid to input', inject(function($compile, $rootScope){ |
| 189 | + var element = $compile("<input ng-model='txtInput' ng-minlength='10'>")($rootScope); |
| 190 | + $rootScope.$apply("txtInput='LTten'"); |
| 191 | + expectAriaAttr(element, 'aria-invalid', 'true'); |
| 192 | + |
| 193 | + $rootScope.$apply("txtInput='morethantencharacters'"); |
| 194 | + expectAriaAttr(element, 'aria-invalid', 'false'); |
| 195 | + })); |
| 196 | + |
| 197 | + it('should not attach itself if aria-invalid is already present', inject(function($compile, $rootScope){ |
| 198 | + var element = $compile("<input ng-model='txtInput' ng-minlength='10' aria-invalid='userSetValue'>")($rootScope); |
| 199 | + $rootScope.$apply("txtInput='LTten'"); |
| 200 | + expectAriaAttr(element, 'aria-invalid', 'userSetValue'); |
| 201 | + })); |
| 202 | + |
| 203 | + describe('disabled', function(){ |
| 204 | + beforeEach(function(){ |
| 205 | + angular.module('ariaTest', ['ngAria']).config(function($ariaProvider){ |
| 206 | + $ariaProvider.config({ |
| 207 | + ariaInvalid : false |
| 208 | + }); |
| 209 | + }); |
| 210 | + |
| 211 | + module('ariaTest'); |
| 212 | + }); |
| 213 | + |
| 214 | + it('should not attach aria-invalid if the option is disabled', inject(function($compile, $rootScope){ |
| 215 | + var element = $compile("<input ng-model='txtInput' ng-minlength='10'>")($rootScope); |
| 216 | + $rootScope.$apply("txtInput='LTten'"); |
| 217 | + expectAriaAttr(element, 'aria-invalid', undefined); |
| 218 | + })); |
| 219 | + }); |
| 220 | + }); |
| 221 | + |
| 222 | + describe('aria-required', function(){ |
| 223 | + beforeEach(module('ngAria')); |
| 224 | + |
| 225 | + it('should attach aria-required to input, textarea, select and ngRequired', inject(function($compile, $rootScope){ |
| 226 | + var element = [ |
| 227 | + $compile("<input ng-model='val' required>")($rootScope), |
| 228 | + $compile("<textarea ng-model='val' required></textarea>")($rootScope), |
| 229 | + $compile("<select ng-model='val' required></select>")($rootScope), |
| 230 | + $compile("<input ng-model='val' ng-required='true'>")($rootScope) |
| 231 | + ]; |
| 232 | + $rootScope.$digest(); |
| 233 | + expectAriaAttr(element, 'aria-required', 'true'); |
| 234 | + |
| 235 | + element = [ |
| 236 | + $compile("<input ng-model='val'>")($rootScope), |
| 237 | + $compile("<textarea ng-model='val'></textarea>")($rootScope), |
| 238 | + $compile("<select ng-model='val'></select>")($rootScope), |
| 239 | + $compile("<input ng-model='val' ng-required='false'>")($rootScope) |
| 240 | + ]; |
| 241 | + $rootScope.$apply("val='input is valid now'"); |
| 242 | + expectAriaAttr(element, 'aria-required', 'false'); |
| 243 | + })); |
| 244 | + |
| 245 | + it('should not attach itself if aria-required is already present', inject(function($compile, $rootScope){ |
| 246 | + var element = [ |
| 247 | + $compile("<input ng-model='val' required aria-required='userSetValue'>")($rootScope), |
| 248 | + $compile("<textarea ng-model='val' required aria-required='userSetValue'></textarea>")($rootScope), |
| 249 | + $compile("<select ng-model='val' required aria-required='userSetValue'></select>")($rootScope), |
| 250 | + $compile("<input ng-model='val' ng-required='true' aria-required='userSetValue'>")($rootScope) |
| 251 | + ]; |
| 252 | + |
| 253 | + $rootScope.$digest(); |
| 254 | + expectAriaAttr(element, 'aria-required', 'userSetValue'); |
| 255 | + })); |
| 256 | + |
| 257 | + describe('disabled', function(){ |
| 258 | + beforeEach(function(){ |
| 259 | + angular.module('ariaTest', ['ngAria']).config(function($ariaProvider){ |
| 260 | + $ariaProvider.config({ |
| 261 | + ariaRequired : false |
| 262 | + }); |
| 263 | + }); |
| 264 | + |
| 265 | + module('ariaTest'); |
| 266 | + }); |
| 267 | + |
| 268 | + it('should not attach aria-required when the option is disabled', inject(function($compile, $rootScope){ |
| 269 | + var element = [ |
| 270 | + $compile("<input ng-model='val' required>")($rootScope), |
| 271 | + $compile("<textarea ng-model='val' required></textarea>")($rootScope), |
| 272 | + $compile("<select ng-model='val' required></select>")($rootScope) |
| 273 | + ]; |
| 274 | + |
| 275 | + $rootScope.$digest(); |
| 276 | + expectAriaAttr(element, 'aria-required', undefined); |
| 277 | + })); |
| 278 | + }); |
| 279 | + }); |
| 280 | + |
| 281 | + describe('aria-multiline', function(){ |
| 282 | + beforeEach(module('ngAria')); |
| 283 | + |
| 284 | + it('should attach aria-multiline to textbox and role="textbox"', inject(function($compile, $rootScope){ |
| 285 | + var element = [ |
| 286 | + $compile("<textarea></textarea>")($rootScope), |
| 287 | + $compile("<div role='textbox'></div>")($rootScope) |
| 288 | + ]; |
| 289 | + |
| 290 | + $rootScope.$digest(); |
| 291 | + expectAriaAttr(element, 'aria-multiline', 'true'); |
| 292 | + })); |
| 293 | + |
| 294 | + it('should not attach if aria-multiline is already present', inject(function($compile, $rootScope){ |
| 295 | + var element = [ |
| 296 | + $compile("<textarea aria-multiline='userSetValue'></textarea>")($rootScope), |
| 297 | + $compile("<div role='textbox' aria-multiline='userSetValue'></div>")($rootScope) |
| 298 | + ]; |
| 299 | + |
| 300 | + $rootScope.$digest(); |
| 301 | + expectAriaAttr(element, 'aria-multiline', 'userSetValue'); |
| 302 | + })); |
| 303 | + |
| 304 | + describe('disabled', function(){ |
| 305 | + beforeEach(function(){ |
| 306 | + angular.module('ariaTest', ['ngAria']).config(function($ariaProvider){ |
| 307 | + $ariaProvider.config({ |
| 308 | + ariaMultiline : false |
| 309 | + }); |
| 310 | + }); |
| 311 | + |
| 312 | + module('ariaTest'); |
| 313 | + }); |
| 314 | + |
| 315 | + it('should not attach itself to textbox or role="textbox" when disabled', inject(function($compile, $rootScope){ |
| 316 | + var element = [ |
| 317 | + $compile("<textarea></textarea>")($rootScope), |
| 318 | + $compile("<div role='textbox'></div>")($rootScope) |
| 319 | + ]; |
| 320 | + |
| 321 | + $rootScope.$digest(); |
| 322 | + expectAriaAttr(element, 'aria-multiline', undefined); |
| 323 | + })); |
| 324 | + }); |
| 325 | + }); |
| 326 | + |
| 327 | + describe('aria-value', function(){ |
| 328 | + beforeEach(module('ngAria')); |
| 329 | + |
| 330 | + it('should attach to input type="range"', inject(function($compile, $rootScope){ |
| 331 | + var element = [ |
| 332 | + $compile('<input type="range" ng-model="val" min="0" max="100">')($rootScope), |
| 333 | + $compile('<div role="progressbar" min="0" max="100" ng-model="val">')($rootScope), |
| 334 | + $compile('<div role="slider" min="0" max="100" ng-model="val">')($rootScope) |
| 335 | + ]; |
| 336 | + |
| 337 | + $rootScope.$apply('val=50'); |
| 338 | + expectAriaAttr(element, 'aria-valuenow', "50"); |
| 339 | + expectAriaAttr(element, 'aria-valuemin', "0"); |
| 340 | + expectAriaAttr(element, 'aria-valuemax', "100"); |
| 341 | + |
| 342 | + $rootScope.$apply('val=90'); |
| 343 | + expectAriaAttr(element, 'aria-valuenow', "90"); |
| 344 | + })); |
| 345 | + |
| 346 | + it('should not attach if aria-value* is already present', inject(function($compile, $rootScope){ |
| 347 | + var element = [ |
| 348 | + $compile('<input type="range" ng-model="val" min="0" max="100" aria-valuenow="userSetValue1" aria-valuemin="userSetValue2" aria-valuemax="userSetValue3">')($rootScope), |
| 349 | + $compile('<div role="progressbar" min="0" max="100" ng-model="val" aria-valuenow="userSetValue1" aria-valuemin="userSetValue2" aria-valuemax="userSetValue3">')($rootScope), |
| 350 | + $compile('<div role="slider" min="0" max="100" ng-model="val" aria-valuenow="userSetValue1" aria-valuemin="userSetValue2" aria-valuemax="userSetValue3">')($rootScope) |
| 351 | + ]; |
| 352 | + |
| 353 | + $rootScope.$apply('val=50'); |
| 354 | + expectAriaAttr(element, 'aria-valuenow', "userSetValue1"); |
| 355 | + expectAriaAttr(element, 'aria-valuemin', "userSetValue2"); |
| 356 | + expectAriaAttr(element, 'aria-valuemax', "userSetValue3"); |
| 357 | + })); |
| 358 | + |
| 359 | + describe('disabled', function(){ |
| 360 | + beforeEach(function(){ |
| 361 | + angular.module('ariaTest', ['ngAria']).config(function($ariaProvider){ |
| 362 | + $ariaProvider.config({ |
| 363 | + ariaValue : false |
| 364 | + }); |
| 365 | + }); |
| 366 | + |
| 367 | + module('ariaTest'); |
| 368 | + }); |
| 369 | + |
| 370 | + it('should not attach itself when the option is disabled', inject(function($compile, $rootScope){ |
| 371 | + var element = [ |
| 372 | + $compile('<input type="range" ng-model="val" min="0" max="100">')($rootScope), |
| 373 | + $compile('<div role="progressbar" min="0" max="100" ng-model="val">')($rootScope) |
| 374 | + ]; |
| 375 | + |
| 376 | + $rootScope.$apply('val=50'); |
| 377 | + expectAriaAttr(element, 'aria-valuenow', undefined); |
| 378 | + expectAriaAttr(element, 'aria-valuemin', undefined); |
| 379 | + expectAriaAttr(element, 'aria-valuemax', undefined); |
| 380 | + })); |
| 381 | + }); |
| 382 | + }); |
| 383 | + |
| 384 | + describe('tabindex', function(){ |
| 385 | + beforeEach(module('ngAria')); |
| 386 | + |
| 387 | + it('should attach tabindex to role=button, role=checkbox and ng-click', inject(function($compile, $rootScope){ |
| 388 | + var element = [ |
| 389 | + $compile("<div role='button'></div>")($rootScope), |
| 390 | + $compile("<div role='checkbox'></div>")($rootScope), |
| 391 | + $compile("<div ng-click='someAction()'></div>")($rootScope) |
| 392 | + ]; |
| 393 | + $rootScope.$digest(); |
| 394 | + |
| 395 | + expectAriaAttr(element, 'tabindex', '0'); |
| 396 | + })); |
| 397 | + |
| 398 | + it('should not attach tabindex to role=button, role=checkbox and ng-click if they are already present', inject(function($compile, $rootScope){ |
| 399 | + var element = [ |
| 400 | + $compile("<div role='button' tabindex='userSetValue'></div>")($rootScope), |
| 401 | + $compile("<div role='checkbox' tabindex='userSetValue'></div>")($rootScope), |
| 402 | + $compile("<div ng-click='someAction()' tabindex='userSetValue'></div>")($rootScope) |
| 403 | + ]; |
| 404 | + $rootScope.$digest(); |
| 405 | + |
| 406 | + expectAriaAttr(element, 'tabindex', 'userSetValue'); |
| 407 | + })); |
| 408 | + |
| 409 | + it('should set proper tabindex values for radiogroup', inject(function($compile, $rootScope){ |
| 410 | + var element = $compile("<div role='radiogroup'><div role='radio' ng-model='val' value='one'>1</div><div role='radio' ng-model='val' value='two'>2</div></div>")($rootScope); |
| 411 | + |
| 412 | + $rootScope.$apply("val='one'"); |
| 413 | + expect(angular.element(angular.element(element).children()[0]).attr('tabindex')).toBe('0'); |
| 414 | + expect(angular.element(angular.element(element).children()[1]).attr('tabindex')).toBe('-1'); |
| 415 | + |
| 416 | + $rootScope.$apply("val='two'"); |
| 417 | + expect(angular.element(angular.element(element).children()[0]).attr('tabindex')).toBe('-1'); |
| 418 | + expect(angular.element(angular.element(element).children()[1]).attr('tabindex')).toBe('0'); |
| 419 | + |
| 420 | + dealoc(element); |
| 421 | + })); |
| 422 | + |
| 423 | + describe('disabled', function(){ |
| 424 | + beforeEach(function(){ |
| 425 | + angular.module('ariaTest', ['ngAria']).config(function($ariaProvider){ |
| 426 | + $ariaProvider.config({ |
| 427 | + tabindex : false |
| 428 | + }); |
| 429 | + }); |
| 430 | + |
| 431 | + module('ariaTest'); |
| 432 | + }); |
| 433 | + it('should not attach when disabled', inject(function($compile, $rootScope){ |
| 434 | + var element = [ |
| 435 | + $compile("<div role='button'></div>")($rootScope), |
| 436 | + $compile("<div role='checkbox'></div>")($rootScope), |
| 437 | + $compile("<div ng-click='someAction()'></div>")($rootScope) |
| 438 | + ]; |
| 439 | + $rootScope.$digest(); |
| 440 | + expectAriaAttr(element, 'tabindex', undefined); |
| 441 | + })); |
| 442 | + }); |
| 443 | + }); |
| 444 | +}); |
0 commit comments