-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: toHaveAccessibleName and toHaveAccessibleDescription (#377)
* Add toHaveAccessibleName * Add toHaveAccessibleDescription * Update README.md Co-authored-by: Sebastian Silbermann <[email protected]>
- Loading branch information
Showing
7 changed files
with
559 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import {render} from './helpers/test-utils' | ||
|
||
describe('.toHaveAccessibleDescription', () => { | ||
it('works with the link title attribute', () => { | ||
const {queryByTestId} = render(` | ||
<div> | ||
<a data-testid="link" href="/" aria-label="Home page" title="A link to start over">Start</a> | ||
<a data-testid="extra-link" href="/about" aria-label="About page">About</a> | ||
</div> | ||
`) | ||
|
||
const link = queryByTestId('link') | ||
expect(link).toHaveAccessibleDescription() | ||
expect(link).toHaveAccessibleDescription('A link to start over') | ||
expect(link).not.toHaveAccessibleDescription('Home page') | ||
expect(() => { | ||
expect(link).toHaveAccessibleDescription('Invalid description') | ||
}).toThrow(/expected element to have accessible description/i) | ||
expect(() => { | ||
expect(link).not.toHaveAccessibleDescription() | ||
}).toThrow(/expected element not to have accessible description/i) | ||
|
||
const extraLink = queryByTestId('extra-link') | ||
expect(extraLink).not.toHaveAccessibleDescription() | ||
expect(() => { | ||
expect(extraLink).toHaveAccessibleDescription() | ||
}).toThrow(/expected element to have accessible description/i) | ||
}) | ||
|
||
it('works with aria-describedby attributes', () => { | ||
const {queryByTestId} = render(` | ||
<div> | ||
<img src="avatar.jpg" data-testid="avatar" alt="User profile pic"> | ||
<img src="logo.jpg" data-testid="logo" alt="Company logo" aria-describedby="t1"> | ||
<span id="t1" role="presentation">The logo of Our Company</span> | ||
</div> | ||
`) | ||
|
||
const avatar = queryByTestId('avatar') | ||
expect(avatar).not.toHaveAccessibleDescription() | ||
expect(() => { | ||
expect(avatar).toHaveAccessibleDescription('User profile pic') | ||
}).toThrow(/expected element to have accessible description/i) | ||
|
||
const logo = queryByTestId('logo') | ||
expect(logo).not.toHaveAccessibleDescription('Company logo') | ||
expect(logo).toHaveAccessibleDescription('The logo of Our Company') | ||
expect(logo).toHaveAccessibleDescription(/logo of our company/i) | ||
expect(logo).toHaveAccessibleDescription( | ||
expect.stringContaining('logo of Our Company'), | ||
) | ||
expect(() => { | ||
expect(logo).toHaveAccessibleDescription("Our company's logo") | ||
}).toThrow(/expected element to have accessible description/i) | ||
expect(() => { | ||
expect(logo).not.toHaveAccessibleDescription('The logo of Our Company') | ||
}).toThrow(/expected element not to have accessible description/i) | ||
}) | ||
}) |
Oops, something went wrong.