-
Notifications
You must be signed in to change notification settings - Fork 469
feat(byRole): Add name
filter
#408
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
e8193d8
a70ed30
5c41d96
225887a
608fd2f
3e683d7
cfce8d9
d6ac518
3fcf430
7a53e33
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 | ||||
---|---|---|---|---|---|---|
@@ -1,5 +1,6 @@ | ||||||
import {configure, getConfig} from '../config' | ||||||
import {render} from './helpers/test-utils' | ||||||
import {getQueriesForElement} from '../get-queries-for-element' | ||||||
import {render, renderIntoDocument} from './helpers/test-utils' | ||||||
|
||||||
test('by default logs accessible roles when it fails', () => { | ||||||
const {getByRole} = render(`<h1>Hi</h1>`) | ||||||
|
@@ -10,6 +11,7 @@ Here are the accessible roles: | |||||
|
||||||
heading: | ||||||
|
||||||
Name "Hi": | ||||||
<h1 /> | ||||||
|
||||||
-------------------------------------------------- | ||||||
|
@@ -32,6 +34,7 @@ Here are the available roles: | |||||
|
||||||
heading: | ||||||
|
||||||
Name "Hi": | ||||||
<h1 /> | ||||||
|
||||||
-------------------------------------------------- | ||||||
|
@@ -183,6 +186,144 @@ test('can include inaccessible roles', () => { | |||||
expect(getByRole('list', {hidden: true})).not.toBeNull() | ||||||
}) | ||||||
|
||||||
test('can be filtered by accessible name', () => { | ||||||
const {getByRole} = renderIntoDocument( | ||||||
` | ||||||
<div> | ||||||
<h1>Order</h1> | ||||||
<h2>Delivery Adress</h2> | ||||||
<form aria-label="Delivery Adress"> | ||||||
<label> | ||||||
<div>Street</div> | ||||||
<input type="text" /> | ||||||
</label> | ||||||
<input type="submit" /> | ||||||
</form> | ||||||
<h2>Invoice Adress</h2> | ||||||
<form aria-label="Invoice Adress"> | ||||||
<label> | ||||||
<div>Street</div> | ||||||
<input type="text" /> | ||||||
</label> | ||||||
<input type="submit" /> | ||||||
</form> | ||||||
</div>`, | ||||||
) | ||||||
|
||||||
const deliveryForm = getByRole('form', {name: 'Delivery Adress'}) | ||||||
expect(deliveryForm).not.toBeNull() | ||||||
|
||||||
expect( | ||||||
// TODO: upstream bug in `aria-query`; should be `button` role | ||||||
getQueriesForElement(deliveryForm).getByRole('textbox', {name: 'Submit'}), | ||||||
).not.toBeNull() | ||||||
|
||||||
const invoiceForm = getByRole('form', {name: 'Delivery Adress'}) | ||||||
expect(invoiceForm).not.toBeNull() | ||||||
|
||||||
expect( | ||||||
getQueriesForElement(invoiceForm).getByRole('textbox', {name: 'Street'}), | ||||||
).not.toBeNull() | ||||||
}) | ||||||
|
||||||
test('accessible name comparison is case sensitive', () => { | ||||||
const {getByRole} = render(`<h1>Sign <em>up</em></h1>`) | ||||||
|
||||||
// actual: "Sign up", | ||||||
// queried: "Sign Up" | ||||||
expect(() => getByRole('heading', {name: 'Sign Up'})) | ||||||
.toThrowErrorMatchingInlineSnapshot(` | ||||||
"Unable to find an accessible element with the role "heading" and name "Sign Up" | ||||||
|
||||||
Here are the accessible roles: | ||||||
|
||||||
heading: | ||||||
|
||||||
Name "Sign up": | ||||||
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. The only difference between this and the query is the 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 had trouble understanding it as well. I would like to keep the test to make sure that |
||||||
<h1 /> | ||||||
|
||||||
-------------------------------------------------- | ||||||
|
||||||
<div> | ||||||
<h1> | ||||||
Sign | ||||||
<em> | ||||||
up | ||||||
</em> | ||||||
</h1> | ||||||
</div>" | ||||||
`) | ||||||
}) | ||||||
|
||||||
test('accessible name filter implements TextMatch', () => { | ||||||
const {getByRole} = render( | ||||||
`<h1>Sign <em>up</em></h1><h2>Details</h2><h2>Your Signature</h2>`, | ||||||
) | ||||||
|
||||||
// subset via regex | ||||||
expect(getByRole('heading', {name: /gn u/})).not.toBeNull() | ||||||
// regex | ||||||
expect(getByRole('heading', {name: /^sign/i})).not.toBeNull() | ||||||
// function | ||||||
expect( | ||||||
getByRole('heading', { | ||||||
name: (name, element) => { | ||||||
return element.nodeName === 'H2' && name === 'Your Signature' | ||||||
}, | ||||||
}), | ||||||
).not.toBeNull() | ||||||
}) | ||||||
|
||||||
test('TextMatch serialization in error message', () => { | ||||||
const {getByRole} = render(`<h1>Sign <em>up</em></h1>`) | ||||||
|
||||||
expect(() => getByRole('heading', {name: /Login/})) | ||||||
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. Not sure it's necessary to have all three of these because they're all testing the same codepath and their differences are covered in other tests. 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. It's more about showcasing how the 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
I think this makes it more clear that it's not just a typo... |
||||||
.toThrowErrorMatchingInlineSnapshot(` | ||||||
"Unable to find an accessible element with the role "heading" and name \`/Login/\` | ||||||
|
||||||
Here are the accessible roles: | ||||||
|
||||||
heading: | ||||||
|
||||||
Name "Sign up": | ||||||
<h1 /> | ||||||
|
||||||
-------------------------------------------------- | ||||||
|
||||||
<div> | ||||||
<h1> | ||||||
Sign | ||||||
<em> | ||||||
up | ||||||
</em> | ||||||
</h1> | ||||||
</div>" | ||||||
`) | ||||||
|
||||||
expect(() => getByRole('heading', {name: () => false})) | ||||||
.toThrowErrorMatchingInlineSnapshot(` | ||||||
"Unable to find an accessible element with the role "heading" and name \`() => false\` | ||||||
|
||||||
Here are the accessible roles: | ||||||
|
||||||
heading: | ||||||
|
||||||
Name "Sign up": | ||||||
<h1 /> | ||||||
|
||||||
-------------------------------------------------- | ||||||
|
||||||
<div> | ||||||
<h1> | ||||||
Sign | ||||||
<em> | ||||||
up | ||||||
</em> | ||||||
</h1> | ||||||
</div>" | ||||||
`) | ||||||
}) | ||||||
|
||||||
describe('configuration', () => { | ||||||
let originalConfig | ||||||
beforeEach(() => { | ||||||
|
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.
Should we contribute a fix to aria-query before merging this?
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.
Right. They released a new major. Opening a separate PR that bumps aria-query and then we'll see if that worked.