-
Notifications
You must be signed in to change notification settings - Fork 470
fireEvent.keyPress is not working (ignored or not being triggered) #405
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
Comments
This one's got me stumped. I don't have time to look at it right now, but if anyone wants to do a little digging that would be appreciated! |
I dig into the code and now I know at least the reason.
And they are expecting to have a As I fire the event without it in the params Now, I think the solution might be just get the target from the HTMLElement. And if we pass the |
Thanks for investigating. I'm not sure I understand though. Could you clarify your suggestion? |
Sure @kentcdodds . What I meant is that, when we use I will see if I have some time today to try it. I let you know. Maybe I could contribute with your project :) |
I'm still very confused. If you could make either a codesandbox with your suggested workaround or a pull request work your suggested fix that would probably help. |
I was also confused by how @maufarinelli was wording this, so here's my attempt at rewording what he's suggested. Hope it helps:
|
Thanks for the help @NateRadebaugh, I'm not sure that will solve the issue. Did you try that locally to verify it works? If so, feel free to open a pull request on it. Thanks! |
@maufarinelli What mocked function? Assuming you expect Also:
|
I'm also experiencing this. Here's an even simpler example: https://codesandbox.io/s/react-testing-library-demo-forked-xn4f8 For posterity since I didn't make an account on codesandbox:
|
The fix is to set the charCode in the options: |
I tried replacing |
There's nothing actionable for us to do. If you want to test this behavior I recommend you use a end-to-end testing setup with e.g. https://playwright.dev/. |
Since we need ```charCode: 13``` to call ```fireEvent.keyDown()``` using 'Enter' key and a lot of users need to test when 'Enter' key got pressed, I'm proposing this change on this library docs to help more people on this. reference: testing-library/dom-testing-library#405
I am still facing the same issue, none of the solutions worked. |
@ak-spotter - In order for us to help, please open a new issue and provide a CodeSandbox (https://react.new/), or a link to a repository on GitHub. |
@MatanBobi Here's my code- App.tsx- function App() {
return (
<div className="App">
<input type="checkbox" data-testid="testing-it" />
</div>
);
}
export default App; App-test.tsx import { fireEvent, render } from '@testing-library/react';
import App from './App';
describe('App', () => {
it('should work as expected', async () => {
const { getByTestId } = render(<App />);
const testCheckBox = getByTestId('testing-it');
expect(testCheckBox).not.toBeChecked();
fireEvent.focus(testCheckBox);
fireEvent.keyPress(testCheckBox, { keyCode: 32, charCode: 32, key: ' ', code: 'Space' });
expect(testCheckBox).toBeChecked(); // this fails
});
}); CodeSandbox link- https://codesandbox.io/s/cool-star-6k6ghp |
@MatanBobi thanks for you response. I have opened a new issue- #1258 |
🙏 🙏 🙏 🙏 🙏 🙏 🙏 🙏 🙏 🙏 🙏 🙏 🙏 |
DOM Testing Library
version: "@testing-library/dom": "^6.10.1" and "@testing-library/react": "^9.3.2",`node
version: v12.13.1`npm
(oryarn
) version: yarn 1.19.1`Relevant code or config:
What you did:
I am trying to test a small component which contains an input type="text"
If I
fireEvent.change(input, { target: { value: 'test' } });
I can expect its value and it is changed totest
as expectedBut if I
fireEvent.keyPress(input, { key: 'Enter', code: 13 });
, my mocked function is never calledWhat happened:
fireEvent.keyPress(input, { key: 'Enter', code: 13 });
mocked function is never called. Looks like it ignores the eventReproduction:
https://codesandbox.io/s/react-testing-library-demo-2l41s
Problem description:
fireEvent keyPress is not working
The text was updated successfully, but these errors were encountered: