Native button should trigger click on keydown space
See original GitHub issueFor of all, thank you for this handy library 😄
@testing-library/user-event
version: 12.0.11- Testing Framework and version: jest - 26.1.0
- DOM Environment: react-dom - 16.13.1
Relevant code or config
import React from "react";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
describe("test", () => {
it("emits click on button type space", async () => {
const handleClick = jest.fn();
render(<button onClick={handleClick}>foo</button>);
screen.getByText("foo").focus();
await userEvent.type(screen.getByText("foo"), " ", { skipClick: true });
expect(handleClick).toHaveBeenCalledTimes(1);
});
});
Reproduction repository:
https://codesandbox.io/s/angry-bas-bttix?file=/src/user-event.test.js
Problem description:
A native button triggers a click event on keydown space.
Currently only keydown enter is handled properly by userEvent.type
.
Suggested solution:
Add the same behavior that keydown enter has for keydown space.
Issue Analytics
- State:
- Created 3 years ago
- Comments:16 (11 by maintainers)
Top Results From Across the Web
Reactjs trigger button with keydown when pressing space bar ...
Is there a way to use OnKeyDown so you can hover over an img on the other side of the screen, press space...
Read more >Buttons: Why Space and Enter? Why different event listeners?
If you're dealing with regular native <button> elements, it's sufficient to just use the "click" event listener. The browser will fire the ...
Read more >Brief Note on Buttons, Enter, and Space - Adrian Roselli
A native <button> fires on key down when that key is Enter . If you hold down the Enter key, it continues to...
Read more >Testing Key, Mouse and other DOM events - Vue Test Utils
The Wrapper exposes an async trigger method. It can be used to trigger DOM events. test('triggers a click', async () => { const...
Read more >user-event v13 - Testing Library
Note that click will trigger hover events before clicking. To disable this, set the skipHover option to true .
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That makes sense to me, though I think having a modifier (like
{space}
) would be useful. Would you be interested in adding support for that?Thanks for the recap! If it is okay for you, I would try to also correct the bug in the same PR.
Actually a button is not the only element that has a
value
that should not be changed by typing.We also need to consider als
<input>
elements with one of the following types:I will include these in the fix as well.