Skip to content

Commit

Permalink
feat: toHaveAccessibleDescription supports aria-description (#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
amanmahajan7 authored Jan 3, 2024
1 parent b64b953 commit 1fb156c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,12 @@ make a partial match passing a regular expression, or by using
aria-describedby="t1"
/>
<span id="t1" role="presentation">The logo of Our Company</span>
<img
src="logo.jpg"
data-testid="logo2"
alt="Company logo"
aria-description="The logo of Our Company"
/>
```

```js
Expand All @@ -606,6 +612,9 @@ expect(getByTestId('logo')).not.toHaveAccessibleDescription('Company logo')
expect(getByTestId('logo')).toHaveAccessibleDescription(
'The logo of Our Company',
)
expect(getByTestId('logo2')).toHaveAccessibleDescription(
'The logo of Our Company',
)
```

<hr />
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"aria-query": "^5.0.0",
"chalk": "^3.0.0",
"css.escape": "^1.5.1",
"dom-accessibility-api": "^0.5.6",
"dom-accessibility-api": "^0.6.3",
"lodash": "^4.17.15",
"redent": "^3.0.0"
},
Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/to-have-accessible-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ describe('.toHaveAccessibleDescription', () => {
}).toThrow(/expected element not to have accessible description/i)
})

it('works with aria-description attribute', () => {
const {queryByTestId} = render(`
<img src="logo.jpg" data-testid="logo" alt="Company logo" aria-description="The logo of Our Company">
`)

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)
})

it('handles multiple ids', () => {
const {queryByTestId} = render(`
<div>
Expand Down

0 comments on commit 1fb156c

Please sign in to comment.