question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getByRole should accept a second argument to refine query as in react-testing-library

See original GitHub issue

Describe the Feature

Should be able to query a Button element as follows:

getByRole('button', {name: /submit/i})

At the present time, this is impossible so I have to add redundant accessiblityLabel props, for example:

<Button accessibilityLabel="submit-button">Submit</Button>

And then, use queryByA11yLabel('submit-button')

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
AugustinLFcommented, Nov 25, 2021

A thing you’ll need to consider is that if you match by name, you should also match by label text, since you should definitely match <Button accessibilityLabel="exit" onPress={...}><Icon type="door" /></Button> with getByRole('button', {name: 'exit'}).

Not necessarily relevant but which might be interesting to you, we’ve reimplemented that in userland in our codebase. As you can see the reasoning is the same (first find the element with the role, then try to match the name in its children). Since it does work for us (and does seem to work as well as web), I guess your implementation should do the job.

const queryByAccessibleName = (renderApi: Queries, name: string) =>
  renderApi.queryByLabelText(name) || renderApi.queryByText(name)

const getByRole = (role: AccessibilityRole, options: { name?: string } = {}) => {
      const elements = queryAllByAccessibleName(renderResult, name)

      const e = new Error(
        `Unable to find an accessible element with the role "${role}" and name "${name}"`
      )
      e.name = 'TestingLibraryElementError'
      Error.captureStackTrace(e, getByRole)

      if (elements.length === 0) {
        throw e
      }

      const elementWithRole = elements.find((element) => element.props.accessibilityRole === role)
      if (elementWithRole) {
        return elementWithRole
      }

      // a text can be nested within a button, so the accessibilityRole wouldn't be on the same
      // element
      try {
        const roledElements = renderResult.getAllByRole(role)
        for (const roledElement of roledElements) {
          const withinRoledElement = nativeWithin(roledElement)
          const labelledTextWithinRoledElement = queryByAccessibleName(withinRoledElement, name)
          if (labelledTextWithinRoledElement) {
            return labelledTextWithinRoledElement
          }
        }
        return
      } catch {
        throw e
      }

      throw e
}
0reactions
kiranjdcommented, Dec 2, 2021

@thymikee Issued a draft PR. Appreciate your feedback

Read more comments on GitHub >

github_iconTop Results From Across the Web

ByRole | Testing Library
It can be used to query a specific element if multiple elements with the same role are present on the rendered content.
Read more >
ByRole Queries in React Native Testing Library - YouTube
Big thanks to my employer, Test Double, for supporting my streaming! Find out more about how we can help you on your next...
Read more >
getByRole method - RenderResult class - rtl.react library
Returns a single element with the given role value, defaulting to an exact match. Throws if no element is found. Use queryByRole if...
Read more >
How to get second item in getByRole in React Testing Library ...
Use screen.getAllByRole('button')[1]. https://testing-library.com/docs/dom-testing-library/cheatsheet/#queries.
Read more >
Testing - Recoil
It can be helpful to test Recoil state when testing a component. You can compare the new state against expected values using this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found