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

onClick event fired twice by typing "{enter}" or "{space}" on button element #554

Closed
with-heart opened this issue Feb 6, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@with-heart
Copy link

  • @testing-library/user-event version: 12.6.3
  • Testing Framework and version: @testing-library/[email protected]
  • DOM Environment: [email protected] (I believe this is the version CodeSandbox uses with the default React template)

Relevant code or config

userEvent.type(screen.getByRole('button'), '{enter}')
userEvent.type(screen.getByRole('button'), '{space}')

What you did:

Used either of the above lines on a button element, which should trigger two events:

  • onClick with detail: 0 (representing that the event was caused by the keyboard)
  • onKeyPress

What happened:

The onClick event is fired twice, once with detail: 0 (correct) and a second time with detail: 1 (incorrect; shouldn't even occur).

In the reproduction below, you can see tests that validate that this is happening, and you can also interact with the button in the Browser tab to see what browser behavior is by default:

  • When clicking the button element with the mouse, we receive one onClick event with detail: 1.
  • When pressing Enter or Space on the focused button element, we receive one onClick event with detail: 0 and one onKeyPress event.

Reproduction repository:

https://codesandbox.io/s/ecstatic-easley-t6lie?file=/src/button.test.js

Problem description:

I'm working on some hooks that provide WAI-ARIA accordion functionality and I'm attempting to test an event relating to pressing Enter or Space on an accordion header element with role "button". In the case of this element, I'd like to ignore the onClick event that's fired by a keypress and handle the functionality only in onKeyPress.

Suggested solution:

In the handleEnter/handleSpaceOnClickable, fireEvent.click is called on currentElement(), which I believe causes the onClick with detail: 1. I'm unsure what the onClick with detail: 0 is caused by, but I imagine if we're able to consolidate this into a single call with detail: 0, it would resolve the issue.

@ph-fritsche ph-fritsche mentioned this issue Feb 28, 2021
8 tasks
@ph-fritsche ph-fritsche added needs investigation Someone has to do research on this bug Something isn't working labels Mar 16, 2021
@ph-fritsche
Copy link
Member

ph-fritsche commented Mar 17, 2021

The extra click event detail: 1 is cause by the automatic click performed by userEvent.type.
If you call userEvent.click(element, '{enter}', {skipClick: true}) or use userEvent.keyboard('[Enter]') there is only one click with detail: 0.

@with-heart
Copy link
Author

@ph-fritsche Good to know! Also thanks for taking the time to collect and understand all of the issues in #521 and doing so much excellent work in #581. The open source world needs more people like you! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants