Skip to content

Add new getByRole queryselector. #93

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

Closed
JeffBaumgardt opened this issue Sep 4, 2018 · 4 comments
Closed

Add new getByRole queryselector. #93

JeffBaumgardt opened this issue Sep 4, 2018 · 4 comments

Comments

@JeffBaumgardt
Copy link
Contributor

Describe the feature you'd like:

A new querySelector to select elements based on role. I have a dialog that while I can query inside the dialog just fine I cannot first a click to close the dialog because all the current queries target elements inside the dialog so the out of bounds click handlers do not fire.

Suggested implementation:

const queryAllByRole = queryAllByAttribute.bind(null, 'role')

const queryByRole = queryByAttribute.bind(null, 'role')
const queryAllByRole = queryAllByAttribute.bind(null, 'role')

function getAllByRole(container, id, ...rest) {
  const els = queryAllByRole(container, id, ...rest)
  if (!els.length) {
    throw getElementError(
      `Unable to find an element by role=${id}`,
      container,
    )
  }
  return els
}

function getByRole(...args) {
  return firstResultOrNull(getAllByRole, ...args)
}

Describe alternatives you've considered:

I'm using Material UI where there is a lot of abstraction in DOM that I can't put data-testid on the outer dialog. So targeting it by testid won't work for triggering the onClose event.

The only thing I've considered is writing the
const dialogConstainer = container.querySelector('[role="dialog"]')
And then writing assertions from there.

Teachability, Documentation, Adoption, Migration Strategy:

This is a non breaking change as it is only an addition to the existing query set.


getByRole

getByRole(
  container: HTMLElement,
  text: TextMatch,
  options?: {
    exact?: boolean = true,
    collapseWhitespace?: boolean = false,
    trim?: boolean = true,
  }): HTMLElement`

A shortcut to container.querySelector(`[role="${yourRole}"]`) (and it
also accepts a TextMatch).

// <div role="dialog">...</div>
const dialogContainer = getByTestrole(container, 'dialog')
@gnapse
Copy link
Member

gnapse commented Sep 4, 2018

Funny, I was thinking something similar now after the recent discussion in testing-library/jest-dom#55. It would be a nice addition.

@kentcdodds
Copy link
Member

Thanks for the thorough feature request @JeffBaumgardt! I'm in favor of this. Would you like to implement it?

@JeffBaumgardt
Copy link
Contributor Author

Can do I'll have a PR later today.

@JeffBaumgardt
Copy link
Contributor Author

Closed via #94

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

3 participants