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.

Native button should trigger click on keydown space

See original GitHub issue

For 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:closed
  • Created 3 years ago
  • Comments:16 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
kentcdoddscommented, Jul 21, 2020

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?

1reaction
visualjerkcommented, Jul 22, 2020

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:

  • button
  • color
  • file
  • image
  • reset
  • submit

I will include these in the fix as well.

Read more comments on GitHub >

github_iconTop 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 >

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