-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathto-be-partially-checked.ts
37 lines (32 loc) · 1.26 KB
/
to-be-partially-checked.ts
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
import type { ReactTestInstance } from 'react-test-renderer';
import { matcherHint } from 'jest-matcher-utils';
import redent from 'redent';
import { computeAriaChecked, getRole, isAccessibilityElement } from '../helpers/accessibility';
import { ErrorWithStack } from '../helpers/errors';
import { formatElement } from '../helpers/format-element';
import { checkHostElement } from './utils';
export function toBePartiallyChecked(this: jest.MatcherContext, element: ReactTestInstance) {
checkHostElement(element, toBePartiallyChecked, this);
if (!hasValidAccessibilityRole(element)) {
throw new ErrorWithStack(
'toBePartiallyChecked() works only on accessibility elements with "checkbox" role.',
toBePartiallyChecked,
);
}
return {
pass: computeAriaChecked(element) === 'mixed',
message: () => {
const is = this.isNot ? 'is' : 'is not';
return [
matcherHint(`${this.isNot ? '.not' : ''}.toBePartiallyChecked`, 'element', ''),
'',
`Received element ${is} partially checked:`,
redent(formatElement(element), 2),
].join('\n');
},
};
}
function hasValidAccessibilityRole(element: ReactTestInstance) {
const role = getRole(element);
return isAccessibilityElement(element) && role === 'checkbox';
}