Skip to content

Commit 4f078ec

Browse files
committed
Add an aria-label to the close button and simplify the testing setup.
1 parent e7712cb commit 4f078ec

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

src/lib/dialog/dialog-content-directives.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Directive} from '@angular/core';
1+
import {Directive, Input} from '@angular/core';
22
import {MdDialogRef} from './dialog-ref';
33

44

@@ -8,10 +8,14 @@ import {MdDialogRef} from './dialog-ref';
88
@Directive({
99
selector: 'button[md-dialog-close], button[mat-dialog-close]',
1010
host: {
11-
'(click)': 'dialogRef.close()'
11+
'(click)': 'dialogRef.close()',
12+
'[attr.aria-label]': 'ariaLabel'
1213
}
1314
})
1415
export class MdDialogClose {
16+
/** Screenreader label for the button. */
17+
@Input('aria-label') ariaLabel: string = 'Close dialog';
18+
1519
constructor(public dialogRef: MdDialogRef<any>) { }
1620
}
1721

src/lib/dialog/dialog.spec.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -311,41 +311,48 @@ describe('MdDialog', () => {
311311
});
312312

313313
describe('dialog content elements', () => {
314-
let fixture: ComponentFixture<ContentElementDialog>;
315-
let dialogElement: HTMLElement;
314+
let dialogRef: MdDialogRef<ContentElementDialog>;
316315

317316
beforeEach(() => {
318-
fixture = TestBed.createComponent(ContentElementDialog);
319-
dialogElement = fixture.debugElement.nativeElement;
320-
dialog.open(ContentElementDialog);
321-
fixture.detectChanges();
317+
dialogRef = dialog.open(ContentElementDialog);
318+
viewContainerFixture.detectChanges();
322319
});
323320

324-
it('close the dialog when clicking on the close button', () => {
321+
it('should close the dialog when clicking on the close button', () => {
325322
expect(overlayContainerElement.querySelectorAll('.md-dialog-container').length).toBe(1);
326323

327-
(dialogElement.querySelector('button[md-dialog-close]') as HTMLElement).click();
324+
(overlayContainerElement.querySelector('button[md-dialog-close]') as HTMLElement).click();
328325

329326
expect(overlayContainerElement.querySelectorAll('.md-dialog-container').length).toBe(0);
330327
});
331328

332-
it('close not close the dialog if [md-dialog-close] is applied on a non-button node', () => {
329+
it('should not close the dialog if [md-dialog-close] is applied on a non-button node', () => {
333330
expect(overlayContainerElement.querySelectorAll('.md-dialog-container').length).toBe(1);
334331

335-
(dialogElement.querySelector('div[md-dialog-close]') as HTMLElement).click();
332+
(overlayContainerElement.querySelector('div[md-dialog-close]') as HTMLElement).click();
336333

337334
expect(overlayContainerElement.querySelectorAll('.md-dialog-container').length).toBe(1);
338335
});
339336

337+
it('should allow for a user-specified aria-label on the close button', () => {
338+
let button = overlayContainerElement.querySelector('button[md-dialog-close]');
339+
340+
dialogRef.componentInstance.closeButtonAriaLabel = 'Best close button ever';
341+
viewContainerFixture.detectChanges();
342+
343+
expect(button.getAttribute('aria-label')).toBe('Best close button ever');
344+
});
345+
340346
it('should add a role to the dialog title', () => {
341-
let header = dialogElement.querySelector('[md-dialog-title]');
347+
let header = overlayContainerElement.querySelector('[md-dialog-title]');
342348
expect(header.getAttribute('role')).toBe('heading');
343349
});
344350

345351
it('should add a role to the dialog content', () => {
346-
let content = dialogElement.querySelector('md-dialog-content');
352+
let content = overlayContainerElement.querySelector('md-dialog-content');
347353
expect(content.getAttribute('role')).toBe('main');
348354
});
355+
349356
});
350357
});
351358

@@ -378,12 +385,14 @@ class PizzaMsg {
378385
<h1 md-dialog-title>This is the title</h1>
379386
<md-dialog-content>Lorem ipsum dolor sit amet.</md-dialog-content>
380387
<md-dialog-actions>
381-
<button md-dialog-close>Close</button>
388+
<button md-dialog-close [aria-label]="closeButtonAriaLabel">Close</button>
382389
<div md-dialog-close>Should not close</div>
383390
</md-dialog-actions>
384391
`
385392
})
386-
class ContentElementDialog {}
393+
class ContentElementDialog {
394+
closeButtonAriaLabel: string;
395+
}
387396

388397
// Create a real (non-test) NgModule as a workaround for
389398
// https://github.com/angular/angular/issues/10760

0 commit comments

Comments
 (0)