@@ -34,6 +34,8 @@ describe('MatSnackBar', () => {
34
34
let simpleMessage = 'Burritos are here!' ;
35
35
let simpleActionLabel = 'pickup' ;
36
36
37
+ const announceDelay = 150 ;
38
+
37
39
beforeEach ( fakeAsync ( ( ) => {
38
40
TestBed . configureTestingModule ( {
39
41
imports : [ MatSnackBarModule , SnackBarTestModule , NoopAnimationsModule ] ,
@@ -60,44 +62,99 @@ describe('MatSnackBar', () => {
60
62
testViewContainerRef = viewContainerFixture . componentInstance . childViewContainer ;
61
63
} ) ;
62
64
63
- it ( 'should have the role of `alert` with an `assertive` politeness if no announcement message ' +
64
- 'is provided' , ( ) => {
65
+ it ( 'should open with content first in the inert region' , ( ) => {
66
+ snackBar . open ( 'Snack time!' , 'Chew' ) ;
67
+ viewContainerFixture . detectChanges ( ) ;
68
+
69
+ const containerElement = overlayContainerElement . querySelector ( 'mat-mdc-snack-bar-container' ) ! ;
70
+ const inertElement = containerElement . querySelector ( '[aria-hidden]' ) ! ;
71
+
72
+ expect ( inertElement . getAttribute ( 'aria-hidden' ) )
73
+ . toBe ( 'true' , 'Expected the non-live region to be aria-hidden' ) ;
74
+ expect ( inertElement . textContent ) . toContain ( 'Snack time!' ,
75
+ 'Expected non-live region to contain the snack bar content' ) ;
76
+
77
+ const liveElement = containerElement . querySelector ( '[aria-live]' ) ! ;
78
+ expect ( liveElement . childNodes . length )
79
+ . toBe ( 0 , 'Expected live region to not contain any content' ) ;
80
+ } ) ;
81
+
82
+ it ( 'should move content to the live region after 100ms' , fakeAsync ( ( ) => {
83
+ snackBar . open ( 'Snack time!' , 'Chew' ) ;
84
+ viewContainerFixture . detectChanges ( ) ;
85
+
86
+ const containerElement = overlayContainerElement . querySelector ( 'mat-mdc-snack-bar-container' ) ! ;
87
+ const liveElement = containerElement . querySelector ( '[aria-live]' ) ! ;
88
+ tick ( announceDelay ) ;
89
+
90
+ expect ( liveElement . textContent ) . toContain ( 'Snack time!' ,
91
+ 'Expected live region to contain the snack bar content' ) ;
92
+
93
+ const inertElement = containerElement . querySelector ( '[aria-hidden]' ) ! ;
94
+ expect ( inertElement ) . toBeFalsy ( 'Expected non-live region to not contain any content' ) ;
95
+ flush ( ) ;
96
+ } ) ) ;
97
+
98
+ it ( 'should preserve focus when moving content to the live region' , fakeAsync ( ( ) => {
99
+ snackBar . open ( 'Snack time!' , 'Chew' ) ;
100
+ viewContainerFixture . detectChanges ( ) ;
101
+
102
+ const actionButton = overlayContainerElement
103
+ . querySelector ( '.mat-mdc-simple-snack-bar .mat-mdc-snack-bar-action' ) ! as HTMLElement ;
104
+ actionButton . focus ( ) ;
105
+ flush ( ) ;
106
+
107
+ expect ( document . activeElement )
108
+ . toBe ( actionButton , 'Expected the focus to remain on the action button' ) ;
109
+ } ) ) ;
110
+
111
+ it ( 'should have aria-live of `assertive` with an `assertive` politeness if no announcement ' +
112
+ 'message is provided' , ( ) => {
65
113
snackBar . openFromComponent ( BurritosNotification ,
66
114
{ announcementMessage : '' , politeness : 'assertive' } ) ;
67
115
68
116
viewContainerFixture . detectChanges ( ) ;
69
117
70
118
const containerElement = overlayContainerElement . querySelector ( 'mat-mdc-snack-bar-container' ) ! ;
71
- expect ( containerElement . getAttribute ( 'role' ) )
72
- . toBe ( 'alert' , 'Expected snack bar container to have role="alert"' ) ;
119
+ const liveElement = containerElement . querySelector ( '[aria-live]' ) ! ;
120
+
121
+ expect ( liveElement . getAttribute ( 'aria-live' ) ) . toBe ( 'assertive' ,
122
+ 'Expected snack bar container live region to have aria-live="assertive"' ) ;
73
123
} ) ;
74
124
75
- it ( 'should have the role of `status ` with an `assertive` politeness if an announcement message ' +
76
- 'is provided' , ( ) => {
125
+ it ( 'should have aria-live of `polite ` with an `assertive` politeness if an announcement ' +
126
+ 'message is provided' , ( ) => {
77
127
snackBar . openFromComponent ( BurritosNotification ,
78
128
{ announcementMessage : 'Yay Burritos' , politeness : 'assertive' } ) ;
79
129
viewContainerFixture . detectChanges ( ) ;
80
130
81
131
const containerElement = overlayContainerElement . querySelector ( 'mat-mdc-snack-bar-container' ) ! ;
82
- expect ( containerElement . getAttribute ( 'role' ) )
83
- . toBe ( 'status' , 'Expected snack bar container to have role="status"' ) ;
132
+ const liveElement = containerElement . querySelector ( '[aria-live]' ) ! ;
133
+
134
+ expect ( liveElement . getAttribute ( 'aria-live' ) )
135
+ . toBe ( 'polite' , 'Expected snack bar container live region to have aria-live="polite"' ) ;
84
136
} ) ;
85
137
86
- it ( 'should have the role of `status ` with a `polite` politeness' , ( ) => {
138
+ it ( 'should have aria-live of `polite ` with a `polite` politeness' , ( ) => {
87
139
snackBar . openFromComponent ( BurritosNotification , { politeness : 'polite' } ) ;
88
140
viewContainerFixture . detectChanges ( ) ;
89
141
90
142
const containerElement = overlayContainerElement . querySelector ( 'mat-mdc-snack-bar-container' ) ! ;
91
- expect ( containerElement . getAttribute ( 'role' ) )
92
- . toBe ( 'status' , 'Expected snack bar container to have role="status"' ) ;
143
+ const liveElement = containerElement . querySelector ( '[aria-live]' ) ! ;
144
+
145
+ expect ( liveElement . getAttribute ( 'aria-live' ) )
146
+ . toBe ( 'polite' , 'Expected snack bar container live region to have aria-live="polite"' ) ;
93
147
} ) ;
94
148
95
149
it ( 'should remove the role if the politeness is turned off' , ( ) => {
96
150
snackBar . openFromComponent ( BurritosNotification , { politeness : 'off' } ) ;
97
151
viewContainerFixture . detectChanges ( ) ;
98
152
99
153
const containerElement = overlayContainerElement . querySelector ( 'mat-mdc-snack-bar-container' ) ! ;
100
- expect ( containerElement . getAttribute ( 'role' ) ) . toBeFalsy ( 'Expected role to be removed' ) ;
154
+ const liveElement = containerElement . querySelector ( '[aria-live]' ) ! ;
155
+
156
+ expect ( liveElement . getAttribute ( 'aria-live' ) )
157
+ . toBe ( 'off' , 'Expected snack bar container live region to have aria-live="off"' ) ;
101
158
} ) ;
102
159
103
160
it ( 'should have exactly one MDC label element when opened through simple snack bar' , ( ) => {
@@ -197,6 +254,7 @@ describe('MatSnackBar', () => {
197
254
198
255
snackBar . open ( simpleMessage , undefined , { announcementMessage : simpleMessage } ) ;
199
256
viewContainerFixture . detectChanges ( ) ;
257
+ flush ( ) ;
200
258
201
259
expect ( overlayContainerElement . childElementCount )
202
260
. toBe ( 1 , 'Expected the overlay with the default announcement message to be added' ) ;
@@ -212,6 +270,7 @@ describe('MatSnackBar', () => {
212
270
politeness : 'assertive'
213
271
} ) ;
214
272
viewContainerFixture . detectChanges ( ) ;
273
+ flush ( ) ;
215
274
216
275
expect ( overlayContainerElement . childElementCount )
217
276
. toBe ( 1 , 'Expected the overlay with a custom `announcementMessage` to be added' ) ;
0 commit comments