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

Components: improve tests for ToggleGroupControl #45627

Merged

Conversation

flootr
Copy link
Contributor

@flootr flootr commented Nov 9, 2022

What?

Improve tests for the ToggleGroupControl component.

Why?

Follow along RTL best practices to improve code quality.

How?

  • Using @testing-library/user-event instead of fireEvent
  • Assert using .toBeVisible rather than .toBeInTheDocument in one case

Testing Instructions

Verify unit tests still pass

npm run test:unit packages/components/src/toggle-group-control/test
@codesandbox
Copy link

codesandbox bot commented Nov 9, 2022

CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@tyxla tyxla added [Type] Code Quality Issues or PRs that relate to code quality [Package] Components /packages/components labels Nov 9, 2022
@tyxla tyxla added this to In progress (owned) ⏳ in WordPress Components via automation Nov 9, 2022
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.

Thanks for this @flootr! It looks good overall, I just have a couple of questions.

@@ -121,7 +124,7 @@ describe( 'ToggleGroupControl', () => {
'Click for Sumptuous Caponata'
);

fireEvent.focus( secondRadio );
await user.click( secondRadio );
Copy link
Member

Choose a reason for hiding this comment

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

Did we mean to change the focus to be click instead?

Copy link
Member

Choose a reason for hiding this comment

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

In both tests, we're testing whether a tooltip appears or not over a radio form item when the item is focused. If userEvent has some method for simulating "move the mouse over the input", that would be great to use here. The tooltip is then supposed to appear.

user.tab() works because it focuses the right element, but it's quite an indirect and non-obvious way to do that.

I think user.click is wrong. The tooltip indeed doesn't appear, so the test passes, but it doesn't appear for the wrong reasons.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with Jarda on this. Also IMHO the safest bet is to just keep triggering the focus event, but just not with fireEvent:

Suggested change
await user.click( secondRadio );
secondRadio.focus();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback! I agree on keeping it simple in this case. Does 848e112 work?

@@ -103,11 +104,11 @@ describe( 'ToggleGroupControl', () => {
'Click for Delicious Gnocchi'
);

fireEvent.focus( firstRadio );
firstRadio.focus();
Copy link
Member

Choose a reason for hiding this comment

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

As discussed face-to-face, we can try using userEvent.hover() and if that's enough, it resembles the user behavior much better, but it also is wrapped in an act() so it'll be compatible to React 18.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Implemented via 443a127

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.

LGTM 🚀 Thanks!

@tyxla tyxla merged commit 2d8c0ee into WordPress:trunk Nov 9, 2022
WordPress Components automation moved this from In progress (owned) ⏳ to Done 🎉 Nov 9, 2022
@github-actions github-actions bot added this to the Gutenberg 14.6 milestone Nov 9, 2022
@tyxla tyxla added First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository and removed First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository labels Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Components /packages/components [Type] Code Quality Issues or PRs that relate to code quality
3 participants