Description
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 to aria
roles (i.e. using aria-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:
const createIssueBtn = getByText(/create issue/i)
expect(createIssueBtn).toHaveRole('button')
fireEvent.click(createIssueBtn)
const createIssueHeader = getByText(/create issue/i)
expect(createIssueHeader).toHaveRole('header')
Instead, the opposite can only be done (i.e. getByRole
, then assert with toHaveTextContent
). 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:
const utils = render(<App />)
const createIssueBtn = utils.getByText(/create issue/i)
// Since the heading on the page we're going to also has the text "create issue", we need to be a little dirty and make sure this element is a button
expect(createIssueBtn.tagName).toBe('BUTTON')
fireEvent.click(createIssueButton)
// Wait for the "create issue" element to be a heading instead of a button -- necessary since the click won't trigger the page change immediately
await wait(() =>
expect(utils.getByRole('heading')).toHaveTextContent(/create issue/i)
)
const createIssueHeader = utils.getByText(/create issue/i)
// Again, just being paranoid and making sure this element isn't just the button that's on the home page
expect(examSchedulerHeader.tagName).not.toBe('BUTTON')