-
Notifications
You must be signed in to change notification settings - Fork 274
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
Conversation
There was a problem hiding this 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?
@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
|
@@ -56,6 +56,17 @@ const getNodeByText = (node, text) => { | |||
: text.test(textToTest); | |||
} | |||
} | |||
|
|||
const isParentTextComponent = filterNodeByType(node.parent?.parent, Text); |
There was a problem hiding this comment.
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();
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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? 😊
Closed via #934 |
Summary
When using getByText() on a custom component using nested Fragments, it should return the text content within the fragments.
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.