-
Notifications
You must be signed in to change notification settings - Fork 240
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
Support "enter" keydown on button #179
Comments
Hmm, but here you aren't testing for the |
Does that work on all elements though? Or just form controls? |
Good question, I would assume it's a typical button behaviour? |
I wrote my own test and got some interesting results: https://codepen.io/nickmccurdy/pen/RwweGxG?editors=1010 To summarize, all buttons (using |
Okay, thanks for testing this @nickmccurdy! |
This would be super helpful for me as well, due to this issue w/ fireEvent. testing-library/react-testing-library#551 In my case, I have to support a submit-like event for textarea's onKeyPress for "Enter". |
This is now supported with |
@kentcdodds Awesome! Thanks so much :) |
Regarding Lines 109 to 118 in 4bbb4b9
|
It appears that this will also trigger the onClick event of the button before the keypress is triggered. So if we pass the same callback for both onClick and onKeydown, it will be called twice. To avoid this, we need |
Feel free to correct me if there's another way to do it, but I found I stripped it down to a regular old 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));
}); |
Where is userEvent imported from? |
It's the default export from |
FYI: |
There's something fishy about user-event enter keypress, |
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:Thank you,
Patrick
The text was updated successfully, but these errors were encountered: