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