Closed
Description
Describe the feature you'd like:
I'd like to introduce getClosestBy*
as a way to find an element that is the known element or some ancestor of it that matches one of they testing-library
queries.
For example, given the following markup, I'd like to be able to get the button by its text.
<button>
<span>A</span>
</button>
<button>
<span>B</span>
</button>
getClosestByRole(getByText("B"), "button")
Suggested implementation:
Recursively walk up from the node and find the first node that matches.
Describe alternatives you've considered:
Use native closest
:
getByText("B").closest("button");
Walk the tree myself:
getByText("B").parentNode
Get by a known index:
getAllByRole('button')[1]
Abuse a TextMatcher:
getByRole((role, element) => role === 'button' && element.textContent === "B")
Teachability, Documentation, Adoption, Migration Strategy:
getClosestBy*
would be togetBy*
as closest
is to querySelector
.
Metadata
Metadata
Assignees
Labels
No labels