1- import {
2- inject ,
3- fakeAsync ,
4- flushMicrotasks ,
5- tick ,
6- addProviders ,
7- TestComponentBuilder ,
8- ComponentFixture ,
9- TestBed ,
10- } from '@angular/core/testing' ;
1+ import { inject , fakeAsync , tick , ComponentFixture , TestBed } from '@angular/core/testing' ;
112import { Component } from '@angular/core' ;
123import { By } from '@angular/platform-browser' ;
134import { MdLiveAnnouncer , LIVE_ANNOUNCER_ELEMENT_TOKEN } from './live-announcer' ;
145
6+
157describe ( 'MdLiveAnnouncer' , ( ) => {
16- let live : MdLiveAnnouncer ;
17- let builder : TestComponentBuilder ;
18- let liveEl : Element ;
8+ let announcer : MdLiveAnnouncer ;
9+ let ariaLiveElement : Element ;
10+ let fixture : ComponentFixture < TestApp > ;
1911
2012 describe ( 'with default element' , ( ) => {
2113 beforeEach ( ( ) => TestBed . configureTestingModule ( {
2214 declarations : [ TestApp ] ,
2315 providers : [ MdLiveAnnouncer ]
2416 } ) ) ;
2517
26- beforeEach ( fakeAsync ( inject ( [ TestComponentBuilder , MdLiveAnnouncer ] ,
27- ( tcb : TestComponentBuilder , _live : MdLiveAnnouncer ) => {
28- builder = tcb ;
29- live = _live ;
30- liveEl = getLiveElement ( ) ;
31- } ) ) ) ;
18+ beforeEach ( fakeAsync ( inject ( [ MdLiveAnnouncer ] , ( la : MdLiveAnnouncer ) => {
19+ announcer = la ;
20+ ariaLiveElement = getLiveElement ( ) ;
21+ fixture = TestBed . createComponent ( TestApp ) ;
22+ } ) ) ) ;
3223
3324 afterEach ( ( ) => {
3425 // In our tests we always remove the current live element, because otherwise we would have
3526 // multiple live elements due multiple service instantiations.
36- liveEl . parentNode . removeChild ( liveEl ) ;
27+ ariaLiveElement . parentNode . removeChild ( ariaLiveElement ) ;
3728 } ) ;
3829
3930 it ( 'should correctly update the announce text' , fakeAsync ( ( ) => {
40- let appFixture : ComponentFixture < TestApp > = null ;
41-
42- builder . createAsync ( TestApp ) . then ( fixture => {
43- appFixture = fixture ;
44- } ) ;
45-
46- flushMicrotasks ( ) ;
47-
48- let buttonElement = appFixture . debugElement
49- . query ( By . css ( 'button' ) ) . nativeElement ;
50-
31+ let buttonElement = fixture . debugElement . query ( By . css ( 'button' ) ) . nativeElement ;
5132 buttonElement . click ( ) ;
5233
5334 // This flushes our 100ms timeout for the screenreaders.
5435 tick ( 100 ) ;
5536
56- expect ( liveEl . textContent ) . toBe ( 'Test' ) ;
37+ expect ( ariaLiveElement . textContent ) . toBe ( 'Test' ) ;
5738 } ) ) ;
5839
5940 it ( 'should correctly update the politeness attribute' , fakeAsync ( ( ) => {
60- let appFixture : ComponentFixture < TestApp > = null ;
61-
62- builder . createAsync ( TestApp ) . then ( fixture => {
63- appFixture = fixture ;
64- } ) ;
65-
66- flushMicrotasks ( ) ;
67-
68- live . announce ( 'Hey Google' , 'assertive' ) ;
41+ announcer . announce ( 'Hey Google' , 'assertive' ) ;
6942
7043 // This flushes our 100ms timeout for the screenreaders.
7144 tick ( 100 ) ;
7245
73- expect ( liveEl . textContent ) . toBe ( 'Hey Google' ) ;
74- expect ( liveEl . getAttribute ( 'aria-live' ) ) . toBe ( 'assertive' ) ;
46+ expect ( ariaLiveElement . textContent ) . toBe ( 'Hey Google' ) ;
47+ expect ( ariaLiveElement . getAttribute ( 'aria-live' ) ) . toBe ( 'assertive' ) ;
7548 } ) ) ;
7649
7750 it ( 'should apply the aria-live value polite by default' , fakeAsync ( ( ) => {
78- let appFixture : ComponentFixture < TestApp > = null ;
79-
80- builder . createAsync ( TestApp ) . then ( fixture => {
81- appFixture = fixture ;
82- } ) ;
83-
84- flushMicrotasks ( ) ;
85-
86- live . announce ( 'Hey Google' ) ;
51+ announcer . announce ( 'Hey Google' ) ;
8752
8853 // This flushes our 100ms timeout for the screenreaders.
8954 tick ( 100 ) ;
9055
91- expect ( liveEl . textContent ) . toBe ( 'Hey Google' ) ;
92- expect ( liveEl . getAttribute ( 'aria-live' ) ) . toBe ( 'polite' ) ;
56+ expect ( ariaLiveElement . textContent ) . toBe ( 'Hey Google' ) ;
57+ expect ( ariaLiveElement . getAttribute ( 'aria-live' ) ) . toBe ( 'polite' ) ;
9358 } ) ) ;
9459 } ) ;
9560
@@ -99,48 +64,43 @@ describe('MdLiveAnnouncer', () => {
9964 beforeEach ( ( ) => {
10065 customLiveElement = document . createElement ( 'div' ) ;
10166
102- addProviders ( [
103- { provide : LIVE_ANNOUNCER_ELEMENT_TOKEN , useValue : customLiveElement } ,
104- MdLiveAnnouncer ,
105- ] ) ;
67+ return TestBed . configureTestingModule ( {
68+ declarations : [ TestApp ] ,
69+ providers : [
70+ { provide : LIVE_ANNOUNCER_ELEMENT_TOKEN , useValue : customLiveElement } ,
71+ MdLiveAnnouncer ,
72+ ] ,
73+ } ) ;
10674 } ) ;
10775
108- beforeEach ( inject ( [ TestComponentBuilder , MdLiveAnnouncer ] ,
109- ( tcb : TestComponentBuilder , _live : MdLiveAnnouncer ) => {
110- builder = tcb ;
111- live = _live ;
112- liveEl = getLiveElement ( ) ;
76+ beforeEach ( inject ( [ MdLiveAnnouncer ] , ( la : MdLiveAnnouncer ) => {
77+ announcer = la ;
78+ ariaLiveElement = getLiveElement ( ) ;
11379 } ) ) ;
11480
11581
11682 it ( 'should allow to use a custom live element' , fakeAsync ( ( ) => {
117- live . announce ( 'Custom Element' ) ;
83+ announcer . announce ( 'Custom Element' ) ;
11884
11985 // This flushes our 100ms timeout for the screenreaders.
12086 tick ( 100 ) ;
12187
12288 expect ( customLiveElement . textContent ) . toBe ( 'Custom Element' ) ;
12389 } ) ) ;
12490 } ) ;
125-
12691} ) ;
12792
12893
12994function getLiveElement ( ) : Element {
13095 return document . body . querySelector ( '.md-live-announcer' ) ;
13196}
13297
133- @Component ( {
134- selector : 'test-app' ,
135- template : `<button (click)="announceText('Test')">Announce</button>` ,
136- } )
98+ @Component ( { template : `<button (click)="announceText('Test')">Announce</button>` } )
13799class TestApp {
138-
139- constructor ( private live : MdLiveAnnouncer ) { } ;
100+ constructor ( public live : MdLiveAnnouncer ) { } ;
140101
141102 announceText ( message : string ) {
142103 this . live . announce ( message ) ;
143104 }
144-
145105}
146106
0 commit comments