Skip to content

*ByText cannot find text in React fragments when passed into the rendered element as a child component #1513

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
islazh opened this issue Oct 12, 2023 · 4 comments

Comments

@islazh
Copy link

islazh commented Oct 12, 2023

Describe the bug

*ByText methods cannot find text in fragments when the fragment is passed into <Text /> as a child component.

Expected behavior

getByText should be able to find the text that is passed into <Text/> component as a React fragment child component.

Steps to Reproduce

Minimal failing case:

    const BananaText = () => <>{'BANANA'}</>;
    const componentWithNonWorkingExpectation = render(
      <Text>
        <BananaText />
      </Text>,
    );

    // this fails unexpectedly
    expect(componentWithNonWorkingExpectation.getByText('BANANA')).toBeTruthy();

The text is present when debug() is used and is also present in the application.

As an aside, *ByText can find text in fragments when directly inside a component, e.g.

    const componentWithWorkingExpectation = render(
      <Text><>{'BANANA'}</></Text>,
    );
   // this passes
    expect(componentWithWorkingExpectation.getByText('BANANA')).toBeTruthy();

Versions

11.2.0

@pierrezimmermannbam
Copy link
Collaborator

Hello @islazh, this test works on the current version, I believe it was fixed by #1222 so it should work if you upgrade to 11.5.0, let me know if upgrading does fix your problem

@islazh
Copy link
Author

islazh commented Oct 13, 2023

Great, thank you!

@pierrezimmermannbam
Copy link
Collaborator

You're welcome! I'm closing this issue then, will reopen if your problem persists after upgrading

@mdjastrzebski
Copy link
Member

mdjastrzebski commented Oct 13, 2023

I've checked locally in the latest RNTL and it works now. Both for:

  • const BananaText = () => <>{'BANANA'}</>;
  • const BananaText = () => 'BANANA';

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

3 participants