Skip to content

fix: add React.Fragment support for matching text #663

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
wants to merge 2 commits into from

Conversation

lachiet
Copy link

@lachiet lachiet commented Feb 5, 2021

Summary

When using getByText() on a custom component using nested Fragments, it should return the text content within the fragments.

const CustomText = () => {
  return <React.Fragment>Hello World</React.Fragment>;
};

<Text>
  <CustomText />
</Text>

Test plan

I've added an additional test and all other tests are passing. Let me know if there's any concerns or other things we can add to protect against regression.

Copy link
Member

@thymikee thymikee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this improvement! What if, instead, we adjust the getChildrenAsText to detect Text with an array of strings?

@lachiet
Copy link
Author

lachiet commented Feb 5, 2021

Thanks for this improvement! What if, instead, we adjust the getChildrenAsText to detect Text with an array of strings?

@thymikee no problem! I initially tried that approach but it seems like I can't access the children or props.children from the child object? This is what I get when I console.log(child) out before this line - any ideas?

{
  '$$typeof': Symbol(react.element),
  type: [Function: CustomText],
  key: null,
  ref: null,
  props: {},
  _owner: null,
   _store: {}
}

@@ -56,6 +56,17 @@ const getNodeByText = (node, text) => {
: text.test(textToTest);
}
}

const isParentTextComponent = filterNodeByType(node.parent?.parent, Text);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking parents non-recursively will result in a failure in e.g. this scenario:

const Wrap = ({ children }) => <>{children}</>;

  const CustomText = () => {
    return <React.Fragment>Hello World</React.Fragment>;
  };

  expect(
    render(
      <Text>
        <Wrap>
          <CustomText />
        </Wrap>
      </Text>
    ).queryByText('Hello World')
  ).toBeTruthy();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check this PR as well: #554 as it's changing the way we're interfacing text to be similar to React Testing Library. Grabbing by text with non-direct string children is tricky and we'll likely need to trade-off some functionality, or come up with a more complex solution.

We want to solve this problem once for good, so if you're willing to help improve the library, please chime in there and see how we can support this scenario with string precision API

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, #554 is now merged. Would you be still interested in pursuing this improvement? 😊

@thymikee
Copy link
Member

Closed via #934

@thymikee thymikee closed this Mar 25, 2022
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

Successfully merging this pull request may close these issues.

2 participants