-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathto-be-disabled.ts
55 lines (46 loc) · 1.65 KB
/
to-be-disabled.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
48
49
50
51
52
53
54
55
import type { ReactTestInstance } from 'react-test-renderer';
import { matcherHint } from 'jest-matcher-utils';
import redent from 'redent';
import { computeAriaDisabled } from '../helpers/accessibility';
import { getHostParent } from '../helpers/component-tree';
import { formatElement } from '../helpers/format-element';
import { checkHostElement } from './utils';
export function toBeDisabled(this: jest.MatcherContext, element: ReactTestInstance) {
checkHostElement(element, toBeDisabled, this);
const isDisabled = computeAriaDisabled(element) || isAncestorDisabled(element);
return {
pass: isDisabled,
message: () => {
const is = this.isNot ? 'is' : 'is not';
return [
matcherHint(`${this.isNot ? '.not' : ''}.toBeDisabled`, 'element', ''),
'',
`Received element ${is} disabled:`,
redent(formatElement(element), 2),
].join('\n');
},
};
}
export function toBeEnabled(this: jest.MatcherContext, element: ReactTestInstance) {
checkHostElement(element, toBeEnabled, this);
const isEnabled = !computeAriaDisabled(element) && !isAncestorDisabled(element);
return {
pass: isEnabled,
message: () => {
const is = this.isNot ? 'is' : 'is not';
return [
matcherHint(`${this.isNot ? '.not' : ''}.toBeEnabled`, 'element', ''),
'',
`Received element ${is} enabled:`,
redent(formatElement(element), 2),
].join('\n');
},
};
}
function isAncestorDisabled(element: ReactTestInstance): boolean {
const parent = getHostParent(element);
if (parent == null) {
return false;
}
return computeAriaDisabled(parent) || isAncestorDisabled(parent);
}