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

Button: Update test assertion to match test name #54260

Merged
merged 4 commits into from
Sep 12, 2023
Merged

Conversation

brookewp
Copy link
Contributor

@brookewp brookewp commented Sep 7, 2023

What?

While working on #48222, I encountered a Button test where the assertion didn't match the test's intention based on the test name should populate tooltip with description content for buttons with visible labels (buttons with children)'.

Why?

As mentioned in the test name, the test is meant to check if the tooltip has been populated with the description content, but the assertion checks the button is visible instead. This will pass, even if the tooltip isn't there or it is populated with the description content. Therefore, if we intend to test the tooltip, we should query for that instead.

How?

By changing the role from button to tooltip. There are no actions taken to show the tooltip, so we also need to add that it is hidden and change the assertion to find by text content instead. I believe the description comes from the aria-label rather than aria-describedby, so we can't check for an accessible description.

Testing Instructions

Ensure tests pass: npm run test:unit packages/components/src/button/index.tsx

@brookewp brookewp added [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. [Package] Components /packages/components labels Sep 7, 2023
@brookewp brookewp self-assigned this Sep 7, 2023
} )
).toBeVisible();
).toHaveTextContent( 'Description text' );
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer to keep the existing assertion for the button, since it tests the existence of the actual button described by the visually hidden description text. Then I'd just add the assertion you created here for the tooltip, since that tests the hidden tooltip part.

In other words, this tests two different parts of what's rendered and to me it makes sense to test them both for a better test coverage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair point!

Copy link
Contributor

@ciampo ciampo Sep 9, 2023

Choose a reason for hiding this comment

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

Just flagging that, after #54312 will get merged, the tooltip won't be found in the DOM until visible (ie. triggered with hover/focus)

Comment on lines 261 to 265
expect(
screen.getByRole( 'tooltip', {
hidden: true,
} )
).toHaveTextContent( 'Description text' );
Copy link
Member

@tyxla tyxla Sep 11, 2023

Choose a reason for hiding this comment

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

Just flagging that, when #54312 merged, the tooltip won't be found in the DOM until visible (ie. triggered with hover/focus)

Good point @ciampo! I guess then we'll need to trigger a hover, and will need to add a waitFor() so the assertion is called again when the tooltip appears. Something like:

Suggested change
expect(
screen.getByRole( 'tooltip', {
hidden: true,
} )
).toHaveTextContent( 'Description text' );
const user = userEvent.setup();
await user.hover(
screen.getByRole( 'button', {
description: 'Description text',
} )
);
await waitFor( () => {
expect(
screen.getByRole( 'tooltip', {
hidden: true,
} )
).toHaveTextContent( 'Description text' );
} );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The other tooltip tests used tab so I followed that, which lets us get around using waitFor():

expect(
screen.getByRole( 'button', {
description: 'Description text',
} )
).toBeVisible();
await user.tab();
expect(
screen.getByRole( 'tooltip', {
name: 'Description text',
} )
).toBeVisible();
await cleanupTooltip( user );

But if hover is preferable, we could use findByRole instead:

const button = screen.getByRole( 'button', {
	description: 'Description text',
} );

expect( button ).toBeVisible();

await user.hover( button );

expect(
	await screen.findByRole( 'tooltip', {
		name: 'Description text',
	} )
).toBeVisible();

Not sure if this info is still relevant, but it seems like find* is preferable over waitFor().

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm personally ok with both tab and hover, and I also prefer using findBy instead of waitFor.

I guess we can keep the code as-is and wrap up the PR :)

Copy link
Member

Choose a reason for hiding this comment

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

I just quickly drafted this as an example, obviously, you can use whatever makes sense the most. In any case you can totally go with tab and findBy if you wish, I think this is a minor stylistic choice that wouldn't affect the quality of the test anyway.

Copy link
Contributor

@ciampo ciampo left a comment

Choose a reason for hiding this comment

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

🚀

Copy link
Member

@tyxla tyxla left a comment

Choose a reason for hiding this comment

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

Looking good, thanks @brookewp 🚀

@tyxla
Copy link
Member

tyxla commented Sep 12, 2023

Restarted the performance tests, seems like a flaky run.

@brookewp brookewp merged commit 5331fb6 into trunk Sep 12, 2023
49 checks passed
@brookewp brookewp deleted the update/button-test branch September 12, 2023 17:29
@github-actions github-actions bot added this to the Gutenberg 16.7 milestone Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Components /packages/components [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants