11import { A11yModule , CDK_DESCRIBEDBY_HOST_ATTRIBUTE } from '../index' ;
22import { AriaDescriber , MESSAGES_CONTAINER_ID } from './aria-describer' ;
3- import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
4- import { Component , ElementRef , ViewChild } from '@angular/core' ;
3+ import { ComponentFixture , TestBed } from '@angular/core/testing' ;
4+ import { Component , ElementRef , ViewChild , Provider } from '@angular/core' ;
5+ import { Platform } from '@angular/cdk/platform' ;
56
67describe ( 'AriaDescriber' , ( ) => {
78 let ariaDescriber : AriaDescriber ;
89 let component : TestApp ;
910 let fixture : ComponentFixture < TestApp > ;
1011
11- beforeEach ( async ( ( ) => {
12+ function createFixture ( providers : Provider [ ] = [ ] ) {
1213 TestBed . configureTestingModule ( {
1314 imports : [ A11yModule ] ,
1415 declarations : [ TestApp ] ,
15- providers : [ AriaDescriber ] ,
16+ providers : [ AriaDescriber , ... providers ] ,
1617 } ) . compileComponents ( ) ;
17- } ) ) ;
1818
19- beforeEach ( ( ) => {
2019 fixture = TestBed . createComponent ( TestApp ) ;
2120 component = fixture . componentInstance ;
2221 ariaDescriber = component . ariaDescriber ;
23- } ) ;
22+ }
2423
2524 afterEach ( ( ) => {
2625 ariaDescriber . ngOnDestroy ( ) ;
2726 } ) ;
2827
2928 it ( 'should initialize without the message container' , ( ) => {
29+ createFixture ( ) ;
3030 expect ( getMessagesContainer ( ) ) . toBeNull ( ) ;
3131 } ) ;
3232
3333 it ( 'should be able to create a message element' , ( ) => {
34+ createFixture ( ) ;
3435 ariaDescriber . describe ( component . element1 , 'My Message' ) ;
3536 expectMessages ( [ 'My Message' ] ) ;
3637 } ) ;
3738
39+ it ( 'should hide the message container' , ( ) => {
40+ createFixture ( ) ;
41+ ariaDescriber . describe ( component . element1 , 'My Message' ) ;
42+ expect ( getMessagesContainer ( ) . classList ) . toContain ( 'cdk-visually-hidden' ) ;
43+ } ) ;
44+
3845 it ( 'should not register empty strings' , ( ) => {
46+ createFixture ( ) ;
3947 ariaDescriber . describe ( component . element1 , '' ) ;
4048 expect ( getMessageElements ( ) ) . toBe ( null ) ;
4149 } ) ;
4250
4351 it ( 'should not register non-string values' , ( ) => {
52+ createFixture ( ) ;
4453 expect ( ( ) => ariaDescriber . describe ( component . element1 , null ! ) ) . not . toThrow ( ) ;
4554 expect ( getMessageElements ( ) ) . toBe ( null ) ;
4655 } ) ;
4756
4857 it ( 'should not throw when trying to remove non-string value' , ( ) => {
58+ createFixture ( ) ;
4959 expect ( ( ) => ariaDescriber . removeDescription ( component . element1 , null ! ) ) . not . toThrow ( ) ;
5060 } ) ;
5161
5262 it ( 'should de-dupe a message registered multiple times' , ( ) => {
63+ createFixture ( ) ;
5364 ariaDescriber . describe ( component . element1 , 'My Message' ) ;
5465 ariaDescriber . describe ( component . element2 , 'My Message' ) ;
5566 ariaDescriber . describe ( component . element3 , 'My Message' ) ;
@@ -60,6 +71,7 @@ describe('AriaDescriber', () => {
6071 } ) ;
6172
6273 it ( 'should be able to register multiple messages' , ( ) => {
74+ createFixture ( ) ;
6375 ariaDescriber . describe ( component . element1 , 'First Message' ) ;
6476 ariaDescriber . describe ( component . element2 , 'Second Message' ) ;
6577 expectMessages ( [ 'First Message' , 'Second Message' ] ) ;
@@ -68,6 +80,7 @@ describe('AriaDescriber', () => {
6880 } ) ;
6981
7082 it ( 'should be able to unregister messages' , ( ) => {
83+ createFixture ( ) ;
7184 ariaDescriber . describe ( component . element1 , 'My Message' ) ;
7285 expectMessages ( [ 'My Message' ] ) ;
7386
@@ -87,6 +100,7 @@ describe('AriaDescriber', () => {
87100 } ) ;
88101
89102 it ( 'should be able to unregister messages while having others registered' , ( ) => {
103+ createFixture ( ) ;
90104 ariaDescriber . describe ( component . element1 , 'Persistent Message' ) ;
91105 ariaDescriber . describe ( component . element2 , 'My Message' ) ;
92106 expectMessages ( [ 'Persistent Message' , 'My Message' ] ) ;
@@ -105,24 +119,28 @@ describe('AriaDescriber', () => {
105119 } ) ;
106120
107121 it ( 'should be able to append to an existing list of aria describedby' , ( ) => {
122+ createFixture ( ) ;
108123 ariaDescriber . describe ( component . element4 , 'My Message' ) ;
109124 expectMessages ( [ 'My Message' ] ) ;
110125 expectMessage ( component . element4 , 'My Message' ) ;
111126 } ) ;
112127
113128 it ( 'should be able to handle multiple regisitrations of the same message to an element' , ( ) => {
129+ createFixture ( ) ;
114130 ariaDescriber . describe ( component . element1 , 'My Message' ) ;
115131 ariaDescriber . describe ( component . element1 , 'My Message' ) ;
116132 expectMessages ( [ 'My Message' ] ) ;
117133 expectMessage ( component . element1 , 'My Message' ) ;
118134 } ) ;
119135
120136 it ( 'should not throw when attempting to describe a non-element node' , ( ) => {
137+ createFixture ( ) ;
121138 const node : any = document . createComment ( 'Not an element node' ) ;
122139 expect ( ( ) => ariaDescriber . describe ( node , 'This looks like an element' ) ) . not . toThrow ( ) ;
123140 } ) ;
124141
125142 it ( 'should clear any pre-existing containers' , ( ) => {
143+ createFixture ( ) ;
126144 const extraContainer = document . createElement ( 'div' ) ;
127145 extraContainer . id = MESSAGES_CONTAINER_ID ;
128146 document . body . appendChild ( extraContainer ) ;
@@ -132,10 +150,28 @@ describe('AriaDescriber', () => {
132150 // Use `querySelectorAll` with an attribute since `getElementById` will stop at the first match.
133151 expect ( document . querySelectorAll ( `[id='${ MESSAGES_CONTAINER_ID } ']` ) . length ) . toBe ( 1 ) ;
134152 } ) ;
153+
154+ it ( 'should set `aria-hidden` on the container by default' , ( ) => {
155+ createFixture ( [ { provide : Platform , useValue : { BLINK : true } } ] ) ;
156+ ariaDescriber . describe ( component . element1 , 'My Message' ) ;
157+ expect ( getMessagesContainer ( ) . getAttribute ( 'aria-hidden' ) ) . toBe ( 'true' ) ;
158+ } ) ;
159+
160+ it ( 'should disable `aria-hidden` on the container in IE' , ( ) => {
161+ createFixture ( [ { provide : Platform , useValue : { TRIDENT : true } } ] ) ;
162+ ariaDescriber . describe ( component . element1 , 'My Message' ) ;
163+ expect ( getMessagesContainer ( ) . getAttribute ( 'aria-hidden' ) ) . toBe ( 'false' ) ;
164+ } ) ;
165+
166+ it ( 'should disable `aria-hidden` on the container in Edge' , ( ) => {
167+ createFixture ( [ { provide : Platform , useValue : { EDGE : true } } ] ) ;
168+ ariaDescriber . describe ( component . element1 , 'My Message' ) ;
169+ expect ( getMessagesContainer ( ) . getAttribute ( 'aria-hidden' ) ) . toBe ( 'false' ) ;
170+ } ) ;
135171} ) ;
136172
137173function getMessagesContainer ( ) {
138- return document . querySelector ( `#${ MESSAGES_CONTAINER_ID } ` ) ;
174+ return document . querySelector ( `#${ MESSAGES_CONTAINER_ID } ` ) ! ;
139175}
140176
141177function getMessageElements ( ) : Node [ ] | null {
0 commit comments