-
Notifications
You must be signed in to change notification settings - Fork 273
Enable partial matching #546
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
if (text === textToTest) { | ||
return { exact: true }; | ||
} else { | ||
if (textToTest.includes(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.
Using includes
here posed a lot of problem since it means that the parent nodes will also match. Hence the significant update to the logic.
@@ -95,13 +112,46 @@ const getNodeByTestId = (node, testID) => { | |||
: testID.test(node.props.testID); | |||
}; | |||
|
|||
export const nonErroringGetByText = (instance: ReactTestInstance) => | |||
function nonErroringGetByTextFn(text: string | RegExp) { | |||
const matchingInstances = instance.findAll((node) => |
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.
Since we switch from find
to findAll
, the implementation doesn't throw anymore if no match is found. So I updated that so we can still use this implementation in queryByText
(which shouldn't throw).
How does it relate to #541? |
Don't be sorry! Too bad you didn't see the PR earlier. However, if you feel like it, I'd appreciate reviewing that PR to make it right :) |
I'm on it, will run my tests against that implementation to see if it behaves as I'd expect it! |
Summary
Follow up of #489, I'm trying to make the migration from
@5.X
to@7.X
in a fairly large project. Another missed breaking change is that the current implementation doesn't permit "partial matches", as isrender(<Text>Hello World</Text>).getByText('Hello')
will throw. We have a lot of tests relying on this pattern (which is also the way web behaves). In this PR I fixed that.This is technically a breaking change since some people might rely on
getByText
throwing in this case. But I do believe that we should seek for an API as close as possible from@testing-library/react
.Test plan
I added several tests that should cover the different edge cases related to this change.