Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 5716340

Browse files
fix(button): fix aria-label async injection and tests
1 parent 6113648 commit 5716340

File tree

5 files changed

+49
-51
lines changed

5 files changed

+49
-51
lines changed

src/components/button/button.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ angular
6161
* </md-button>
6262
* </hljs>
6363
*/
64-
function MdButtonDirective($mdButtonInkRipple, $mdTheming, $mdAria, $timeout, $interpolate) {
64+
function MdButtonDirective($mdButtonInkRipple, $mdTheming, $mdAria, $timeout) {
6565

6666
return {
6767
restrict: 'EA',

src/components/button/button.spec.js

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
fdescribe('md-button', function() {
1+
describe('md-button', function() {
22

33
beforeEach(module('material.components.button'));
44

@@ -9,47 +9,50 @@ fdescribe('md-button', function() {
99
expect(button[0]).toHaveClass('hide-sm');
1010
}));
1111

12-
it('should only have one ripple container when a custom ripple color is set', inject(function ($compile, $rootScope, $timeout) {
13-
var button = $compile('<md-button md-ink-ripple="#f00">button</md-button>')($rootScope);
12+
describe('with ARIA support', function() {
1413

15-
button.triggerHandler({ type: '$md.pressdown', pointer: { x: 0, y: 0 } });
16-
expect(button[0].getElementsByClassName('md-ripple-container').length).toBe(0);
17-
}));
14+
it('should only have one ripple container when a custom ripple color is set', inject(function ($compile, $rootScope) {
15+
var button = $compile('<md-button md-ink-ripple="#f00">button</md-button>')($rootScope);
1816

17+
button.triggerHandler({ type: '$md.pressdown', pointer: { x: 0, y: 0 } });
18+
expect(button[0].getElementsByClassName('md-ripple-container').length).toBe(0);
19+
}));
1920

20-
it('should expect an aria-label if element has no text', inject(function($compile, $rootScope, $log) {
21-
spyOn($log, 'warn');
22-
var button = $compile('<md-button><md-icon></md-icon></md-button>')($rootScope);
23-
$rootScope.$apply();
24-
expect($log.warn).toHaveBeenCalled();
2521

26-
$log.warn.calls.reset();
27-
button = $compile('<md-button aria-label="something"><md-icon></md-icon></md-button>')($rootScope);
28-
$rootScope.$apply();
29-
expect($log.warn).not.toHaveBeenCalled();
30-
}));
22+
it('should expect an aria-label if element has no text', inject(function($compile, $rootScope, $log) {
23+
spyOn($log, 'warn');
24+
var button = $compile('<md-button><md-icon></md-icon></md-button>')($rootScope);
25+
$rootScope.$apply();
26+
expect($log.warn).toHaveBeenCalled();
27+
28+
$log.warn.calls.reset();
29+
button = $compile('<md-button aria-label="something"><md-icon></md-icon></md-button>')($rootScope);
30+
$rootScope.$apply();
31+
expect($log.warn).not.toHaveBeenCalled();
32+
}));
3133

32-
it('should expect an aria-label if element has text content', inject(function($compile, $rootScope, $log) {
33-
spyOn($log, 'warn');
34+
it('should expect an aria-label if element has text content', inject(function($compile, $rootScope, $log) {
35+
spyOn($log, 'warn');
3436

35-
var button = $compile('<md-button>Hello</md-button>')($rootScope);
36-
expect(button.attr('aria-label')).toBe("Hello");
37-
expect($log.warn).not.toHaveBeenCalled();
38-
}));
37+
var button = $compile('<md-button>Hello</md-button>')($rootScope);
38+
expect(button.attr('aria-label')).toBe("Hello");
39+
expect($log.warn).not.toHaveBeenCalled();
40+
}));
3941

40-
it('should set an aria-label if the text content using bindings', inject(function($$rAF, $compile, $rootScope, $log) {
41-
spyOn($log, 'warn');
42+
it('should set an aria-label if the text content using bindings', inject(function($$rAF, $compile, $rootScope, $log, $timeout) {
43+
spyOn($log, 'warn');
4244

43-
var scope = angular.extend($rootScope.$new(),{greetings : "Welcome"});
44-
var button = $compile('<md-button>{{greetings}}</md-button>')(scope);
45+
var scope = angular.extend($rootScope.$new(),{greetings : "Welcome"});
46+
var button = $compile('<md-button>{{greetings}}</md-button>')(scope);
4547

46-
$rootScope.$apply();
47-
$$rAF.flush(); // needed for $mdAria.expectAsync()
48+
$rootScope.$apply();
49+
$$rAF.flush(); // needed for $mdAria.expectAsync()
4850

49-
expect(button.attr('aria-label')).toBe("Welcome");
50-
expect($log.warn).not.toHaveBeenCalled();
51-
}));
51+
expect(button.attr('aria-label')).toBe("Welcome");
52+
expect($log.warn).not.toHaveBeenCalled();
53+
}));
5254

55+
});
5356

5457
it('should allow attribute directive syntax', inject(function($compile, $rootScope) {
5558
var button = $compile('<a md-button href="https://google.com">google</a>')($rootScope.$new());

src/components/tooltip/tooltip.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('<md-tooltip> directive', function() {
4141
'</md-button>'
4242
);
4343

44-
expect(element.attr('aria-label')).toBeUndefined();
44+
expect(element.attr('aria-label')).toBe("Hello");
4545
});
4646

4747
it('should label parent', function(){

src/core/services/aria/aria.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ angular.module('material.core')
55
/*
66
* @ngInject
77
*/
8-
function AriaService($$rAF, $log, $window) {
8+
function AriaService($$rAF, $log, $window, $interpolate) {
99

1010
return {
1111
expect: expect,
@@ -44,18 +44,25 @@ function AriaService($$rAF, $log, $window) {
4444
// the text may not be defined yet in the case of a binding.
4545
// There is a higher chance that a binding will be defined if we wait one frame.
4646
$$rAF(function() {
47-
expect(element, attrName, defaultValueGetter());
47+
expect(element, attrName, defaultValueGetter());
4848
});
4949
}
5050

5151
function expectWithText(element, attrName) {
52-
expectAsync(element, attrName, function() {
53-
return getText(element);
54-
});
52+
var content = getText(element) || "";
53+
var hasBinding = content.indexOf($interpolate.startSymbol())>-1;
54+
55+
if ( hasBinding ) {
56+
expectAsync(element, attrName, function() {
57+
return getText(element);
58+
});
59+
} else {
60+
expect(element, attrName, content);
61+
}
5562
}
5663

5764
function getText(element) {
58-
return element.text().trim();
65+
return (element.text() || "").trim();
5966
}
6067

6168
function childHasAttribute(node, attrName) {

test/angular-material-mocks.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,6 @@ angular.module('ngMaterial-mock', [
6363
*/
6464
$provide.constant('$MD_THEME_CSS', '/**/');
6565

66-
/**
67-
* Intercept to make .expectWithText() to be synchronous
68-
*/
69-
$provide.decorator('$mdAria', function($delegate){
70-
71-
$delegate.expectWithText = function(element, attrName){
72-
$delegate.expect(element, attrName, element.text().trim());
73-
};
74-
75-
return $delegate;
76-
});
77-
7866
/**
7967
* Add throttle() and wrap .flush() to catch `no callbacks present`
8068
* errors

0 commit comments

Comments
 (0)