Skip to content

Element that has been hidden after waiting is still visible #363

@bolivier

Description

@bolivier
  • react-testing-library version: 6.1.2
  • react version: 16.8.6
  • node version: 11.13.0
  • yarn version: 1.15.2

Relevant code or config:

class Hider extends React.Component {
  state = {
    visible: true
  };
  render() {
    return (
      <div>
        <span>{this.props.text || "hi"}</span>
        <button onClick={() => this.setState({ visible: false })}>hide</button>
        <div>{this.props.children}</div>
      </div>
    );
  }
}
class App extends React.Component {
  render() {
    return (
      <div>
        <Hider>
          <span>hello world</span>
        </Hider>
      </div>
    );
  }
}
it("should not work properly", async () => {
  const wrapper = render(<App visible={false} />);
  fireEvent.click(wrapper.getByText("hide"));
  await wait(() => {
    expect(wrapper.queryByText("hide")).not.toBeInTheDocument();
  });
});

What you did:

I was writing to test a modal component that hides children by returning null.

What happened:

Querying the container for the text of the button results in returning the button, even though that button isn't present in the DOM.

The error message is {"location": null} contains: <button />

A snapshot of that error:


Screen Shot 2019-04-24 at 2 28 26 PM

Reproduction:

I created a reproduction of the issue here: https://github.com/bolivier/react-testing-library-reproduction

Problem description:

Event though the components are not present, they are found and the tests fail

Suggested solution:

I'm unsure of a solution. I did notice that everything behaves as expected if there's no component nesting. So if the App component handles the visibility, then the tests will pass.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions