-
Notifications
You must be signed in to change notification settings - Fork 403
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 toHaveRole #143
Comments
This comment has been minimized.
This comment has been minimized.
Yes, it seems like every query should have a corresponding jest-dom method off topic@connormeredith I think we still want this for general usage even if there is an alternative way for this particular example This exact flow is really common, where a button opens a modal or page with the same text in a heading.
|
This comment has been minimized.
This comment has been minimized.
Yes, wrapping the specific assertion with Can you take the part of the discussion about this specific scenario to https://spectrum.chat/testing-library? The feature request for |
Sure thing 🙂 One last thing in response that I think might still be relevant -- the |
- await wait(() =>
- expect(utils.getByRole('heading')).toHaveTextContent(/create issue/i)
- )
+ expect(await utils.findByRole('heading')).toHaveTextContent(/create issue/i) |
Yeah -- so in that solution |
I'm confused as to what the conclusion was in the above discussion? Do we think we need this matcher? Isn't it |
This is the meat of what I'm getting at. An alternative solution is similar to add a // This would fail if an element that's a heading with the 'create issue' text doesn't exist
const createIssueButton = getByText(/create issue/i, { role: 'heading' }) |
@babramczyk With testing-library/dom-testing-library#408 you'll be able to use |
I came up with a use case for this where I wish I had a role matcher: Query by TestId for robustness and then assert it has a certain role for accessibility. What I wish existed: // tab with user-event to reach the next focusable element
await user.tab()
// use a testId to query the right element
const editHandle = getByTestId(container, 'edit-handle')
// assert the element has an interactive role and receives focus
expect(editHandle).toHaveRole('button')
expect(editHandle).toHaveFocus() The |
@babramczyk |
I think it could be of use to have a If someone wants to contribute with a pull request implementing this, that'd be great. I cannot promise I'll get to it anytime soon. |
There is "Priority" section (https://testing-library.com/docs/queries/about/#priority) where in case with buttons we should use
So @babramczyk initial comment can be rewritten and no needs in Case of @marcysutton can be also rewritten using
|
The problem with… expect(editHandle).toHaveProperty('tagName', 'BUTTON'); …is that such a test will break if for some reason a More commonly, it is possible to use In sum, I do think this could be a useful nice-to-have API. |
At my current workplace, we have the exact "query by test id and assert the role" use-case described above. Using the implicit role is important, to account for shuffling around the implementations in existing applications. Moving away from test ids is not always possible; there are different testing practices and historical reasons for them. All I want to do is provide a way to improve accessibility, without arguing about those :) Long story short, I'm interested in contributing a PR for this! If someone hasn't started on it yet, I'm happy to give it a shot. I glanced at the contributing guide, and the path seems relatively clear to me. If the maintainers want to pitch any ideas or direction, I'm happy to take them into account 😌 |
Describe the feature you'd like:
A
toHaveRole
matcher that uses a similar implementation to@testing-library
's described here. In other words, checking implicit roles in addition toaria
roles (i.e. usingaria-query
).Motivation
The use case that prompted this was a page that had a button (example text "Create Issue") that was supposed to navigate to a page with a heading with the same text ("Create Issue").
Ideally, the test would look like this:
Instead, the opposite can only be done (i.e.
getByRole
, then assert withtoHaveTextContent
). This is a little more fickle, since it relies on these elements being the first of the specified role on the page, which may not always be the case. Querying by text first is much more specific, and hopefully more robust.Concretely, here's what I had to implement as an alternative:
The text was updated successfully, but these errors were encountered: