1
1
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
import React from 'react' ;
4
- import { act , fireEvent , screen , within } from '@testing-library/react' ;
4
+ import { act , fireEvent , screen , waitFor , within } from '@testing-library/react' ;
5
5
6
6
import AppLayout , { AppLayoutProps } from '../../../lib/components/app-layout' ;
7
7
import customCssProps from '../../../lib/components/internal/generated/custom-css-properties' ;
@@ -89,16 +89,18 @@ describeEachAppLayout({ sizes: ['desktop'] }, ({ theme }) => {
89
89
}
90
90
} ) ;
91
91
92
- test ( 'uses the provided content width if one is provided' , ( ) => {
92
+ test ( 'uses the provided content width if one is provided' , async ( ) => {
93
93
const { wrapper } = renderComponent ( < AppLayout minContentWidth = { 120 } maxContentWidth = { 650 } /> ) ;
94
94
95
95
if ( theme === 'classic' ) {
96
96
expect ( wrapper . findContentRegion ( ) . getElement ( ) ) . toHaveStyle ( { minWidth : '120px' , maxWidth : '650px' } ) ;
97
97
} else {
98
- const minWidthInGrid = wrapper . getElement ( ) . style . getPropertyValue ( customCssProps . minContentWidth ) ;
99
- const maxWidthInGrid = wrapper . getElement ( ) . style . getPropertyValue ( customCssProps . maxContentWidth ) ;
100
- expect ( minWidthInGrid ) . toBe ( theme === 'refresh' ? '120px' : '' ) ;
101
- expect ( maxWidthInGrid ) . toBe ( '650px' ) ;
98
+ await waitFor ( ( ) => {
99
+ const minWidthInGrid = wrapper . getElement ( ) . style . getPropertyValue ( customCssProps . minContentWidth ) ;
100
+ const maxWidthInGrid = wrapper . getElement ( ) . style . getPropertyValue ( customCssProps . maxContentWidth ) ;
101
+ expect ( minWidthInGrid ) . toBe ( theme === 'refresh' ? '120px' : '' ) ;
102
+ expect ( maxWidthInGrid ) . toBe ( '650px' ) ;
103
+ } ) ;
102
104
}
103
105
} ) ;
104
106
@@ -125,14 +127,16 @@ describeEachAppLayout({ sizes: ['desktop'] }, ({ theme }) => {
125
127
expect ( wrapper . findOpenNavigationPanel ( ) ) . toBeTruthy ( ) ;
126
128
} ) ;
127
129
128
- test ( 'Allows notifications to be sticky' , ( ) => {
130
+ test ( 'Allows notifications to be sticky' , async ( ) => {
129
131
const { wrapper } = renderComponent ( < AppLayout notifications = "Test" stickyNotifications = { true } /> ) ;
130
132
const stickyNotificationsClassName = {
131
133
classic : notificationStyles [ 'notifications-sticky' ] ,
132
134
refresh : visualRefreshStyles [ 'sticky-notifications' ] ,
133
135
'refresh-toolbar' : visualRefreshToolbarNotificationStyles [ 'sticky-notifications' ] ,
134
136
} [ theme ] ;
135
- expect ( wrapper . findByClassName ( stickyNotificationsClassName ) ) . not . toBeNull ( ) ;
137
+ await waitFor ( ( ) => {
138
+ expect ( wrapper . findByClassName ( stickyNotificationsClassName ) ) . not . toBeNull ( ) ;
139
+ } ) ;
136
140
} ) ;
137
141
138
142
describe ( 'unfocusable content' , ( ) => {
@@ -152,30 +156,37 @@ describeEachAppLayout({ sizes: ['desktop'] }, ({ theme }) => {
152
156
} ) ;
153
157
} ) ;
154
158
155
- test ( 'should render an active drawer' , ( ) => {
159
+ test ( 'should render an active drawer' , async ( ) => {
156
160
const { wrapper } = renderComponent (
157
161
< AppLayout activeDrawerId = { testDrawer . id } drawers = { [ testDrawer ] } onDrawerChange = { ( ) => { } } />
158
162
) ;
159
163
160
- expect ( wrapper . findActiveDrawer ( ) ) . toBeTruthy ( ) ;
164
+ await waitFor ( ( ) => {
165
+ expect ( wrapper . findActiveDrawer ( ) ) . toBeTruthy ( ) ;
166
+ } ) ;
161
167
} ) ;
162
168
163
- test ( `should toggle drawer on click` , ( ) => {
169
+ test ( `should toggle drawer on click` , async ( ) => {
164
170
const { wrapper } = renderComponent ( < AppLayout toolsHide = { true } drawers = { [ testDrawer ] } /> ) ;
165
- wrapper . findDrawersTriggers ( ) ! [ 0 ] . click ( ) ;
171
+ await waitFor ( ( ) => {
172
+ wrapper . findDrawersTriggers ( ) ! [ 0 ] . click ( ) ;
173
+ } ) ;
174
+
166
175
expect ( wrapper . findActiveDrawer ( ) ) . toBeTruthy ( ) ;
167
176
wrapper . findDrawersTriggers ( ) ! [ 0 ] . click ( ) ;
168
177
expect ( wrapper . findActiveDrawer ( ) ) . toBeFalsy ( ) ;
169
178
} ) ;
170
179
171
- test ( `Moves focus to slider when opened` , ( ) => {
180
+ test ( `Moves focus to slider when opened` , async ( ) => {
172
181
const { wrapper } = renderComponent ( < AppLayout drawers = { [ { ...testDrawer , resizable : true } ] } /> ) ;
173
182
174
- wrapper . findDrawerTriggerById ( 'security' ) ! . click ( ) ;
183
+ await waitFor ( ( ) => {
184
+ wrapper . findDrawerTriggerById ( 'security' ) ! . click ( ) ;
185
+ } ) ;
175
186
expect ( wrapper . findActiveDrawerResizeHandle ( ) ! . getElement ( ) ) . toHaveFocus ( ) ;
176
187
} ) ;
177
188
178
- test ( 'should change size via keyboard events on slider handle' , ( ) => {
189
+ test ( 'should change size via keyboard events on slider handle' , async ( ) => {
179
190
const onDrawerItemResize = jest . fn ( ) ;
180
191
const testDrawerResizable : AppLayoutProps . Drawer = {
181
192
...testDrawer ,
@@ -186,12 +197,14 @@ describeEachAppLayout({ sizes: ['desktop'] }, ({ theme }) => {
186
197
const { wrapper } = renderComponent (
187
198
< AppLayout activeDrawerId = { testDrawerResizable . id } drawers = { [ testDrawerResizable ] } />
188
199
) ;
189
- wrapper . findActiveDrawerResizeHandle ( ) ! . keydown ( KeyCode . left ) ;
200
+ await waitFor ( ( ) => {
201
+ wrapper . findActiveDrawerResizeHandle ( ) ! . keydown ( KeyCode . left ) ;
202
+ } ) ;
190
203
191
204
expect ( onDrawerItemResize ) . toHaveBeenCalledWith ( { size : expect . any ( Number ) , id : 'security' } ) ;
192
205
} ) ;
193
206
194
- test ( 'should change size via mouse pointer on slider handle' , ( ) => {
207
+ test ( 'should change size via mouse pointer on slider handle' , async ( ) => {
195
208
const onDrawerItemResize = jest . fn ( ) ;
196
209
const testDrawerResizable : AppLayoutProps . Drawer = {
197
210
...testDrawer ,
@@ -201,18 +214,23 @@ describeEachAppLayout({ sizes: ['desktop'] }, ({ theme }) => {
201
214
const { wrapper } = renderComponent (
202
215
< AppLayout activeDrawerId = { testDrawerResizable . id } drawers = { [ testDrawerResizable ] } />
203
216
) ;
204
- wrapper . findActiveDrawerResizeHandle ( ) ! . fireEvent ( new MouseEvent ( 'pointerdown' , { bubbles : true } ) ) ;
217
+ await waitFor ( ( ) => {
218
+ wrapper . findActiveDrawerResizeHandle ( ) ! . fireEvent ( new MouseEvent ( 'pointerdown' , { bubbles : true } ) ) ;
219
+ } ) ;
205
220
const resizeEvent = new MouseEvent ( 'pointermove' , { bubbles : true } ) ;
206
221
wrapper . findActiveDrawerResizeHandle ( ) ! . fireEvent ( resizeEvent ) ;
207
222
wrapper . findActiveDrawerResizeHandle ( ) ! . fireEvent ( new MouseEvent ( 'pointerup' , { bubbles : true } ) ) ;
208
223
209
224
expect ( onDrawerItemResize ) . toHaveBeenCalledWith ( { size : expect . any ( Number ) , id : 'security' } ) ;
210
225
} ) ;
211
226
212
- test ( 'should read relative size on resize handle' , ( ) => {
227
+ test ( 'should read relative size on resize handle' , async ( ) => {
213
228
const { wrapper } = renderComponent ( < AppLayout drawers = { [ { ...testDrawer , resizable : true } ] } /> ) ;
214
229
215
- wrapper . findDrawerTriggerById ( testDrawer . id ) ! . click ( ) ;
230
+ await waitFor ( ( ) => {
231
+ wrapper . findDrawerTriggerById ( testDrawer . id ) ! . click ( ) ;
232
+ } ) ;
233
+
216
234
expect ( wrapper . findActiveDrawerResizeHandle ( ) ! . getElement ( ) ) . toHaveAttribute ( 'aria-valuenow' , '0' ) ;
217
235
} ) ;
218
236
@@ -222,18 +240,22 @@ describeEachAppLayout({ sizes: ['desktop'] }, ({ theme }) => {
222
240
expect ( wrapper . findDrawersTriggers ( ) ! . length ) . toBeLessThan ( 100 ) ;
223
241
} ) ;
224
242
225
- test ( 'Renders aria-controls on toggle only when active' , ( ) => {
243
+ test ( 'Renders aria-controls on toggle only when active' , async ( ) => {
226
244
const { wrapper } = renderComponent ( < AppLayout drawers = { [ testDrawer ] } /> ) ;
227
- expect ( wrapper . findDrawerTriggerById ( 'security' ) ! . getElement ( ) ) . not . toHaveAttribute ( 'aria-controls' ) ;
245
+ await waitFor ( ( ) => {
246
+ expect ( wrapper . findDrawerTriggerById ( 'security' ) ! . getElement ( ) ) . not . toHaveAttribute ( 'aria-controls' ) ;
247
+ } ) ;
228
248
wrapper . findDrawerTriggerById ( 'security' ) ! . click ( ) ;
229
249
expect ( wrapper . findDrawerTriggerById ( 'security' ) ! . getElement ( ) ) . toHaveAttribute ( 'aria-controls' , 'security' ) ;
230
250
} ) ;
231
251
232
- test ( 'should render badge when defined' , ( ) => {
252
+ test ( 'should render badge when defined' , async ( ) => {
233
253
const { wrapper } = renderComponent ( < AppLayout drawers = { manyDrawers } /> ) ;
234
254
235
- expect ( wrapper . findDrawerTriggerById ( manyDrawers [ 0 ] . id , { hasBadge : true } ) ) . toBeTruthy ( ) ;
236
- expect ( wrapper . findDrawerTriggerById ( manyDrawers [ 1 ] . id , { hasBadge : false } ) ) . toBeTruthy ( ) ;
255
+ await waitFor ( ( ) => {
256
+ expect ( wrapper . findDrawerTriggerById ( manyDrawers [ 0 ] . id , { hasBadge : true } ) ) . toBeTruthy ( ) ;
257
+ expect ( wrapper . findDrawerTriggerById ( manyDrawers [ 1 ] . id , { hasBadge : false } ) ) . toBeTruthy ( ) ;
258
+ } ) ;
237
259
} ) ;
238
260
239
261
test ( 'should return null when searching for a non-existing drawer with hasBadge condition' , ( ) => {
@@ -243,10 +265,12 @@ describeEachAppLayout({ sizes: ['desktop'] }, ({ theme }) => {
243
265
expect ( wrapper . findDrawerTriggerById ( 'non-existing' , { hasBadge : false } ) ) . toBeNull ( ) ;
244
266
} ) ;
245
267
246
- test ( 'should have width equal to the size declaration' , ( ) => {
268
+ test ( 'should have width equal to the size declaration' , async ( ) => {
247
269
const { wrapper } = renderComponent ( < AppLayout drawers = { [ { ...testDrawer , resizable : true , defaultSize : 500 } ] } /> ) ;
248
270
249
- wrapper . findDrawersTriggers ( ) ! [ 0 ] . click ( ) ;
271
+ await waitFor ( ( ) => {
272
+ wrapper . findDrawersTriggers ( ) ! [ 0 ] . click ( ) ;
273
+ } ) ;
250
274
expect ( getActiveDrawerWidth ( wrapper ) ) . toEqual ( '500px' ) ;
251
275
} ) ;
252
276
} ) ;
@@ -304,23 +328,28 @@ describeEachAppLayout({ themes: ['classic'], sizes: ['desktop'] }, () => {
304
328
305
329
describeEachAppLayout ( { themes : [ 'refresh' , 'refresh-toolbar' ] , sizes : [ 'desktop' ] } , ( { theme } ) => {
306
330
const styles = theme === 'refresh' ? visualRefreshStyles : toolbarStyles ;
307
- test ( `${ theme } - renders roles only when aria labels are not provided` , ( ) => {
331
+ test ( `${ theme } - renders roles only when aria labels are not provided` , async ( ) => {
308
332
const { wrapper } = renderComponent ( < AppLayout navigationHide = { true } drawers = { [ testDrawerWithoutLabels ] } /> ) ;
309
333
310
- expect ( wrapper . findDrawerTriggerById ( testDrawer . id ) ! . getElement ( ) ) . not . toHaveAttribute ( 'aria-label' ) ;
334
+ await waitFor ( ( ) => {
335
+ expect ( wrapper . findDrawerTriggerById ( testDrawer . id ) ! . getElement ( ) ) . not . toHaveAttribute ( 'aria-label' ) ;
336
+ } ) ;
337
+
311
338
expect ( wrapper . findByClassName ( styles [ 'drawers-desktop-triggers-container' ] ) ! . getElement ( ) ) . not . toHaveAttribute (
312
339
'aria-label'
313
340
) ;
314
341
expect ( wrapper . findByClassName ( styles [ 'drawers-trigger-content' ] ) ! . getElement ( ) ) . toHaveAttribute ( 'role' , 'toolbar' ) ;
315
342
} ) ;
316
343
317
- test ( `${ theme } - renders roles and aria labels when provided` , ( ) => {
344
+ test ( `${ theme } - renders roles and aria labels when provided` , async ( ) => {
318
345
const { wrapper } = renderComponent ( < AppLayout drawers = { [ testDrawer ] } ariaLabels = { { drawers : 'Drawers' } } /> ) ;
319
346
320
- expect ( wrapper . findDrawerTriggerById ( 'security' ) ! . getElement ( ) ) . toHaveAttribute (
321
- 'aria-label' ,
322
- 'Security trigger button'
323
- ) ;
347
+ await waitFor ( ( ) => {
348
+ expect ( wrapper . findDrawerTriggerById ( 'security' ) ! . getElement ( ) ) . toHaveAttribute (
349
+ 'aria-label' ,
350
+ 'Security trigger button'
351
+ ) ;
352
+ } ) ;
324
353
expect ( wrapper . findByClassName ( styles [ 'drawers-desktop-triggers-container' ] ) ! . getElement ( ) ) . toHaveAttribute (
325
354
'aria-label' ,
326
355
'Drawers'
0 commit comments