Skip to content
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

Using fireEvent.change() on a select element fires the event handler, but doesn't update state. #908

Open
Taelkir opened this issue May 6, 2021 · 14 comments
Labels
bug Something isn't working

Comments

@Taelkir
Copy link

Taelkir commented May 6, 2021

  • @testing-library/react version: @testing-library/react@11.2.5
  • Testing Framework and version: jest through react-scripts@4.0.2
  • DOM Environment: jsdom@16.4.0 (through react-scripts)
  • node v12.18.0

Relevant code or config:

  const selectOne = screen.getByRole("combobox", { name: "My select" });
  fireEvent.change(selectOne, {
    target: { value: "OPTION1" }
  });

  expect(screen.getByText("OPTION1")).toBeInTheDocument();

What you did:

Attempting to change a <select> element and to check that different content is displayed based on what is selected. This works in a browser, but I can't get the test to recognise that.

What happened:

console.log() statements in the event handler of my component shows that the state is not changing; the fact these statements are logging at all show that the handler is being fired.

Reproduction:

Codesandbox here: https://codesandbox.io/s/react-testing-library-demo-forked-v09xi?file=/src/App.js

I've stripped down my TS project here to try and pin down the problem and am getting the same failing test behaviour.

Problem description:

I would expect the tests to change the app state in the same way as the browser does.

Suggested solution:

I'm not sure what's wrong; the event is happening, it's just the state then seems to reset to its original value.

@eps1lon
Copy link
Member

eps1lon commented May 7, 2021

Thanks for the report.

This is probably an old issue. It's caused due to the timing of when the state updater function runs.

If you capture the value, the test works as expected:

handleSelectChange = (event) => {
+   const value = event.target.value;
    this.setState(
      () => {
-       return { selectValue: event.target.value };
+       return { selectValue: value };
      },
      () => {
        console.log("State updated to: ", this.state.selectValue);
      }
    );
  };

@Taelkir
Copy link
Author

Taelkir commented May 7, 2021

Yep, that workaround is good for me in my project. Thanks!

@eps1lon
Copy link
Member

eps1lon commented May 7, 2021

Just to be clear: The code is working fine with manual testing in a browser. It only fails with our fireEvent.change implementation. So we do want to keep an eye on this bug.

@eps1lon eps1lon added the bug Something isn't working label May 7, 2021
@davelosert

This comment was marked as off-topic.

@gugu

This comment was marked as off-topic.

@gugu

This comment was marked as off-topic.

@eps1lon

This comment was marked as resolved.

@gugu

This comment was marked as off-topic.

@JuanjoOrt

This comment was marked as off-topic.

@gugu

This comment was marked as off-topic.

@demo-Ghost
Copy link

demo-Ghost commented Sep 15, 2022

Hi. I'm using react-select in my project and can't seem to find a way to capture the event properly. This happens due to the fact I don't have access to the event in the scope of the onChange function.

Is there another way for this to work, other than grabbing the event in the handleChange funtion?

Edit: I'm really new to react-testing-library, so if there is another way to override this behavior from fireEvent.change on a select component, please let me know. I do have to use react-select because of a restriction on the project, but any react-testing-library API will do.

Thanks in advance!

@demo-Ghost
Copy link

Imported the react-select-event library and it magically worked. It's also mentioned in the react testing library docs in the ecosystem section.

https://testing-library.com/docs/ecosystem-react-select-event

@fattylee2021
Copy link

@eps1lon thanks, this workaround works for me

@sanathp
Copy link

sanathp commented Dec 22, 2022

Using userEvent instead of fireEvent worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

8 participants