Adding getByTextWithType
#569
Closed
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.
Summary
This PR adds a much-needed query function by us called
getByTextWithType
.We have found that we often need to look for a string inside a specific component that contains it. For example, a component like
Header
that contains a few texts or our ownButton
component. In our tests we want to be able to find thatHeader
orButton
component, so our options areUNSAFE_getAllByType
and get all theButton
s and reference the one we want to test by index, which feels too fragile, orgetAllByText
and then run up the parent stack to find theButton
for each, which feels like the tests do too much "extra" work.This new query will solve this problem. It is called like so:
getByTextWithType('some text', Button)
(same for the*All*
variation). It works the same as the rest of theget*
andgetAll*
queries for returns and throws, and it will return theButton
component that contains a text with'some text'
in it.The way it works is basically it looks for all texts, same as
getAllByText
and then runs up the parent stack to see if there is a node with typeButton
(the second argument in general). If is exists, then it's a successful result. If not, it is ignored.It could be part of
get*ByText
with an optional second argument for type, but I didn't want to compicate this PR. I'm happy to do that though if people feel it makes it simpler.Test plan
I have added tests for both new queries as well as a couple extra sanity check tests. The tests should make usage and results pretty clear.