-
Notifications
You must be signed in to change notification settings - Fork 405
/
Copy pathto-be-partially-checked.js
50 lines (42 loc) · 1.3 KB
/
to-be-partially-checked.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
47
48
49
50
import {checkHtmlElement} from './utils'
export function toBePartiallyChecked(element) {
checkHtmlElement(element, toBePartiallyChecked, this)
const isValidInput = () => {
return (
element.tagName.toLowerCase() === 'input' && element.type === 'checkbox'
)
}
const isValidAriaElement = () => {
return element.getAttribute('role') === 'checkbox'
}
if (!isValidInput() && !isValidAriaElement()) {
return {
pass: false,
message: () =>
'only inputs with type="checkbox" or elements with role="checkbox" and a valid aria-checked attribute can be used with .toBePartiallyChecked(). Use .toHaveValue() instead',
}
}
const isPartiallyChecked = () => {
const isAriaMixed = element.getAttribute('aria-checked') === 'mixed'
if (isValidInput()) {
return element.indeterminate || isAriaMixed
}
return isAriaMixed
}
return {
pass: isPartiallyChecked(),
message: () => {
const is = isPartiallyChecked() ? 'is' : 'is not'
return [
this.utils.matcherHint(
`${this.isNot ? '.not' : ''}.toBePartiallyChecked`,
'element',
'',
),
'',
`Received element ${is} partially checked:`,
` ${this.utils.printReceived(element.cloneNode(false))}`,
].join('\n')
},
}
}