1
1
import { MutationObserverFactory } from '@angular/cdk/observers' ;
2
+ import { Overlay } from '@angular/cdk/overlay' ;
3
+ import { ComponentPortal } from '@angular/cdk/portal' ;
2
4
import { Component } from '@angular/core' ;
3
5
import { ComponentFixture , fakeAsync , flush , inject , TestBed , tick } from '@angular/core/testing' ;
4
6
import { By } from '@angular/platform-browser' ;
@@ -12,24 +14,24 @@ import {
12
14
13
15
describe ( 'LiveAnnouncer' , ( ) => {
14
16
let announcer : LiveAnnouncer ;
17
+ let overlay : Overlay ;
15
18
let ariaLiveElement : Element ;
16
19
let fixture : ComponentFixture < TestApp > ;
17
20
18
21
describe ( 'with default element' , ( ) => {
19
22
beforeEach ( ( ) =>
20
23
TestBed . configureTestingModule ( {
21
24
imports : [ A11yModule ] ,
22
- declarations : [ TestApp ] ,
25
+ declarations : [ TestApp , TestModal ] ,
23
26
} ) ,
24
27
) ;
25
28
26
- beforeEach ( fakeAsync (
27
- inject ( [ LiveAnnouncer ] , ( la : LiveAnnouncer ) => {
28
- announcer = la ;
29
- ariaLiveElement = getLiveElement ( ) ;
30
- fixture = TestBed . createComponent ( TestApp ) ;
31
- } ) ,
32
- ) ) ;
29
+ beforeEach ( fakeAsync ( ( ) => {
30
+ overlay = TestBed . inject ( Overlay ) ;
31
+ announcer = TestBed . inject ( LiveAnnouncer ) ;
32
+ ariaLiveElement = getLiveElement ( ) ;
33
+ fixture = TestBed . createComponent ( TestApp ) ;
34
+ } ) ) ;
33
35
34
36
it ( 'should correctly update the announce text' , fakeAsync ( ( ) => {
35
37
let buttonElement = fixture . debugElement . query ( By . css ( 'button' ) ) ! . nativeElement ;
@@ -172,6 +174,49 @@ describe('LiveAnnouncer', () => {
172
174
// Since we're testing whether the timeouts were flushed, we don't need any
173
175
// assertions here. `fakeAsync` will fail the test if a timer was left over.
174
176
} ) ) ;
177
+
178
+ it ( 'should add aria-owns to open aria-modal elements' , fakeAsync ( ( ) => {
179
+ const portal = new ComponentPortal ( TestModal ) ;
180
+ const overlayRef = overlay . create ( ) ;
181
+ const componentRef = overlayRef . attach ( portal ) ;
182
+ const modal = componentRef . location . nativeElement ;
183
+ fixture . detectChanges ( ) ;
184
+
185
+ expect ( ariaLiveElement . id ) . toBeTruthy ( ) ;
186
+ expect ( modal . hasAttribute ( 'aria-owns' ) ) . toBe ( false ) ;
187
+
188
+ announcer . announce ( 'Hey Google' , 'assertive' ) ;
189
+ tick ( 100 ) ;
190
+ expect ( modal . getAttribute ( 'aria-owns' ) ) . toBe ( ariaLiveElement . id ) ;
191
+
192
+ // Verify that the ID isn't duplicated.
193
+ announcer . announce ( 'Hey Google again' , 'assertive' ) ;
194
+ tick ( 100 ) ;
195
+ expect ( modal . getAttribute ( 'aria-owns' ) ) . toBe ( ariaLiveElement . id ) ;
196
+ } ) ) ;
197
+
198
+ it ( 'should expand aria-owns of open aria-modal elements' , fakeAsync ( ( ) => {
199
+ const portal = new ComponentPortal ( TestModal ) ;
200
+ const overlayRef = overlay . create ( ) ;
201
+ const componentRef = overlayRef . attach ( portal ) ;
202
+ const modal = componentRef . location . nativeElement ;
203
+ fixture . detectChanges ( ) ;
204
+
205
+ componentRef . instance . ariaOwns = 'foo bar' ;
206
+ componentRef . changeDetectorRef . detectChanges ( ) ;
207
+
208
+ expect ( ariaLiveElement . id ) . toBeTruthy ( ) ;
209
+ expect ( modal . getAttribute ( 'aria-owns' ) ) . toBe ( 'foo bar' ) ;
210
+
211
+ announcer . announce ( 'Hey Google' , 'assertive' ) ;
212
+ tick ( 100 ) ;
213
+ expect ( modal . getAttribute ( 'aria-owns' ) ) . toBe ( `foo bar ${ ariaLiveElement . id } ` ) ;
214
+
215
+ // Verify that the ID isn't duplicated.
216
+ announcer . announce ( 'Hey Google again' , 'assertive' ) ;
217
+ tick ( 100 ) ;
218
+ expect ( modal . getAttribute ( 'aria-owns' ) ) . toBe ( `foo bar ${ ariaLiveElement . id } ` ) ;
219
+ } ) ) ;
175
220
} ) ;
176
221
177
222
describe ( 'with a custom element' , ( ) => {
@@ -359,6 +404,11 @@ class TestApp {
359
404
}
360
405
}
361
406
407
+ @Component ( { template : '' , host : { '[attr.aria-owns]' : 'ariaOwns' , 'aria-modal' : 'true' } } )
408
+ class TestModal {
409
+ ariaOwns : string | null = null ;
410
+ }
411
+
362
412
@Component ( {
363
413
template : `
364
414
<div
0 commit comments