-
Notifications
You must be signed in to change notification settings - Fork 469
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -412,6 +412,7 @@ test('queryAllByRole returns semantic html elements', () => { | |||||||
</tbody> | ||||||||
</table> | ||||||||
<table role="grid"></table> | ||||||||
<div role="meter progressbar" /> | ||||||||
<button>Button</button> | ||||||||
</form> | ||||||||
`) | ||||||||
|
@@ -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) | ||||||||
}) | ||||||||
|
||||||||
test('getAll* matchers return an array', () => { | ||||||||
|
@@ -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) | ||||||||
|
@@ -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) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
}) | ||||||||
|
||||||||
test('getAll* matchers throw for 0 matches', () => { | ||||||||
|
@@ -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> | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ function queryAllByRole( | |
hidden = getConfig().defaultHidden, | ||
trim, | ||
normalizer, | ||
queryFallbacks = false, | ||
} = {}, | ||
) { | ||
const matcher = exact ? matches : fuzzyMatches | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have a test specifically covering this case? We should. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kentcdodds ping you to remind again. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.