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

react-test-renderer findAllByProps returns duplicate results for RN components #16264

Closed
TAGraves opened this issue Oct 9, 2017 · 2 comments
Closed
Labels
Ran Commands One of our bots successfully processed a command. Resolution: Locked This issue was locked by the bot.

Comments

@TAGraves
Copy link

TAGraves commented Oct 9, 2017

Reproducible Example: https://github.com/TAGraves/react-test-renderer-bug-example (just yarn install then yarn test.
Using:
react => 16.0.0
react-native => 0.49.3
react-test-renderer => 16.0.0

I'm trying to use some of the new utilities provided by react-test-renderer to make assertions against a RN component hierarchy, but unfortunately the way RN mocks components when using Jest makes this a futile attempt. findAllByProps is returning twice as many elements as it should. I've attached a super simple example at the top (rendering just a single <View /> but getting two results from findAllByProps).

The issue stems from mockComponent.js. When it mocks a component out, its render returns another component with the same name and props. So if your intended hierarchy looks like:

<View testID="a">
    <SomeComponent />
</View>

the outputted hierarchy will be:

<View testID="a">
    <View testID="a">
      <SomeComponent />
    </View>
</View>

I believe this can be fixed by changing the mocked implementation just to:

  const Component = class extends RealComponent {
    render() {
      return this.props.children || null;
    }
  };

and I am happy to submit a PR with this change, but would like some guidance on if there's a reason for the existing approach before I go changing things.

Thank you!

@pull-bot
Copy link

@facebook-github-bot no-template

@facebook-github-bot
Copy link
Contributor

Hey, thanks for reporting this issue! It looks like your description is missing some necessary information, or the list of reproduction steps is not complete. Can you please add all the details specified in the Issue Template? This is necessary for people to be able to understand and reproduce the issue being reported. I am going to close this, but feel free to open a new issue with the additional information provided. Thanks! See "What to Expect from Maintainers" to learn more.

@facebook-github-bot facebook-github-bot added the Ran Commands One of our bots successfully processed a command. label Oct 10, 2017
@facebook facebook locked as resolved and limited conversation to collaborators Oct 10, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Oct 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Ran Commands One of our bots successfully processed a command. Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

4 participants