Skip to content
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

Tooltip: improve tests #57345

Merged
merged 25 commits into from
Jan 4, 2024
Merged

Tooltip: improve tests #57345

merged 25 commits into from
Jan 4, 2024

Conversation

ciampo
Copy link
Contributor

@ciampo ciampo commented Dec 22, 2023

Related to #57206 and #57204

What?

Improve Tooltip tests:

  • make less flaky / hacky
  • test for tooltip to hide after hover / focus / click events as expected
  • tidy up code

Why?

The conversation in #57206 brought up some added nuance to Tooltip's behavior that we should add to our unit tests

How?

  • Switched to @ariakit/test
  • Went test by test, improving assertions, adding comments, and generally proposing a better order/organisation

Testing Instructions

Note: I recommend reviewing code changes commit by commit

  • Tooltip: No runtime changes, make sure unit tests pass
  • Modal: added an extra check on detecting the Escape key => test on Storybook and in the editor that the Modal component still closes as expected when pressing the Escape key

@ciampo ciampo self-assigned this Dec 22, 2023
@ciampo ciampo added [Type] Enhancement A suggestion for improvement. [Package] Components /packages/components labels Dec 22, 2023
@@ -209,7 +209,7 @@ function UnforwardedModal(

if (
shouldCloseOnEsc &&
event.code === 'Escape' &&
( event.code === 'Escape' || event.key === 'Escape' ) &&
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ariakit/test keyboard events use event.key, so I had to add this check in order for the Modal component to react to those simulated user events

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can implement code in @ariakit/test. I'm just not sure how to transform keys into code, and I'd prefer to sidestep the need to maintain a full map of values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can keep this change to the Modal component for now (cc @mirka )


render(
<>
it( 'should not show tooltip if the mouse leaves the tooltip anchor before set delay', async () => {
Copy link
Contributor Author

@ciampo ciampo Dec 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@diegohaz This test keeps failing when the whole suite runs, but passes when run in isolation.

It looks like there is still some "leak" across tests even after using @ariakit/test, but I can't understand what it is — do you have any ideas?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tooltips appear instantly if another tooltip has just been hidden. This is controlled by a global state, which is probably shared among multiple tests within the same file. These tests don't exactly run in total isolation.

If this is the issue, you might need to introduce a delay at the start to ensure tooltips from other tests aren't interfering with this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tooltips appear instantly if another tooltip has just been hidden. This is controlled by a global state, which is probably shared among multiple tests within the same file. These tests don't exactly run in total isolation.

That seems to be the issue, since the test passes if I set the skipTimeout: 0 (although I'd prefer to leave the skipTimeout delay around if possible).

If this is the issue, you might need to introduce a delay at the start to ensure tooltips from other tests aren't interfering with this one.

I tried adding a delay specifically for that test, but I keep getting failures. The only way to reliably have all tests pass seems to be to add

	afterEach( async () => {
		await sleep( 300 );
	} );

although I'd prefer to avoid slowing down the whole suite if possible. Do you have any suggestions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option is to run those tests fully in parallel. I know Vitest offers this feature, but I'm not sure about Jest.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, I'll have to resort to waiting those extra 300ms at the end of each test to make sure that the "skip timeout" behaviour is not triggered.

Copy link
Member

@diegohaz diegohaz Feb 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, this await sleep(300) might not be required anymore with @ariakit/react@0.4.2.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR to remove it: #60897

@ciampo ciampo requested review from mirka and a team December 22, 2023 15:37
Copy link

Flaky tests detected in ac4d28f.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7301468581
📝 Reported issues:

@ciampo ciampo requested a review from diegohaz December 22, 2023 16:47
@@ -66,7 +66,7 @@ function Tooltip( props: TooltipProps ) {

const tooltipStore = Ariakit.useTooltipStore( {
placement: computedPlacement,
timeout: delay,
showTimeout: delay,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the delay prop, it looks like it was always intended to only be applied to the "show" timeout, and not affect the "hide" timeout.

@ciampo ciampo requested a review from diegohaz January 3, 2024 22:44
@ciampo
Copy link
Contributor Author

ciampo commented Jan 3, 2024

@diegohaz @WordPress/gutenberg-components this PR is ready for another round of review

Copy link
Member

@mirka mirka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvements! And thanks for the cleanly separated commits, it was easy to go through 👍

packages/components/src/tooltip/test/index.tsx Outdated Show resolved Hide resolved
packages/components/src/tooltip/test/index.tsx Outdated Show resolved Hide resolved
packages/components/src/tooltip/test/index.tsx Outdated Show resolved Hide resolved
@ciampo ciampo enabled auto-merge (squash) January 4, 2024 23:28
@ciampo ciampo merged commit bb2a39f into trunk Jan 4, 2024
56 checks passed
@ciampo ciampo deleted the refactor/tooltip-tests branch January 4, 2024 23:57
@github-actions github-actions bot added this to the Gutenberg 17.5 milestone Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Components /packages/components [Type] Enhancement A suggestion for improvement.
Projects
Status: Done 🎉
Development

Successfully merging this pull request may close these issues.

4 participants