Skip to content

Commit 9e849cf

Browse files
authored
chore(snackbar): clean up liveAnnouncer msg in tests (#1615)
1 parent ef4c3c9 commit 9e849cf

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/lib/core/a11y/live-announcer.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('MdLiveAnnouncer', () => {
2424
afterEach(() => {
2525
// In our tests we always remove the current live element, because otherwise we would have
2626
// multiple live elements due multiple service instantiations.
27-
ariaLiveElement.parentNode.removeChild(ariaLiveElement);
27+
announcer._removeLiveElement();
2828
});
2929

3030
it('should correctly update the announce text', fakeAsync(() => {
@@ -56,6 +56,18 @@ describe('MdLiveAnnouncer', () => {
5656
expect(ariaLiveElement.textContent).toBe('Hey Google');
5757
expect(ariaLiveElement.getAttribute('aria-live')).toBe('polite');
5858
}));
59+
60+
it('should remove the aria-live element from the DOM', fakeAsync(() => {
61+
announcer.announce('Hey Google');
62+
63+
// This flushes our 100ms timeout for the screenreaders.
64+
tick(100);
65+
66+
announcer._removeLiveElement();
67+
68+
expect(document.body.querySelector('[aria-live]'))
69+
.toBeFalsy('Expected that the aria-live element was remove from the DOM.');
70+
}));
5971
});
6072

6173
describe('with a custom element', () => {

src/lib/core/a11y/live-announcer.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ export class MdLiveAnnouncer {
4040
setTimeout(() => this._liveElement.textContent = message, 100);
4141
}
4242

43+
/** Removes the aria-live element from the DOM. */
44+
_removeLiveElement() {
45+
if (this._liveElement && this._liveElement.parentNode) {
46+
this._liveElement.parentNode.removeChild(this._liveElement);
47+
}
48+
}
49+
4350
private _createLiveElement(): Element {
4451
let liveEl = document.createElement('div');
4552

src/lib/snack-bar/snack-bar.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ import {
1212
ViewContainerRef
1313
} from '@angular/core';
1414
import {MdSnackBar, MdSnackBarModule} from './snack-bar';
15-
import {OverlayContainer} from '../core';
15+
import {OverlayContainer, MdLiveAnnouncer} from '../core';
1616
import {MdSnackBarConfig} from './snack-bar-config';
1717
import {SimpleSnackBar} from './simple-snack-bar';
1818

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

2122
describe('MdSnackBar', () => {
2223
let snackBar: MdSnackBar;
24+
let liveAnnouncer: MdLiveAnnouncer;
2325
let overlayContainerElement: HTMLElement;
2426

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

44-
beforeEach(inject([MdSnackBar], (sb: MdSnackBar) => {
46+
beforeEach(inject([MdSnackBar, MdLiveAnnouncer], (sb: MdSnackBar, la: MdLiveAnnouncer) => {
4547
snackBar = sb;
48+
liveAnnouncer = la;
4649
}));
4750

4851
afterEach(() => {
4952
overlayContainerElement.innerHTML = '';
53+
liveAnnouncer._removeLiveElement();
5054
});
5155

5256
beforeEach(() => {

0 commit comments

Comments
 (0)