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

Commit 2c367f7

Browse files
devversionkara
authored andcommitted
fix(list): expect aria-label with respect to aria-hidden (#9943)
* The list item no longer resolves the aria-label text from the content incorrectly (aria-hidden is ignored - b3cb84d fixed this for $mdAria) * Now shows ARIA warnings properly for list-items being clickable * Fixes incorrect tests with uncompiled buttons due to missing `buttons` module registration. Fixes #9933.
1 parent 6d06188 commit 2c367f7

File tree

2 files changed

+62
-18
lines changed

2 files changed

+62
-18
lines changed

src/components/list/list.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,8 @@ function mdListItemDirective($mdAria, $mdConstant, $mdUtil, $timeout) {
330330
'<md-button class="md-no-style"></md-button>'
331331
);
332332

333-
buttonWrap[0].setAttribute('aria-label', tEl[0].textContent);
333+
// Expect the root element to have a label set. If not set, determine the label from the text content.
334+
$mdAria.expectWithText(tEl, 'aria-label');
334335

335336
copyAttributes(tEl[0], buttonWrap[0]);
336337

src/components/list/list.spec.js

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ describe('mdListItem directive', function() {
22
var attachedElements = [];
33
var $compile, $rootScope;
44

5-
beforeEach(module('material.components.list', 'material.components.checkbox', 'material.components.switch'));
5+
beforeEach(module(
6+
'material.components.list',
7+
'material.components.checkbox',
8+
'material.components.switch',
9+
'material.components.button'
10+
));
11+
612
beforeEach(inject(function(_$compile_, _$rootScope_) {
713
$compile = _$compile_;
814
$rootScope = _$rootScope_;
@@ -295,7 +301,7 @@ describe('mdListItem directive', function() {
295301
// The actual click button will be a child of the button.md-no-style wrapper.
296302
var buttonExecutor = buttonWrap.children()[0];
297303

298-
expect(buttonExecutor.nodeName).toBe('MD-BUTTON');
304+
expect(buttonExecutor.nodeName).toBe('BUTTON');
299305
expect(buttonExecutor.getAttribute('aria-label')).toBe('Hello');
300306
});
301307

@@ -321,7 +327,7 @@ describe('mdListItem directive', function() {
321327

322328
// The secondary container should contain the md-icon,
323329
// which has been transformed to an icon button.
324-
expect(secondaryContainer.children()[0].nodeName).toBe('MD-BUTTON');
330+
expect(secondaryContainer.children()[0].nodeName).toBe('BUTTON');
325331
});
326332

327333
it('should copy ng-show to the generated button parent of a clickable secondary item', function() {
@@ -348,7 +354,7 @@ describe('mdListItem directive', function() {
348354
// which has been transformed to an icon button.
349355
var iconButton = secondaryContainer.children()[0];
350356

351-
expect(iconButton.nodeName).toBe('MD-BUTTON');
357+
expect(iconButton.nodeName).toBe('BUTTON');
352358
expect(iconButton.hasAttribute('ng-show')).toBe(true);
353359

354360
// The actual `md-icon` element, should not have the ng-show attribute anymore.
@@ -379,8 +385,8 @@ describe('mdListItem directive', function() {
379385
// The secondary container should hold the two secondary items.
380386
expect(secondaryContainer.children().length).toBe(2);
381387

382-
expect(secondaryContainer.children()[0].nodeName).toBe('MD-BUTTON');
383-
expect(secondaryContainer.children()[1].nodeName).toBe('MD-BUTTON');
388+
expect(secondaryContainer.children()[0].nodeName).toBe('BUTTON');
389+
expect(secondaryContainer.children()[1].nodeName).toBe('BUTTON');
384390
});
385391

386392
it('should not detect a normal button as a proxy element', function() {
@@ -407,7 +413,7 @@ describe('mdListItem directive', function() {
407413
'</md-list-item>'
408414
);
409415

410-
var button = listItem.find('md-button');
416+
var button = listItem.find('button');
411417

412418
expect(button[0].hasAttribute('ng-click')).toBeTruthy();
413419
expect(button[0].hasAttribute('ng-disabled')).toBeTruthy();
@@ -479,7 +485,7 @@ describe('mdListItem directive', function() {
479485

480486
var listItem = setup(template);
481487

482-
var mdMenuButton = listItem[0].querySelector('md-menu > md-button');
488+
var mdMenuButton = listItem[0].querySelector('md-menu > button');
483489

484490
expect(mdMenuButton.getAttribute('aria-label')).toBe('Open List Menu');
485491
});
@@ -495,12 +501,50 @@ describe('mdListItem directive', function() {
495501

496502
var listItem = setup(template);
497503

498-
var mdMenuButton = listItem[0].querySelector('md-menu > md-button');
504+
var mdMenuButton = listItem[0].querySelector('md-menu > button');
499505

500506
expect(mdMenuButton.getAttribute('ng-click')).toBe('$mdMenu.open($event)');
501507
});
502508
});
503509

510+
describe('aria-label', function() {
511+
512+
it('should copy label to the button executor element', function() {
513+
var listItem = setup('<md-list-item ng-click="null" aria-label="Test">');
514+
var buttonEl = listItem.find('button');
515+
516+
// The aria-label attribute should be moved to the button element.
517+
expect(buttonEl.attr('aria-label')).toBe('Test');
518+
expect(listItem.attr('aria-label')).toBeFalsy();
519+
});
520+
521+
it('should determine the label from the content if not set', function() {
522+
var listItem = setup(
523+
'<md-list-item ng-click="null">' +
524+
'<span>Content</span>' +
525+
'<span aria-hidden="true">Hidden</span>' +
526+
'</md-list-item>'
527+
);
528+
529+
var buttonEl = listItem.find('button');
530+
531+
// The aria-label attribute should be determined from the content.
532+
expect(buttonEl.attr('aria-label')).toBe('Content');
533+
});
534+
535+
it('should warn when label is missing and content is empty', inject(function($log) {
536+
// Clear the log stack to assert that a new warning has been added.
537+
$log.reset();
538+
539+
setup('<md-list-item ng-click="null">');
540+
541+
// Expect $log to have two warnings. A warning for the md-list-item and a second one for the later compiled
542+
// button executor.
543+
expect($log.warn.logs.length).toBe(2);
544+
}));
545+
546+
});
547+
504548
describe('with a clickable item', function() {
505549

506550
it('should wrap secondary icons in a md-button', function() {
@@ -511,10 +555,10 @@ describe('mdListItem directive', function() {
511555
'</md-list-item>'
512556
);
513557

514-
var buttons = listItem.find('md-button');
558+
var buttons = listItem.find('button');
515559

516560
// Check that we wrapped the icon
517-
expect(listItem[0].querySelector('md-button > md-icon')).toBeTruthy();
561+
expect(listItem[0].querySelector('button > md-icon')).toBeTruthy();
518562

519563
// Check the button actions
520564
expect(buttons[0].attributes['ng-click'].value).toBe("something()");
@@ -529,9 +573,8 @@ describe('mdListItem directive', function() {
529573
'</md-list-item>'
530574
);
531575

532-
// There should only be 1 md-button (the wrapper) and one button (the unwrapped one)
533-
expect(listItem.find('md-button').length).toBe(1);
534-
expect(listItem.find('button').length).toBe(1);
576+
// There should be two buttons (the button executor, and the secondary item)
577+
expect(listItem.find('button').length).toBe(2);
535578

536579
// Check that we didn't wrap the button in an md-button
537580
expect(listItem[0].querySelector('md-button button.md-secondary')).toBeFalsy();
@@ -545,11 +588,11 @@ describe('mdListItem directive', function() {
545588
'</md-list-item>'
546589
);
547590

548-
// There should be 2 md-buttons that are siblings
549-
expect(listItem.find('md-button').length).toBe(2);
591+
// There should be 2 buttons that are siblings
592+
expect(listItem.find('button').length).toBe(2);
550593

551594
// Check that we didn't wrap the md-button in an md-button
552-
expect(listItem[0].querySelector('md-button md-button.md-secondary')).toBeFalsy();
595+
expect(listItem[0].querySelector('button.md-button button.md-button.md-secondary')).toBeFalsy();
553596
});
554597
});
555598

0 commit comments

Comments
 (0)