Skip to content

Commit e2a5f1a

Browse files
committed
feat(ByRole): Add support for fallback roles
1 parent a2c3472 commit e2a5f1a

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/__tests__/element-queries.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ test('queryAllByRole returns semantic html elements', () => {
412412
</tbody>
413413
</table>
414414
<table role="grid"></table>
415+
<div role="meter progressbar" />
415416
<button>Button</button>
416417
</form>
417418
`)
@@ -433,6 +434,7 @@ test('queryAllByRole returns semantic html elements', () => {
433434
expect(queryAllByRole(/rowgroup/i)).toHaveLength(2)
434435
expect(queryAllByRole(/(table)|(textbox)/i)).toHaveLength(3)
435436
expect(queryAllByRole(/img/i)).toHaveLength(1)
437+
expect(queryAllByRole('progressbar', {queryFallbacks: true})).toHaveLength(1)
436438
})
437439

438440
test('getAll* matchers return an array', () => {
@@ -471,6 +473,7 @@ test('getAll* matchers return an array', () => {
471473
<option value="volvo">Toyota</option>
472474
<option value="audi">Honda</option>
473475
</select>
476+
<div role="meter progressbar" />
474477
</div>,
475478
`)
476479
expect(getAllByAltText(/finding.*poster$/i)).toHaveLength(2)
@@ -482,6 +485,7 @@ test('getAll* matchers return an array', () => {
482485
expect(getAllByDisplayValue(/cars$/)).toHaveLength(2)
483486
expect(getAllByText(/^where/i)).toHaveLength(1)
484487
expect(getAllByRole(/container/i)).toHaveLength(1)
488+
expect(getAllByRole('progressbar', {queryFallbacks: true})).toHaveLength(1)
485489
})
486490

487491
test('getAll* matchers throw for 0 matches', () => {
@@ -658,6 +662,18 @@ test('using jest helpers to check element role', () => {
658662
expect(getByRole('dialog')).toHaveTextContent('Contents')
659663
})
660664

665+
test('using jest helpers to check element fallback roles', () => {
666+
const {getByRole} = render(`
667+
<div role="meter progressbar">
668+
<span>Contents</span>
669+
</div>
670+
`)
671+
672+
expect(getByRole('progressbar', {queryFallbacks: true})).toHaveTextContent(
673+
'Contents',
674+
)
675+
})
676+
661677
test('test the debug helper prints the dom state here', () => {
662678
const originalDebugPrintLimit = process.env.DEBUG_PRINT_LIMIT
663679
const Large = `<div>

src/__tests__/queries.find.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ test('find asynchronously finds elements', async () => {
3333
<img alt="test alt text" src="/lucy-ricardo.png" />
3434
<span title="test title" />
3535
<div role="dialog"></div>
36+
<div role="meter progressbar"></div>
3637
</div>
3738
`)
3839
await expect(findByLabelText('test-label')).resolves.toBeTruthy()
@@ -56,6 +57,13 @@ test('find asynchronously finds elements', async () => {
5657
await expect(findByRole('dialog')).resolves.toBeTruthy()
5758
await expect(findAllByRole('dialog')).resolves.toHaveLength(1)
5859

60+
await expect(
61+
findByRole('progressbar', {queryFallbacks: true}),
62+
).resolves.toBeTruthy()
63+
await expect(
64+
findAllByRole('progressbar', {queryFallbacks: true}),
65+
).resolves.toHaveLength(1)
66+
5967
await expect(findByTestId('test-id')).resolves.toBeTruthy()
6068
await expect(findAllByTestId('test-id')).resolves.toHaveLength(1)
6169
})

src/queries/role.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function queryAllByRole(
2121
hidden = getConfig().defaultHidden,
2222
trim,
2323
normalizer,
24+
queryFallbacks = false,
2425
} = {},
2526
) {
2627
const matcher = exact ? matches : fuzzyMatches
@@ -40,6 +41,13 @@ function queryAllByRole(
4041
const isRoleSpecifiedExplicitly = node.hasAttribute('role')
4142

4243
if (isRoleSpecifiedExplicitly) {
44+
if (queryFallbacks) {
45+
return node
46+
.getAttribute('role')
47+
.split(' ')
48+
.filter(Boolean)
49+
.some(text => matcher(text, node, role, matchNormalizer))
50+
}
4351
return matcher(node.getAttribute('role'), node, role, matchNormalizer)
4452
}
4553

0 commit comments

Comments
 (0)