question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

click() doesn't quite work for div role="radio"

See original GitHub issue

First, thanks for this library - great abstraction.

I just tried to use the userEvent.click() function on a radio button that is implemented as <div role="radio" /> (e.g. @palmerhq/radio-group) and it didn’t quite yield the same expected behavior as directly calling fireEvent.click(). I see the click logic currently looks for <input type="radio" /> and calls clickBooleanElement, but this implementation of a radio button falls through to clickElement.

In the actual application, clicking said radio button moves the focus to an input field that becomes enabled when this radio button is selected. If I use fireEvent.click to select the radio button, asserting focus to be on the text input afterwards is successful. But using userEvent.click causes the focus assertion to fail.

The current published version of @testing-library/user-event results in the radio div retaining focus. I locally modified it to check for this type of element and call clickBooleanElement instead, but that doesn’t seem to be quite right either. In that version, body ends up with focus.

Thoughts?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
kentcdoddscommented, Jun 1, 2020

🎉 This issue has been resolved in version 10.4.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

2reactions
kentcdoddscommented, May 31, 2020

I believe this actually is evidence of an issue we should address in user-event. We’ve discussed this in the past and I assumed that we didn’t need to worry about wrapping userEvent in act, but based on this example, I think it’s clear we need to figure out a way to automatically wrap userEvent in act.

https://codesandbox.io/s/user-event-issue-188-o0281?file=/src/__tests__/App.test.js:316-358

  act(() => { // <-- required for this to work
    user.click(custom)
  })

Any ideas on how to do this effectively would be welcome.

What I’m personally thinking is we could add a configuration option to @testing-library/dom for wrapping fireEvent in act just like we do for the async act stuff. https://github.com/testing-library/react-testing-library/blob/0afcbea3c3d1ddce218a36d963d39fa83f9a7cf6/src/pure.js#L12-L20

That way, if someone imported @testing-library/react, they would automatically configure @testing-library/dom to wrap fireEvent in act rather than having to do this fancy stuff.

To be clear, we wouldn’t specifically include act in @testing-library/dom, but would instead enable people to do whatever they link whenever an event is fired.

I have a good idea of what this will be in my head and I’ll probably work on it tomorrow morning.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Function on radio button click not firing - Stack Overflow
So here, if the button is clicked, the function fires the alert. If a radio button is clicked, nothing happens. In summary, what...
Read more >
ARIA: radio role - Accessibility - MDN Web Docs
A radio button is a checkable input that when associated with other radio buttons, only one of which can be checked at a...
Read more >
JavaScript · Bootstrap
modal-backdrop to provide a click area for dismissing shown modals when clicking outside the modal. Via data attributes. Activate a modal without writing ......
Read more >
Enhancing The Clickable Area Size - Ahmad Shadeed
Have you ever tried to click or tap on an element (e.g.: button, link) and you realize that it doesn't respond until you...
Read more >
The "Checkbox Hack" (and things you can do with it)
But just because the checkbox is hidden, clicking the <label> still toggles its ... <div class="tabs"> <div class="tab"> <input type="radio" ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found