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

Add option to *ByText to search by innerText #1216

Open
steinybot opened this issue Feb 2, 2023 · 1 comment
Open

Add option to *ByText to search by innerText #1216

steinybot opened this issue Feb 2, 2023 · 1 comment

Comments

@steinybot
Copy link

Describe the feature you'd like:

I'd like to have an includeDescendents option to *ByText which would effectively be the same as searching by innerText (but it's not that simple since it needs to respect the ignore option).

I'm new to testing-library and have more familiarity with Cypress. I was hoping to find similar behaviour to cy.contains.

Suggested implementation:

Describe alternatives you've considered:

cy.contains but this is always an { exact: false} match which is usually less than ideal.

Teachability, Documentation, Adoption, Migration Strategy:

Given this:

      <p>
        Refer to the
        <a
          href="https://testing-library.com/docs/dom-testing-library/api-custom-queries"
          >Custom Queries</a
        >
        section for more details.
      </p>

Then this:

findByText('Refer to the Custom Queries section for more details.', { includeDescendents: true })

Should match the p node.

@MatanBobi
Copy link
Member

As a general idea, the getBy* queries can accept a function that will be called for every element you're rendering and let you query the way you want.
You can have a look at this solution that searches for textContent and gives you the direct parent that has the content you're looking for. Hope it will help :)

@MatanBobi MatanBobi transferred this issue from testing-library/react-testing-library Feb 17, 2023
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

2 participants