Skip to content

Commit 97141f7

Browse files
committed
feat(ByRole) first word should match for multiple roles
1 parent e2a5f1a commit 97141f7

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

src/__tests__/element-queries.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ test('queryAllByRole returns semantic html elements', () => {
434434
expect(queryAllByRole(/rowgroup/i)).toHaveLength(2)
435435
expect(queryAllByRole(/(table)|(textbox)/i)).toHaveLength(3)
436436
expect(queryAllByRole(/img/i)).toHaveLength(1)
437+
expect(queryAllByRole('meter')).toHaveLength(1)
438+
expect(queryAllByRole('progressbar')).toHaveLength(0)
437439
expect(queryAllByRole('progressbar', {queryFallbacks: true})).toHaveLength(1)
438440
})
439441

@@ -485,6 +487,7 @@ test('getAll* matchers return an array', () => {
485487
expect(getAllByDisplayValue(/cars$/)).toHaveLength(2)
486488
expect(getAllByText(/^where/i)).toHaveLength(1)
487489
expect(getAllByRole(/container/i)).toHaveLength(1)
490+
expect(getAllByRole('meter')).toHaveLength(1)
488491
expect(getAllByRole('progressbar', {queryFallbacks: true})).toHaveLength(1)
489492
})
490493

src/__tests__/queries.find.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ test('find asynchronously finds elements', async () => {
5757
await expect(findByRole('dialog')).resolves.toBeTruthy()
5858
await expect(findAllByRole('dialog')).resolves.toHaveLength(1)
5959

60+
await expect(findByRole('meter')).resolves.toBeTruthy()
61+
await expect(findAllByRole('meter')).resolves.toHaveLength(1)
6062
await expect(
6163
findByRole('progressbar', {queryFallbacks: true}),
6264
).resolves.toBeTruthy()

src/queries/role.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,20 @@ function queryAllByRole(
4141
const isRoleSpecifiedExplicitly = node.hasAttribute('role')
4242

4343
if (isRoleSpecifiedExplicitly) {
44+
const roleValue = node.getAttribute('role')
4445
if (queryFallbacks) {
45-
return node
46-
.getAttribute('role')
46+
return roleValue
4747
.split(' ')
4848
.filter(Boolean)
4949
.some(text => matcher(text, node, role, matchNormalizer))
5050
}
51-
return matcher(node.getAttribute('role'), node, role, matchNormalizer)
51+
// if a custom normalizer is passed then let normalizer handle the role value
52+
if (normalizer) {
53+
return matcher(roleValue, node, role, matchNormalizer)
54+
}
55+
// other wise only send the first word to match
56+
const [firstWord] = roleValue.split(' ')
57+
return matcher(firstWord, node, role, matchNormalizer)
5258
}
5359

5460
const implicitRoles = getImplicitAriaRoles(node)

0 commit comments

Comments
 (0)