From 790a1499d3bb6e754030780234130a12a3572699 Mon Sep 17 00:00:00 2001 From: Jeremy Elbourn Date: Wed, 26 Oct 2016 14:09:08 -0700 Subject: [PATCH] chore(snackbar): clean up liveAnnouncer msg in tests --- src/lib/core/a11y/live-announcer.spec.ts | 14 +++++++++++++- src/lib/core/a11y/live-announcer.ts | 7 +++++++ src/lib/snack-bar/snack-bar.spec.ts | 8 ++++++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/lib/core/a11y/live-announcer.spec.ts b/src/lib/core/a11y/live-announcer.spec.ts index 46b789d2ad25..2b0f958ee22d 100644 --- a/src/lib/core/a11y/live-announcer.spec.ts +++ b/src/lib/core/a11y/live-announcer.spec.ts @@ -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(() => { @@ -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', () => { diff --git a/src/lib/core/a11y/live-announcer.ts b/src/lib/core/a11y/live-announcer.ts index b327378d4bfc..ec7781396901 100644 --- a/src/lib/core/a11y/live-announcer.ts +++ b/src/lib/core/a11y/live-announcer.ts @@ -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'); diff --git a/src/lib/snack-bar/snack-bar.spec.ts b/src/lib/snack-bar/snack-bar.spec.ts index 8cac78af5546..9a167e12e75d 100644 --- a/src/lib/snack-bar/snack-bar.spec.ts +++ b/src/lib/snack-bar/snack-bar.spec.ts @@ -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; @@ -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(() => {