-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Components: Refactor more tests to use real timers #47318
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { act, render, screen, within } from '@testing-library/react'; | ||
import { render, screen, within, waitFor } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
/** | ||
|
@@ -16,8 +16,6 @@ import { copy } from '@wordpress/icons'; | |
*/ | ||
import { BlockSwitcher, BlockSwitcherDropdownMenu } from '../'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
jest.mock( '@wordpress/data/src/components/use-select', () => jest.fn() ); | ||
jest.mock( '../../block-title/use-block-display-title', () => | ||
jest.fn().mockReturnValue( 'Block Name' ) | ||
|
@@ -185,9 +183,7 @@ describe( 'BlockSwitcherDropdownMenu', () => { | |
} ); | ||
|
||
test( 'should simulate a keydown event, which should open transform toggle.', async () => { | ||
const user = userEvent.setup( { | ||
advanceTimers: jest.advanceTimersByTime, | ||
} ); | ||
const user = userEvent.setup(); | ||
|
||
render( | ||
<BlockSwitcherDropdownMenu blocks={ [ headingBlock1 ] } /> | ||
|
@@ -212,26 +208,27 @@ describe( 'BlockSwitcherDropdownMenu', () => { | |
} ), | ||
'[ArrowDown]' | ||
); | ||
await act( () => Promise.resolve() ); | ||
|
||
expect( | ||
screen.getByRole( 'button', { | ||
name: 'Block Name', | ||
expanded: true, | ||
} ) | ||
).toBeVisible(); | ||
await waitFor( () => | ||
expect( | ||
screen.getByRole( 'button', { | ||
name: 'Block Name', | ||
expanded: true, | ||
} ) | ||
).toBeVisible() | ||
); | ||
|
||
const menu = screen.getByRole( 'menu', { | ||
name: 'Block Name', | ||
} ); | ||
expect( menu ).toBeInTheDocument(); | ||
expect( menu ).not.toBeVisible(); | ||
await waitFor( () => | ||
expect( | ||
screen.getByRole( 'menu', { | ||
name: 'Block Name', | ||
} ) | ||
).toBeVisible() | ||
); | ||
} ); | ||
|
||
test( 'should simulate a click event, which should call onToggle.', async () => { | ||
const user = userEvent.setup( { | ||
advanceTimers: jest.advanceTimersByTime, | ||
} ); | ||
const user = userEvent.setup(); | ||
|
||
render( | ||
<BlockSwitcherDropdownMenu blocks={ [ headingBlock1 ] } /> | ||
|
@@ -255,26 +252,27 @@ describe( 'BlockSwitcherDropdownMenu', () => { | |
expanded: false, | ||
} ) | ||
); | ||
await act( () => Promise.resolve() ); | ||
|
||
expect( | ||
screen.getByRole( 'button', { | ||
name: 'Block Name', | ||
expanded: true, | ||
} ) | ||
).toBeVisible(); | ||
await waitFor( () => | ||
expect( | ||
screen.getByRole( 'button', { | ||
name: 'Block Name', | ||
expanded: true, | ||
} ) | ||
).toBeVisible() | ||
); | ||
|
||
const menu = screen.getByRole( 'menu', { | ||
name: 'Block Name', | ||
} ); | ||
expect( menu ).toBeInTheDocument(); | ||
expect( menu ).not.toBeVisible(); | ||
await waitFor( () => | ||
expect( | ||
screen.getByRole( 'menu', { | ||
name: 'Block Name', | ||
} ) | ||
).toBeVisible() | ||
); | ||
Comment on lines
-267
to
+271
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand these changes — previously the test was checking for the menu to be in the document, but not visible, while the new version of the test seems to check that the menu is visible. Can you explain the rationale here? Thank you! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous test was just wrong. If you manually go through the steps, you'll see that both the button and the menu should be visible. The previous test just wasn't properly waiting for the menu to be revealed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤦 |
||
} ); | ||
|
||
test( 'should create the transform items for the chosen block.', async () => { | ||
const user = userEvent.setup( { | ||
advanceTimers: jest.advanceTimersByTime, | ||
} ); | ||
const user = userEvent.setup(); | ||
|
||
render( | ||
<BlockSwitcherDropdownMenu blocks={ [ headingBlock1 ] } /> | ||
|
@@ -286,15 +284,16 @@ describe( 'BlockSwitcherDropdownMenu', () => { | |
expanded: false, | ||
} ) | ||
); | ||
await act( () => Promise.resolve() ); | ||
|
||
expect( | ||
within( | ||
screen.getByRole( 'menu', { | ||
name: 'Block Name', | ||
} ) | ||
).getByRole( 'menuitem' ) | ||
).toBeInTheDocument(); | ||
await waitFor( () => | ||
expect( | ||
within( | ||
screen.getByRole( 'menu', { | ||
name: 'Block Name', | ||
} ) | ||
).getByRole( 'menuitem' ) | ||
).toBeInTheDocument() | ||
); | ||
} ); | ||
} ); | ||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as previous comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replied there