-
Notifications
You must be signed in to change notification settings - Fork 273
Adding getByTextWithType
#569
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
Hey @pvinis nice to see you here :) I think that However, we could expose a way to write custom queries, pushing the maintenance and compatibility responsibility to the users. Would you be interested in that? We're discussing exposing root ReactTestInstance, which you could use to construct your query. We could also expose some helpers to make writing such custom queries easier for you. |
(Sorry for the random titled :p I fixed it.) Hello! Mm, the I think it makes sense to expose this and allow everyone to have their custom queries. I wonder how they would be accessed then. What I don't know is:
For this PR specifically, I'd be ok with adding the |
I can imagine something like this: const { root } = render(<Comp />);
const customQuery = getByTextWithType(root);
customQuery("text", Button);
// or
getByTextWithType(root)("text", Button) |
Or this: const customRender = render<typeof getByTextWithType>(<Comp />, {extendWithQueries: [getByTextWithType]});
const {getByTextWithType} = customRender(<Comp />) So we wouldn't have to directly expose |
Yep, that's a good way. We have a wrapper for |
I'll close this for now. |
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.