@@ -60,44 +60,101 @@ describe('MatSnackBar', () => {
60
60
testViewContainerRef = viewContainerFixture . componentInstance . childViewContainer ;
61
61
} ) ;
62
62
63
- it ( 'should have the role of `alert` with an `assertive` politeness if no announcement message ' +
64
- 'is provided' , ( ) => {
63
+ it ( 'should open with content first in the non-live region' , ( ) => {
64
+ snackBar . open ( 'Snack time!' , 'Chew' ) ;
65
+ viewContainerFixture . detectChanges ( ) ;
66
+
67
+ const containerElement = overlayContainerElement . querySelector ( 'mat-mdc-snack-bar-container' ) ! ;
68
+ const nonLiveElement = containerElement . querySelector ( '.mat-mdc-snack-bar-container-non-live' ) ! ;
69
+
70
+ expect ( nonLiveElement . getAttribute ( 'aria-hidden' ) )
71
+ . toBe ( 'true' , 'Expected the non-live region to be aria-hidden' ) ;
72
+ expect ( nonLiveElement . textContent ) . toContain ( 'Snack time!' ,
73
+ 'Expected non-live region to contain the snack bar content' ) ;
74
+
75
+ const liveElement = containerElement . querySelector ( '.mat-mdc-snack-bar-container-live' ) ! ;
76
+ expect ( liveElement . childNodes . length )
77
+ . toBe ( 0 , 'Expected live region to not contain any content' ) ;
78
+ } ) ;
79
+
80
+ it ( 'should move content to the live region after 100ms' , fakeAsync ( ( ) => {
81
+ snackBar . open ( 'Snack time!' , 'Chew' ) ;
82
+ viewContainerFixture . detectChanges ( ) ;
83
+
84
+ const containerElement = overlayContainerElement . querySelector ( 'mat-mdc-snack-bar-container' ) ! ;
85
+ const liveElement = containerElement . querySelector ( '.mat-mdc-snack-bar-container-live' ) ! ;
86
+ tick ( 100 ) ;
87
+
88
+ expect ( liveElement . textContent ) . toContain ( 'Snack time!' ,
89
+ 'Expected live region to contain the snack bar content' ) ;
90
+
91
+ const nonLiveElement = containerElement . querySelector ( '.mat-mdc-snack-bar-container-non-live' ) ! ;
92
+ expect ( nonLiveElement . childNodes . length )
93
+ . toBe ( 0 , 'Expected non-live region to not contain any content' ) ;
94
+ flush ( ) ;
95
+ } ) ) ;
96
+
97
+ it ( 'should preserve focus when moving content to the live region' , fakeAsync ( ( ) => {
98
+ snackBar . open ( 'Snack time!' , 'Chew' ) ;
99
+ viewContainerFixture . detectChanges ( ) ;
100
+
101
+ const actionButton = overlayContainerElement
102
+ . querySelector ( '.mat-mdc-simple-snack-bar .mat-mdc-snack-bar-action' ) ! as HTMLElement ;
103
+ actionButton . focus ( ) ;
104
+ tick ( 100 ) ;
105
+
106
+ expect ( document . activeElement )
107
+ . toBe ( actionButton , 'Expected the focus to remain on the action button' ) ;
108
+ flush ( ) ;
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 ( '.mat-mdc-snack-bar-container-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 ( '.mat-mdc-snack-bar-container-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 ( '.mat-mdc-snack-bar-container-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 ( '.mat-mdc-snack-bar-container-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