Skip to content

chore(snackbar): clean up liveAnnouncer msg in tests #1615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/lib/core/a11y/live-announcer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('MdLiveAnnouncer', () => {
afterEach(() => {
// In our tests we always remove the current live element, because otherwise we would have
// multiple live elements due multiple service instantiations.
ariaLiveElement.parentNode.removeChild(ariaLiveElement);
announcer._removeLiveElement();
});

it('should correctly update the announce text', fakeAsync(() => {
Expand Down Expand Up @@ -56,6 +56,18 @@ describe('MdLiveAnnouncer', () => {
expect(ariaLiveElement.textContent).toBe('Hey Google');
expect(ariaLiveElement.getAttribute('aria-live')).toBe('polite');
}));

it('should remove the aria-live element from the DOM', fakeAsync(() => {
announcer.announce('Hey Google');

// This flushes our 100ms timeout for the screenreaders.
tick(100);

announcer._removeLiveElement();

expect(document.body.querySelector('[aria-live]'))
.toBeFalsy('Expected that the aria-live element was remove from the DOM.');
}));
});

describe('with a custom element', () => {
Expand Down
7 changes: 7 additions & 0 deletions src/lib/core/a11y/live-announcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ export class MdLiveAnnouncer {
setTimeout(() => this._liveElement.textContent = message, 100);
}

/** Removes the aria-live element from the DOM. */
_removeLiveElement() {
if (this._liveElement && this._liveElement.parentNode) {
this._liveElement.parentNode.removeChild(this._liveElement);
}
}

private _createLiveElement(): Element {
let liveEl = document.createElement('div');

Expand Down
8 changes: 6 additions & 2 deletions src/lib/snack-bar/snack-bar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ import {
ViewContainerRef
} from '@angular/core';
import {MdSnackBar, MdSnackBarModule} from './snack-bar';
import {OverlayContainer} from '../core';
import {OverlayContainer, MdLiveAnnouncer} from '../core';
import {MdSnackBarConfig} from './snack-bar-config';
import {SimpleSnackBar} from './simple-snack-bar';


// TODO(josephperrott): Update tests to mock waiting for time to complete for animations.

describe('MdSnackBar', () => {
let snackBar: MdSnackBar;
let liveAnnouncer: MdLiveAnnouncer;
let overlayContainerElement: HTMLElement;

let testViewContainerRef: ViewContainerRef;
Expand All @@ -41,12 +43,14 @@ describe('MdSnackBar', () => {
TestBed.compileComponents();
}));

beforeEach(inject([MdSnackBar], (sb: MdSnackBar) => {
beforeEach(inject([MdSnackBar, MdLiveAnnouncer], (sb: MdSnackBar, la: MdLiveAnnouncer) => {
snackBar = sb;
liveAnnouncer = la;
}));

afterEach(() => {
overlayContainerElement.innerHTML = '';
liveAnnouncer._removeLiveElement();
});

beforeEach(() => {
Expand Down