Skip to content
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

getClosestBy* #327

Closed
mlrawlings opened this issue Jul 25, 2019 · 4 comments
Closed

getClosestBy* #327

mlrawlings opened this issue Jul 25, 2019 · 4 comments

Comments

@mlrawlings
Copy link

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.

@alexkrolick
Copy link
Collaborator

alexkrolick commented Jul 25, 2019

There's been some discussion of this idea and its trade-offs in a few threads already: #204 (comment)

Can you describe what this API would allow you do to that you can't do already? Is it testing attributes on the parent nodes?

@adamduncan
Copy link

adamduncan commented Jan 15, 2020

Understand that specifying tags/attributes in queries is testing implementation details, but there are some cases where this might be the most sane way inferring behaviour? As you say, testing attributes on parent nodes.

E.g. Ensuring a button with nested span is disabled:

<button disabled><span>Do thing</span></button>

// findByText(/Do thing/).should('have.attr', 'disabled') fails
// findByText(/Do thing/).closest('button').should('have.attr', 'disabled') would work but adds another implementation detail?

@kentcdodds had mentioned a hypothetical solution in #204, suggesting a closestInteractiveElement option, but didn't sound too keen on it. Would there be something along those lines be worth exploring in future?

@eps1lon
Copy link
Member

eps1lon commented Jan 15, 2020

E.g. Ensuring a button with nested span is disabled:

This particular instance will be covered by #408. When this lands you'll be able to query the button with getByRole('button', { name: 'Do thing' })

@kentcdodds
Copy link
Member

Yup, closing this in favor of #408. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants