Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
feat(aria): add provider to disable console warnings.
Browse files Browse the repository at this point in the history
Fixes #3507.
  • Loading branch information
devversion committed Jun 9, 2016
1 parent 39911d3 commit b4b265b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 7 deletions.
57 changes: 53 additions & 4 deletions src/core/services/aria/aria.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,60 @@
/**
* @ngdoc module
* @name material.core.aria
* @description
* Aria Expectations for ngMaterial components.
*/
angular
.module('material.core')
.provider('$mdAria', MdAriaProvider);

/**
* Whether we should show ARIA warnings in the console, if labels are missing on the element
* By default the warnings are enabled
*/
var showWarnings = true;


/**
* @ngdoc service
* @name $mdAriaProvider
* @module material.core.aria
*
* @description
*
* Modify options of the `$mdAria` service, which will be used by most of the Angular Material components.
**
*
* You are able to disable `$mdAria` warnings, by using the following markup.
* <hljs lang="js">
* app.config(function($mdAriaProvider) {
* // Globally disables all ARIA warnings.
* $mdAriaProvider.disableWarnings();
* });
* </hljs>
*
*/
function MdAriaProvider() {

angular.module('material.core')
.service('$mdAria', AriaService);
return {
disableWarnings: disableWarnings,
$get: MdAriaService
};

/**
* @ngdoc method
* @name $mdAriaProvider#disableWarnings
* @param {boolean} disable Whether to show `$mdAria` console warnings. Default value is `false`.
*/
function disableWarnings(disable) {
showWarnings = !disable || false;
}
}

/*
* @ngInject
*/
function AriaService($$rAF, $log, $window, $interpolate) {
function MdAriaService($$rAF, $log, $window, $interpolate) {

return {
expect: expect,
Expand All @@ -32,7 +81,7 @@ function AriaService($$rAF, $log, $window, $interpolate) {
defaultValue = angular.isString(defaultValue) ? defaultValue.trim() : '';
if (defaultValue.length) {
element.attr(attrName, defaultValue);
} else {
} else if (showWarnings) {
$log.warn('ARIA: Attribute "', attrName, '", required for accessibility, is missing on node:', node);
}

Expand Down
27 changes: 24 additions & 3 deletions src/core/services/aria/aria.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
describe('$mdAria service', function() {
beforeEach(module('material.core'));

describe('expecting attributes', function(){
var $mdAriaProvider;

beforeEach(module('material.core', function($injector) {
$mdAriaProvider = $injector.get('$mdAriaProvider');
}));

describe('expecting attributes', function() {
it('should warn if an invalid element is specified', inject(function($compile, $rootScope, $log, $mdAria) {
spyOn($log, 'warn');
var target = $compile('<div></div>')($rootScope);
Expand All @@ -28,7 +33,23 @@ describe('$mdAria service', function() {
expect($log.warn).toHaveBeenCalled();
}));

it('should warn if element is emtpry attribute', inject(function($compile, $rootScope, $log, $mdAria) {
it('should not warn if warnings are disabled', inject(function($compile, $rootScope, $log, $mdAria) {
spyOn($log, 'warn');
var button = $compile('<button aria-label><md-icon></md-icon></button>')($rootScope);

$mdAriaProvider.disableWarnings();

// We are able to use the injected $mdAria service, because the service reads its option from
// a global variable.
$mdAria.expect(button, 'aria-label');

expect($log.warn).toHaveBeenCalled();

// Reset our provider, because it will be used for the upcoming tests as well.
$mdAriaProvider.disableWarnings(false);
}));

it('should warn if element is empty attribute', inject(function($compile, $rootScope, $log, $mdAria) {
spyOn($log, 'warn');
var button = $compile('<button aria-label=""><md-icon></md-icon></button>')($rootScope);

Expand Down

0 comments on commit b4b265b

Please sign in to comment.