-
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
JS unit tests: update popover matcher #54168
Conversation
Size Change: 0 B Total Size: 1.52 MB ℹ️ View Unchanged
|
Flaky tests detected in e71b618. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6091438030
|
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.
The change here makes sense to me 👍
Unfortunately, I was able reproduce the snapshot failure when running tests 20+ times.
Nooo! I thought I was special :( |
Yeah, I could reproduce it by running the tests just once 🙂 This PR is a nice refactoring, but it won't have influence on that bug. The bug is caused by the fact that the popover appearance is animated. In a rAF loop, the opacity goes from 0 to 1, and other properties like We used to have plans to add the |
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.
When introducing these wrappers a year ago with @tyxla, we had good reasons why they need to be separate. I would have to find the exact PRs where these discussions happened to know the details. But apparently these reasons disappeared over time, as this PR works perfectly fine.
@@ -5,7 +5,8 @@ | |||
* @param {HTMLElement} element Popover element. | |||
*/ | |||
function toBePositionedPopover( element ) { | |||
const pass = element.classList.contains( 'is-positioned' ); | |||
const popover = element.closest( '.components-popover' ) || element; |
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.
There should be no need to default to element
. If the element or its ancestor doesn't have the .components-popover
class, the check should immediately fail because no popover to check has been found. There's no point checking whether a non-popover element has also the .is-positioned
class.
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.
Also, it would be nice if the matcher detected that the element
is either null
or not inside a Popover, and printed a nice message. At this moment it's just a TypeError
:
TypeError: Cannot read properties of null (reading 'closest')
TypeError: Cannot read properties of null (reading 'classList')
I think we had that at one moment in the past, somehow it got lost.
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.
Thanks for the tips and context here. Much appreciated 🙇🏻
Updated accordingly.
Thanks for testing @Mamaduka and @jsnajdr - also for the explainer. I'm not sure if it's my hardware, but it also took me 20+ times to fail. I though 10 was the magic number 😄 I wonder if mocking
Good to know. I've updated the PR to return more meaningful messages as per @jsnajdr's advice. If folks are okay with the refactor I'll merge, otherwise happy to just kill the PR mercifully. |
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.
Although it doesn't fix any flaky test, this is still a valuable code refactoring 👍 I posted some style nits, otherwise LGTM.
const popover = element && element.closest( '.components-popover' ); | ||
const isPopoverPositioned = | ||
popover && popover.classList.contains( 'is-positioned' ); | ||
const pass = !! popover && !! isPopoverPositioned; |
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.
Some style nits:
- can use
element?.closest
andpopover?.classList
optional chainings pass
can be simply!! isPopoverPositioned
, the!! popover
condition is redundant.
…rapping `getWrappingPopoverElement()`, so this commit just moves `getWrappingPopoverElement` logic into `toBePositionedPopover` to save everyone the trouble.
…element - change messaging based on error type
f1cf806
to
e71b618
Compare
What?
This PR moves
getWrappingPopoverElement
logic intotoBePositionedPopover
.This PR was originally designed to fix the flaky test in
packages/nux/src/components/dot-tip/test/index.js
, which usedtoBePositionedPopover
without a correspondinggetWrappingPopoverElement
.Why?
There was no use of the
toBePositionedPopover()
matcher without a wrappinggetWrappingPopoverElement()
.The alternative to this PR is to just wrap the flaky test assertion in a custom
getWrappingPopoverElement()
.Testing Instructions
CI tests should pass
To make sure the
packages/nux/src/components/dot-tip/test/index.js
tests pass, in your console, run: