-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathto-be-checked.ts
47 lines (41 loc) · 1.43 KB
/
to-be-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
38
39
40
41
42
43
44
45
46
47
import type { ReactTestInstance } from 'react-test-renderer';
import { matcherHint } from 'jest-matcher-utils';
import redent from 'redent';
import {
computeAriaChecked,
getRole,
isAccessibilityElement,
rolesSupportingCheckedState,
} from '../helpers/accessibility';
import { ErrorWithStack } from '../helpers/errors';
import { formatElement } from '../helpers/format-element';
import { isHostSwitch } from '../helpers/host-component-names';
import { checkHostElement } from './utils';
export function toBeChecked(this: jest.MatcherContext, element: ReactTestInstance) {
checkHostElement(element, toBeChecked, this);
if (!isHostSwitch(element) && !isSupportedAccessibilityElement(element)) {
throw new ErrorWithStack(
`toBeChecked() works only on host "Switch" elements or accessibility elements with "checkbox", "radio" or "switch" role.`,
toBeChecked,
);
}
return {
pass: computeAriaChecked(element) === true,
message: () => {
const is = this.isNot ? 'is' : 'is not';
return [
matcherHint(`${this.isNot ? '.not' : ''}.toBeChecked`, 'element', ''),
'',
`Received element ${is} checked:`,
redent(formatElement(element), 2),
].join('\n');
},
};
}
function isSupportedAccessibilityElement(element: ReactTestInstance) {
if (!isAccessibilityElement(element)) {
return false;
}
const role = getRole(element);
return rolesSupportingCheckedState[role];
}