-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Components: improve tests for ToggleGroupControl
#45627
Conversation
|
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 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 ); |
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.
Did we mean to change the focus to be click instead?
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.
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.
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 agree with Jarda on this. Also IMHO the safest bet is to just keep triggering the focus event, but just not with fireEvent
:
await user.click( secondRadio ); | |
secondRadio.focus(); |
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 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(); |
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.
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.
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.
Implemented via 443a127
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.
LGTM 🚀 Thanks!
What?
Improve tests for the
ToggleGroupControl
component.Why?
Follow along RTL best practices to improve code quality.
How?
@testing-library/user-event
instead offireEvent
.toBeVisible
rather than.toBeInTheDocument
in one caseTesting Instructions
Verify unit tests still pass