Skip to content

feat(ByRole): Add support for fallback roles #418

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

Merged
merged 2 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/__tests__/element-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ test('queryAllByRole returns semantic html elements', () => {
</tbody>
</table>
<table role="grid"></table>
<div role="meter progressbar" />
<button>Button</button>
</form>
`)
Expand All @@ -433,6 +434,9 @@ test('queryAllByRole returns semantic html elements', () => {
expect(queryAllByRole(/rowgroup/i)).toHaveLength(2)
expect(queryAllByRole(/(table)|(textbox)/i)).toHaveLength(3)
expect(queryAllByRole(/img/i)).toHaveLength(1)
expect(queryAllByRole('meter')).toHaveLength(1)
expect(queryAllByRole('progressbar')).toHaveLength(0)
expect(queryAllByRole('progressbar', {queryFallbacks: true})).toHaveLength(1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(queryAllByRole('progressbar', {queryFallbacks: true})).toHaveLength(1)
expect(queryAllByRole('meter')).toHaveLength(1)
expect(queryAllByRole('progressbar')).toHaveLength(0)
expect(queryAllByRole('progressbar', {queryFallbacks: true})).toHaveLength(1)

})

test('getAll* matchers return an array', () => {
Expand Down Expand Up @@ -471,6 +475,7 @@ test('getAll* matchers return an array', () => {
<option value="volvo">Toyota</option>
<option value="audi">Honda</option>
</select>
<div role="meter progressbar" />
</div>,
`)
expect(getAllByAltText(/finding.*poster$/i)).toHaveLength(2)
Expand All @@ -482,6 +487,8 @@ test('getAll* matchers return an array', () => {
expect(getAllByDisplayValue(/cars$/)).toHaveLength(2)
expect(getAllByText(/^where/i)).toHaveLength(1)
expect(getAllByRole(/container/i)).toHaveLength(1)
expect(getAllByRole('meter')).toHaveLength(1)
expect(getAllByRole('progressbar', {queryFallbacks: true})).toHaveLength(1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(getAllByRole('progressbar', {queryFallbacks: true})).toHaveLength(1)
expect(getAllByRole('meter')).toHaveLength(1)
expect(getAllByRole('progressbar', {queryFallbacks: true})).toHaveLength(1)

})

test('getAll* matchers throw for 0 matches', () => {
Expand Down Expand Up @@ -658,6 +665,18 @@ test('using jest helpers to check element role', () => {
expect(getByRole('dialog')).toHaveTextContent('Contents')
})

test('using jest helpers to check element fallback roles', () => {
const {getByRole} = render(`
<div role="meter progressbar">
<span>Contents</span>
</div>
`)

expect(getByRole('progressbar', {queryFallbacks: true})).toHaveTextContent(
'Contents',
)
})

test('test the debug helper prints the dom state here', () => {
const originalDebugPrintLimit = process.env.DEBUG_PRINT_LIMIT
const Large = `<div>
Expand Down
10 changes: 10 additions & 0 deletions src/__tests__/queries.find.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ test('find asynchronously finds elements', async () => {
<img alt="test alt text" src="/lucy-ricardo.png" />
<span title="test title" />
<div role="dialog"></div>
<div role="meter progressbar"></div>
</div>
`)
await expect(findByLabelText('test-label')).resolves.toBeTruthy()
Expand All @@ -56,6 +57,15 @@ test('find asynchronously finds elements', async () => {
await expect(findByRole('dialog')).resolves.toBeTruthy()
await expect(findAllByRole('dialog')).resolves.toHaveLength(1)

await expect(findByRole('meter')).resolves.toBeTruthy()
await expect(findAllByRole('meter')).resolves.toHaveLength(1)
await expect(
findByRole('progressbar', {queryFallbacks: true}),
).resolves.toBeTruthy()
await expect(
findAllByRole('progressbar', {queryFallbacks: true}),
).resolves.toHaveLength(1)
Comment on lines +62 to +67
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't make a suggestion in GitHub for this, but I would suggest this change to:

  await expect(findByRole('meter')).resolves.toBeTruthy()
  await expect(findAllByRole('meter')).resolves.toHaveLength(1)
  await expect(findByRole('progressbar', {queryFallbacks: true})).resolves.toBeTruthy()
  await expect(findAllByRole('progressbar', {queryFallbacks: true})).resolves.toHaveLength(1)

Thanks!


await expect(findByTestId('test-id')).resolves.toBeTruthy()
await expect(findAllByTestId('test-id')).resolves.toHaveLength(1)
})
Expand Down
16 changes: 15 additions & 1 deletion src/queries/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function queryAllByRole(
hidden = getConfig().defaultHidden,
trim,
normalizer,
queryFallbacks = false,
} = {},
) {
const matcher = exact ? matches : fuzzyMatches
Expand All @@ -40,7 +41,20 @@ function queryAllByRole(
const isRoleSpecifiedExplicitly = node.hasAttribute('role')

if (isRoleSpecifiedExplicitly) {
return matcher(node.getAttribute('role'), node, role, matchNormalizer)
const roleValue = node.getAttribute('role')
if (queryFallbacks) {
return roleValue
.split(' ')
.filter(Boolean)
.some(text => matcher(text, node, role, matchNormalizer))
}
// if a custom normalizer is passed then let normalizer handle the role value
if (normalizer) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a test specifically covering this case? We should.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it is covered in text-matcher.js.

If this is not the test case you're talking about then can you please guide me where to add the test case for it? That will be very helpful :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kentcdodds ping you to remind again.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. That's fine. Thank you!

return matcher(roleValue, node, role, matchNormalizer)
}
// other wise only send the first word to match
const [firstWord] = roleValue.split(' ')
return matcher(firstWord, node, role, matchNormalizer)
}

const implicitRoles = getImplicitAriaRoles(node)
Expand Down