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' ;
11
2
import { Component } from '@angular/core' ;
12
3
import { By } from '@angular/platform-browser' ;
13
4
import { MdLiveAnnouncer , LIVE_ANNOUNCER_ELEMENT_TOKEN } from './live-announcer' ;
14
5
6
+
15
7
describe ( '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 > ;
19
11
20
12
describe ( 'with default element' , ( ) => {
21
13
beforeEach ( ( ) => TestBed . configureTestingModule ( {
22
14
declarations : [ TestApp ] ,
23
15
providers : [ MdLiveAnnouncer ]
24
16
} ) ) ;
25
17
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
+ } ) ) ) ;
32
23
33
24
afterEach ( ( ) => {
34
25
// In our tests we always remove the current live element, because otherwise we would have
35
26
// multiple live elements due multiple service instantiations.
36
- liveEl . parentNode . removeChild ( liveEl ) ;
27
+ ariaLiveElement . parentNode . removeChild ( ariaLiveElement ) ;
37
28
} ) ;
38
29
39
30
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 ;
51
32
buttonElement . click ( ) ;
52
33
53
34
// This flushes our 100ms timeout for the screenreaders.
54
35
tick ( 100 ) ;
55
36
56
- expect ( liveEl . textContent ) . toBe ( 'Test' ) ;
37
+ expect ( ariaLiveElement . textContent ) . toBe ( 'Test' ) ;
57
38
} ) ) ;
58
39
59
40
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' ) ;
69
42
70
43
// This flushes our 100ms timeout for the screenreaders.
71
44
tick ( 100 ) ;
72
45
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' ) ;
75
48
} ) ) ;
76
49
77
50
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' ) ;
87
52
88
53
// This flushes our 100ms timeout for the screenreaders.
89
54
tick ( 100 ) ;
90
55
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' ) ;
93
58
} ) ) ;
94
59
} ) ;
95
60
@@ -99,48 +64,43 @@ describe('MdLiveAnnouncer', () => {
99
64
beforeEach ( ( ) => {
100
65
customLiveElement = document . createElement ( 'div' ) ;
101
66
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
+ } ) ;
106
74
} ) ;
107
75
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 ( ) ;
113
79
} ) ) ;
114
80
115
81
116
82
it ( 'should allow to use a custom live element' , fakeAsync ( ( ) => {
117
- live . announce ( 'Custom Element' ) ;
83
+ announcer . announce ( 'Custom Element' ) ;
118
84
119
85
// This flushes our 100ms timeout for the screenreaders.
120
86
tick ( 100 ) ;
121
87
122
88
expect ( customLiveElement . textContent ) . toBe ( 'Custom Element' ) ;
123
89
} ) ) ;
124
90
} ) ;
125
-
126
91
} ) ;
127
92
128
93
129
94
function getLiveElement ( ) : Element {
130
95
return document . body . querySelector ( '.md-live-announcer' ) ;
131
96
}
132
97
133
- @Component ( {
134
- selector : 'test-app' ,
135
- template : `<button (click)="announceText('Test')">Announce</button>` ,
136
- } )
98
+ @Component ( { template : `<button (click)="announceText('Test')">Announce</button>` } )
137
99
class TestApp {
138
-
139
- constructor ( private live : MdLiveAnnouncer ) { } ;
100
+ constructor ( public live : MdLiveAnnouncer ) { } ;
140
101
141
102
announceText ( message : string ) {
142
103
this . live . announce ( message ) ;
143
104
}
144
-
145
105
}
146
106
0 commit comments