-
Notifications
You must be signed in to change notification settings - Fork 405
/
Copy pathto-have-accessible-description.js
46 lines (42 loc) · 1.36 KB
/
to-have-accessible-description.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import {computeAccessibleDescription} from 'dom-accessibility-api'
import {checkHtmlElement, getMessage} from './utils'
export function toHaveAccessibleDescription(
htmlElement,
expectedAccessibleDescription,
) {
checkHtmlElement(htmlElement, toHaveAccessibleDescription, this)
const actualAccessibleDescription = computeAccessibleDescription(htmlElement)
const missingExpectedValue = arguments.length === 1
let pass = false
if (missingExpectedValue) {
// When called without an expected value we only want to validate that the element has an
// accessible description, whatever it may be.
pass = actualAccessibleDescription !== ''
} else {
pass =
expectedAccessibleDescription instanceof RegExp
? expectedAccessibleDescription.test(actualAccessibleDescription)
: this.equals(
actualAccessibleDescription,
expectedAccessibleDescription,
)
}
return {
pass,
message: () => {
const to = this.isNot ? 'not to' : 'to'
return getMessage(
this,
this.utils.matcherHint(
`${this.isNot ? '.not' : ''}.${toHaveAccessibleDescription.name}`,
'element',
'',
),
`Expected element ${to} have accessible description`,
expectedAccessibleDescription,
'Received',
actualAccessibleDescription,
)
},
}
}