-
Notifications
You must be signed in to change notification settings - Fork 274
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,12 +20,29 @@ const getNodeByText = (node, text) => { | |
const textChildren = getChildrenAsText(node.props.children, Text); | ||
if (textChildren) { | ||
const textToTest = textChildren.join(''); | ||
return typeof text === 'string' | ||
? text === textToTest | ||
: text.test(textToTest); | ||
if (typeof text === 'string') { | ||
if (text === textToTest) { | ||
return { exact: true }; | ||
} else { | ||
if (textToTest.includes(text)) { | ||
return { | ||
exact: false, | ||
text, | ||
}; | ||
} | ||
|
||
return null; | ||
} | ||
} else { | ||
return text.test(textToTest) | ||
? { | ||
exact: true, | ||
} | ||
: null; | ||
} | ||
} | ||
} | ||
return false; | ||
return null; | ||
} catch (error) { | ||
throw createLibraryNotSupportedError(error); | ||
} | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Since we switch from |
||
getNodeByText(node, text) | ||
); | ||
if (matchingInstances.length === 0) { | ||
return null; | ||
} | ||
|
||
const matches = matchingInstances.map((instance: ReactTestInstance) => ({ | ||
instance, | ||
...getNodeByText(instance, text), | ||
})); | ||
|
||
// Sort the matches from the best to the worst (exact matches should come first, then closest | ||
// matching text) | ||
matches.sort((firstMatch, secondMatch) => { | ||
if (firstMatch.exact) { | ||
return -1; | ||
} | ||
if (secondMatch.exact) { | ||
return 1; | ||
} | ||
return secondMatch.text.length - firstMatch.text.length; | ||
}); | ||
|
||
return matches[0].instance; | ||
}; | ||
|
||
export const getByText = (instance: ReactTestInstance) => | ||
function getByTextFn(text: string | RegExp) { | ||
try { | ||
return instance.find((node) => getNodeByText(node, text)); | ||
} catch (error) { | ||
throw new ErrorWithStack(prepareErrorMessage(error), getByTextFn); | ||
const match = nonErroringGetByText(instance)(text); | ||
if (match) { | ||
return match; | ||
} | ||
|
||
throw new ErrorWithStack( | ||
`No instances found with text: ${String(text)}`, | ||
getByTextFn | ||
); | ||
}; | ||
|
||
export const getByPlaceholderText = (instance: ReactTestInstance) => | ||
|
@@ -148,9 +198,29 @@ export const getByTestId = (instance: ReactTestInstance) => | |
} | ||
}; | ||
|
||
export const getAllByText = (instance: ReactTestInstance) => | ||
export const getAllByText = (rootInstance: ReactTestInstance) => | ||
function getAllByTextFn(text: string | RegExp) { | ||
const results = instance.findAll((node) => getNodeByText(node, text)); | ||
const results = rootInstance.findAll((instance) => { | ||
// We want to match only if there's no better match down the tree. | ||
const match = getNodeByText(instance, text); | ||
if (match && match.exact === false) { | ||
const matchingInstances = instance.findAll((node) => | ||
getNodeByText(node, text) | ||
); | ||
if (matchingInstances.length === 0) { | ||
return false; | ||
} | ||
|
||
if (matchingInstances.length === 1) { | ||
return true; | ||
} | ||
|
||
// There's more than 1 match, that means that down the tree there will be a better matching node | ||
return false; | ||
} | ||
|
||
return Boolean(match); | ||
}); | ||
if (results.length === 0) { | ||
throw new ErrorWithStack( | ||
`No instances found with text: ${String(text)}`, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.