-
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
Block Editor: Unmount after each MediaReplaceFlow
test
#45736
Conversation
Open in CodeSandbox Web Editor | VS Code | VS Code Insiders |
Size Change: +6.57 kB (+1%) Total Size: 1.3 MB
ℹ️ View Unchanged
|
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.
Based on the docs, cleanup should be automatic when using the Jest framework.
Any idea why it is failing for us?
That's true, The trick is that the test and The |
Yes, definitely. At this moment these fixes are still experimental though. We're in the process of figuring out what the best practices actually are. We'll know better after we've fixed a larger sample of tests with diverse scenarios. Then we can write down authoritative instructions. The manual synchronous |
It has some effect because it makes the tests green, but I'm afraid it's another accidental fix where |
MediaReplaceFlow
testsMediaReplaceFlow
test
Thanks for the feedback, @Mamaduka, and @jsnajdr! I agree with the rationale to prefer unmounting vs just cleaning up. Unmounting is much more specific in this case, and it's usually immediately clear that it will cancel any running effects. I've thus updated the tests to Noting that my testing demonstrates that just adding Ready for another look 👀 |
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.
Tests are green for both React 17 and 18. Thanks, @tyxla!
|
||
// Unmount the UI synchronously so that any async effects, like the on-mount focus | ||
// that shows and positions a tooltip, are cancelled right away and never run. | ||
unmount(); |
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.
For any test that's async, and contains await
statements, typically user-event
calls like await user.click()
, this is unsound. The tests are green only because the test function starts winning a microtask race with the floating-ui
's computePosition
.
The popover is opened by the very first click in the test, on the "Replace" button. Then there are four more await
s before the unmount()
. Originally, the unmount()
would be done only a few microtasks later, inside afterEach
. Now it happens a few microtask earlier.
Meanwhile, the "Replace" button triggered another async function with many await
statements, the popover's computePosition()
. If it starts winning the race again, the test will start failing.
One way how this fixed test can be made to fail again is to simply replace fake timers with real ones. Then every await.click
will internally do a setTimeout( 0 )
, and that guarantees that the computePosition
task, which doesn't do any timeouts, just microtasks, will always win and fail the test.
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.
I understand your point, but this is likely true for many other tests. I believe we could make a compromise and go ahead to ship this, unblocking the upgrade.
Perhaps we could add a comment that it needs a follow-up and properly better cleanup in the component itself? From what you explained, it seems like it's an issue that actually spreads beyond the test itself.
WDYT?
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.
You can merge the PR and postpone the problem to be solved later, but I think it's suboptimal.
MediaReplaceFlow
could be a great opportunity to re-introduce the .toBePositioned()
helper created in #45587. Clicking on the "Replace" button actually shows a popover and we're actively working with it:
The tests would then be like:
- Render the component.
- Click the "Replace" button.
- Wait for the popover to be open and positioned.
- Do the test-specific work on the popover: check for presence of menu items, edit the media URL etc.
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.
This was closed because we chose to go with #45863 as an alternative. |
What?
This PR updates the
MediaReplaceFlow
tests to add unmounting after each one, to cancel any remaining async effects.Why?
Those tests are failing in React 18, see #45235. With the unmount, they'll pass both in React 17 and React 18.
How?
We're simply adding an RTL
unmount()
at the end of each test.Testing Instructions
npm run test:unit packages/block-editor/src/components/media-replace-flow/test/index.js