'use strict'; describe('$aria', function() { var scope, $compile, element; beforeEach(module('ngAria')); function injectScopeAndCompiler() { return inject(function(_$compile_, _$rootScope_) { $compile = _$compile_; scope = _$rootScope_; }); } function compileInput(inputHtml) { element = $compile(inputHtml)(scope); scope.$digest(); } describe('aria-hidden', function() { beforeEach(injectScopeAndCompiler); it('should attach aria-hidden to ng-show', function() { compileInput('
'); scope.$apply('val = false'); expect(element.attr('aria-hidden')).toBe('true'); scope.$apply('val = true'); expect(element.attr('aria-hidden')).toBe('false'); }); it('should attach aria-hidden to ng-hide', function() { compileInput('
'); scope.$apply('val = false'); expect(element.attr('aria-hidden')).toBe('false'); scope.$apply('val = true'); expect(element.attr('aria-hidden')).toBe('true'); }); it('should not change aria-hidden if it is already present on ng-show', function() { compileInput('
'); expect(element.attr('aria-hidden')).toBe('userSetValue'); scope.$apply('val = true'); expect(element.attr('aria-hidden')).toBe('userSetValue'); }); it('should not change aria-hidden if it is already present on ng-hide', function() { compileInput('
'); expect(element.attr('aria-hidden')).toBe('userSetValue'); scope.$apply('val = true'); expect(element.attr('aria-hidden')).toBe('userSetValue'); }); }); describe('aria-hidden when disabled', function() { beforeEach(configAriaProvider({ ariaHidden: false })); beforeEach(injectScopeAndCompiler); it('should not attach aria-hidden', function() { scope.$apply('val = false'); compileInput('
'); expect(element.attr('aria-hidden')).toBeUndefined(); compileInput('
'); expect(element.attr('aria-hidden')).toBeUndefined(); }); }); describe('aria-checked', function() { beforeEach(injectScopeAndCompiler); it('should attach itself to input type="checkbox"', function() { compileInput(''); scope.$apply('val = true'); expect(element.attr('aria-checked')).toBe('true'); scope.$apply('val = false'); expect(element.attr('aria-checked')).toBe('false'); }); it('should attach itself to input type="radio"', function() { var element = $compile('' + '')(scope); scope.$apply("val='one'"); expect(element.eq(0).attr('aria-checked')).toBe('true'); expect(element.eq(1).attr('aria-checked')).toBe('false'); scope.$apply("val='two'"); expect(element.eq(0).attr('aria-checked')).toBe('false'); expect(element.eq(1).attr('aria-checked')).toBe('true'); }); it('should attach itself to role="radio"', function() { scope.$apply("val = 'one'"); compileInput('
'); expect(element.attr('aria-checked')).toBe('true'); }); it('should attach itself to role="checkbox"', function() { scope.val = true; compileInput('
'); expect(element.attr('aria-checked')).toBe('true'); }); it('should attach itself to role="menuitemradio"', function() { scope.val = 'one'; compileInput('
'); expect(element.attr('aria-checked')).toBe('true'); }); it('should attach itself to role="menuitemcheckbox"', function() { scope.val = true; compileInput('
'); expect(element.attr('aria-checked')).toBe('true'); }); it('should not attach itself if an aria-checked value is already present', function() { var element = [ $compile("")(scope), $compile("")(scope), $compile("
")(scope), $compile("
")(scope), $compile("
")(scope), $compile("
")(scope) ]; scope.$apply("val1=true;val2='one';val3='1'"); expectAriaAttrOnEachElement(element, 'aria-checked', 'userSetValue'); }); }); describe('aria-checked when disabled', function() { beforeEach(configAriaProvider({ ariaChecked: false })); beforeEach(injectScopeAndCompiler); it('should not attach aria-checked', function() { compileInput("
"); expect(element.attr('aria-checked')).toBeUndefined(); compileInput("
"); expect(element.attr('aria-checked')).toBeUndefined(); compileInput("
"); expect(element.attr('aria-checked')).toBeUndefined(); compileInput("
"); expect(element.attr('aria-checked')).toBeUndefined(); }); }); describe('aria-disabled', function() { beforeEach(injectScopeAndCompiler); it('should attach itself to input elements', function() { scope.$apply('val = false'); compileInput(""); expect(element.attr('aria-disabled')).toBe('false'); scope.$apply('val = true'); expect(element.attr('aria-disabled')).toBe('true'); }); it('should attach itself to textarea elements', function() { scope.$apply('val = false'); compileInput(''); expect(element.attr('aria-disabled')).toBe('false'); scope.$apply('val = true'); expect(element.attr('aria-disabled')).toBe('true'); }); it('should attach itself to button elements', function() { scope.$apply('val = false'); compileInput(''); expect(element.attr('aria-disabled')).toBe('false'); scope.$apply('val = true'); expect(element.attr('aria-disabled')).toBe('true'); }); it('should attach itself to select elements', function() { scope.$apply('val = false'); compileInput(''); expect(element.attr('aria-disabled')).toBe('false'); scope.$apply('val = true'); expect(element.attr('aria-disabled')).toBe('true'); }); it('should not attach itself if an aria-disabled attribute is already present', function() { var element = [ $compile("")(scope), $compile("")(scope), $compile("")(scope), $compile("")(scope) ]; scope.$apply('val = true'); expectAriaAttrOnEachElement(element, 'aria-disabled', 'userSetValue'); }); }); describe('aria-disabled when disabled', function() { beforeEach(configAriaProvider({ ariaDisabled: false })); beforeEach(injectScopeAndCompiler); it('should not attach aria-disabled', function() { var element = [ $compile("")(scope), $compile("")(scope), $compile("")(scope), $compile("")(scope) ]; scope.$apply('val = false'); expectAriaAttrOnEachElement(element, 'aria-disabled', undefined); }); }); describe('aria-invalid', function() { beforeEach(injectScopeAndCompiler); it('should attach aria-invalid to input', function() { compileInput(''); scope.$apply("txtInput='LTten'"); expect(element.attr('aria-invalid')).toBe('true'); scope.$apply("txtInput='morethantencharacters'"); expect(element.attr('aria-invalid')).toBe('false'); }); it('should not attach itself if aria-invalid is already present', function() { compileInput(''); scope.$apply("txtInput='LTten'"); expect(element.attr('aria-invalid')).toBe('userSetValue'); }); }); describe('aria-invalid when disabled', function() { beforeEach(configAriaProvider({ ariaInvalid: false })); beforeEach(injectScopeAndCompiler); it('should not attach aria-invalid if the option is disabled', function() { scope.$apply("txtInput='LTten'"); compileInput(''); expect(element.attr('aria-invalid')).toBeUndefined(); }); }); describe('aria-required', function() { beforeEach(injectScopeAndCompiler); it('should attach aria-required to input', function() { compileInput(''); expect(element.attr('aria-required')).toBe('true'); scope.$apply("val='input is valid now'"); expect(element.attr('aria-required')).toBe('false'); }); it('should attach aria-required to textarea', function() { compileInput(''); expect(element.attr('aria-required')).toBe('true'); scope.$apply("val='input is valid now'"); expect(element.attr('aria-required')).toBe('false'); }); it('should attach aria-required to select', function() { compileInput(''); expect(element.attr('aria-required')).toBe('true'); scope.$apply("val='input is valid now'"); expect(element.attr('aria-required')).toBe('false'); }); it('should attach aria-required to ngRequired', function() { compileInput(''); expect(element.attr('aria-required')).toBe('true'); scope.$apply("val='input is valid now'"); expect(element.attr('aria-required')).toBe('false'); }); it('should not attach itself if aria-required is already present', function() { compileInput(""); expect(element.attr('aria-required')).toBe('userSetValue'); compileInput(""); expect(element.attr('aria-required')).toBe('userSetValue'); compileInput(""); expect(element.attr('aria-required')).toBe('userSetValue'); compileInput(""); expect(element.attr('aria-required')).toBe('userSetValue'); }); }); describe('aria-required when disabled', function() { beforeEach(configAriaProvider({ ariaRequired: false })); beforeEach(injectScopeAndCompiler); it('should not add the aria-required attribute', function() { compileInput(""); expect(element.attr('aria-required')).toBeUndefined(); compileInput(""); expect(element.attr('aria-required')).toBeUndefined(); compileInput(""); expect(element.attr('aria-required')).toBeUndefined(); }); }); describe('aria-multiline', function() { beforeEach(injectScopeAndCompiler); it('should attach itself to textarea', function() { compileInput(''); expect(element.attr('aria-multiline')).toBe('true'); }); it('should attach itself role="textbox"', function() { compileInput('
'); expect(element.attr('aria-multiline')).toBe('true'); }); it('should not attach itself if aria-multiline is already present', function() { compileInput(''); expect(element.attr('aria-multiline')).toBe('userSetValue'); compileInput('
'); expect(element.attr('aria-multiline')).toBe('userSetValue'); }); }); describe('aria-multiline when disabled', function() { beforeEach(configAriaProvider({ ariaMultiline: false })); beforeEach(injectScopeAndCompiler); it('should not attach itself to textarea', function() { compileInput(''); expect(element.attr('aria-multiline')).toBeUndefined(); }); it('should not attach itself role="textbox"', function() { compileInput('
'); expect(element.attr('aria-multiline')).toBeUndefined(); }); }); describe('aria-value', function() { beforeEach(injectScopeAndCompiler); it('should attach to input type="range"', function() { var element = [ $compile('')(scope), $compile('
')(scope), $compile('
')(scope) ]; scope.$apply('val = 50'); expectAriaAttrOnEachElement(element, 'aria-valuenow', "50"); expectAriaAttrOnEachElement(element, 'aria-valuemin', "0"); expectAriaAttrOnEachElement(element, 'aria-valuemax', "100"); scope.$apply('val = 90'); expectAriaAttrOnEachElement(element, 'aria-valuenow', "90"); }); it('should not attach if aria-value* is already present', function() { var element = [ $compile('')(scope), $compile('
')(scope), $compile('
')(scope) ]; scope.$apply('val = 50'); expectAriaAttrOnEachElement(element, 'aria-valuenow', 'userSetValue1'); expectAriaAttrOnEachElement(element, 'aria-valuemin', 'userSetValue2'); expectAriaAttrOnEachElement(element, 'aria-valuemax', 'userSetValue3'); }); }); describe('announcing ngMessages', function() { beforeEach(injectScopeAndCompiler); it('should attach aria-live', function() { var element = [ $compile('
')(scope) ]; expectAriaAttrOnEachElement(element, 'aria-live', "assertive"); }); }); describe('aria-value when disabled', function() { beforeEach(configAriaProvider({ ariaValue: false })); beforeEach(injectScopeAndCompiler); it('should not attach itself', function() { scope.$apply('val = 50'); compileInput(''); expect(element.attr('aria-valuenow')).toBeUndefined(); expect(element.attr('aria-valuemin')).toBeUndefined(); expect(element.attr('aria-valuemax')).toBeUndefined(); compileInput('
'); expect(element.attr('aria-valuenow')).toBeUndefined(); expect(element.attr('aria-valuemin')).toBeUndefined(); expect(element.attr('aria-valuemax')).toBeUndefined(); }); }); describe('tabindex', function() { beforeEach(injectScopeAndCompiler); it('should attach tabindex to role="checkbox", ng-click, and ng-dblclick', function() { compileInput('
'); expect(element.attr('tabindex')).toBe('0'); compileInput('
'); expect(element.attr('tabindex')).toBe('0'); compileInput('
'); expect(element.attr('tabindex')).toBe('0'); }); it('should not attach tabindex if it is already on an element', function() { compileInput('
'); expect(element.attr('tabindex')).toBe('userSetValue'); compileInput('
'); expect(element.attr('tabindex')).toBe('userSetValue'); compileInput('
'); expect(element.attr('tabindex')).toBe('userSetValue'); compileInput('
'); expect(element.attr('tabindex')).toBe('userSetValue'); }); it('should set proper tabindex values for radiogroup', function() { compileInput('
' + '
1
' + '
2
' + '
'); var one = element.contents().eq(0); var two = element.contents().eq(1); scope.$apply("val = 'one'"); expect(one.attr('tabindex')).toBe('0'); expect(two.attr('tabindex')).toBe('-1'); scope.$apply("val = 'two'"); expect(one.attr('tabindex')).toBe('-1'); expect(two.attr('tabindex')).toBe('0'); dealoc(element); }); }); describe('tabindex when disabled', function() { beforeEach(configAriaProvider({ tabindex: false })); beforeEach(injectScopeAndCompiler); it('should not add a tabindex attribute', function() { compileInput('
'); expect(element.attr('tabindex')).toBeUndefined(); compileInput('
'); expect(element.attr('tabindex')).toBeUndefined(); compileInput('
'); expect(element.attr('tabindex')).toBeUndefined(); compileInput('
'); expect(element.attr('tabindex')).toBeUndefined(); }); }); }); function expectAriaAttrOnEachElement(elem, ariaAttr, expected) { angular.forEach(elem, function(val) { expect(angular.element(val).attr(ariaAttr)).toBe(expected); }); } function configAriaProvider(config) { return function() { angular.module('ariaTest', ['ngAria']).config(function($ariaProvider) { $ariaProvider.config(config); }); module('ariaTest'); }; }