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.

Support "enter" keydown on button

See original GitHub issue

In a browser, enter keydown trigger the click event of the button.

It would be nice if the user-event library support this behavior.

You can test the browser behavior here: https://codesandbox.io/s/jovial-glitter-jy883?from-embed

A naive implementation would be to add a keyDown function to user-event:

    keyDown(element, options) {
        fireEvent.keyDown(element, options);

        if (element.tagName === "BUTTON") {
            fireEvent.click(element);
        }
    }

Thank you,

Patrick

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

62reactions
kentcdoddscommented, Jun 8, 2020

This is now supported with userEvent.type(button, '{enter}')

7reactions
ahayes91commented, May 12, 2022

Feel free to correct me if there’s another way to do it, but I found userEvent.type(button, '{enter}') didn’t work for me today to fire the onClick event on a button (maybe this has changed since this ticket was implemented).

I stripped it down to a regular old button and found focusing the button and using userEvent.keyboard('{enter}') was the way to do it (still on user-event v 13.5, FYI):

  it("Using Enter key on regular button should fire onClick event", async () => {
    const mockOnClick = jest.fn();
    render(<button onClick={mockOnClick}>Button</button>);
    const button = screen.getByRole("button");
    button.focus();
    userEvent.keyboard("{enter}");
    await waitFor(() => expect(mockOnClick).toHaveBeenCalledTimes(1));
  });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Trigger a button click with JavaScript on the Enter key in a text ...
Problem with using keydown is that holding down enter will fire the event over and over again. if you attach to the keyup...
Read more >
How To Trigger Button Click on Enter - W3Schools
Trigger a button click on keyboard "enter" with JavaScript. Trigger a Button Click on Enter. Press the "Enter" key inside the input field...
Read more >
How to Trigger Button Click on Enter Key Press using JavaScript
Trigger button click on Enter key press on the keyboard. Use KeyboardEvent keyCode property to trigger button click for submitting form or ...
Read more >
KeyboardEvent.key - Web APIs | MDN
A keydown event is first fired. If the key is held down further and the key produces a character key, then the event...
Read more >
Trigger a button click on Enter key with JavaScript/jQuery
The idea is to bind an event handler to the keyup JavaScript event using jQuery's .keyup(handler) method and use that handler to check...
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