Skip to content

fireEvent.keyPress() is not working #1258

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

Closed
ak-spotter opened this issue Sep 11, 2023 · 3 comments
Closed

fireEvent.keyPress() is not working #1258

ak-spotter opened this issue Sep 11, 2023 · 3 comments

Comments

@ak-spotter
Copy link

I have a checkbox which I want to test using keyPress. The checkbox should be checked on pressing the Space key.
Here's my Component file-

export default function App() {
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <input type="checkbox" data-testid="testing-it" />
    </div>
  );
}

And the test file-

import { fireEvent, render } from "@testing-library/react";
import "@testing-library/jest-dom";

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();
    console.log("testCheckBox...", testCheckBox);

    fireEvent.focus(testCheckBox);
    fireEvent.keyPress(testCheckBox, {
      keyCode: 32,
      charCode: 32,
      key: " ",
      code: "Space"
    });

    expect(testCheckBox).toBeChecked(); // this fails
  });
});

The test fails at the second expect statement- expect(testCheckBox).toBeChecked(); However it should have passed as I focused the checkbox and pressed the Space key.

CodeSandbox link- https://codesandbox.io/s/cool-star-6k6ghp

@MatanBobi
Copy link
Member

Hi @ak-spotter, thanks for opening this.
AFAIR, in order to check a checkbox, you need to run a click event and not keypress.
If you'll add a callback for the onKeyPress on the input, you'll see that it is being called.
Other than that, I highly suggest using user-event for these kind of interactions. fireEvent only dispatches the event and doesn't handle any behavior.
I understand why this can be confusing but this is the expected behavior in this case.

@MatanBobi MatanBobi closed this as not planned Won't fix, can't repro, duplicate, stale Sep 11, 2023
@ak-spotter
Copy link
Author

ak-spotter commented Sep 11, 2023

Hi @MatanBobi
Thanks for the quick response.🙂
When I manually hit the Space key on the UI after focusing the checkbox, it gets checked. So I was expecting the same behaviour in the test too.
I also tried using userEvent instead of fireEvent, but it didn't work.

I actually want to test a draggable element of react-beautiful-dnd using keyPress() or keyDown(). The checkbox was just a simpler example. I saw an answer on StackOverflow explaining how to test the drag behaviour using keyDown() https://stackoverflow.com/a/74068706/7533126 and I tried the same, but it didn't work.

@MatanBobi
Copy link
Member

Got you.
It does look like the onKeyPress is being called but the checkbox isn't being checked.
I think that it's probably some spec decision and not something to do with testing-library (probably somewhere in JSDOM).
If there's a specific issues with react-beautiful-dnd, I suggest trying to understand how to test that component, either by opening an issue in their repo or using stackoverlfow.
If you still believe there's an issue with testing-library, please feel free to comment here and I'll re-open :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants