-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathto-have-accessibility-value.ts
36 lines (32 loc) · 1.27 KB
/
to-have-accessibility-value.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
import type { ReactTestInstance } from 'react-test-renderer';
import { matcherHint, stringify } from 'jest-matcher-utils';
import { computeAriaValue } from '../helpers/accessibility';
import type { AccessibilityValueMatcher } from '../helpers/matchers/match-accessibility-value';
import { matchAccessibilityValue } from '../helpers/matchers/match-accessibility-value';
import { removeUndefinedKeys } from '../helpers/object';
import { checkHostElement, formatMessage } from './utils';
export function toHaveAccessibilityValue(
this: jest.MatcherContext,
element: ReactTestInstance,
expectedValue: AccessibilityValueMatcher,
) {
checkHostElement(element, toHaveAccessibilityValue, this);
const receivedValue = computeAriaValue(element);
return {
pass: matchAccessibilityValue(element, expectedValue),
message: () => {
const matcher = matcherHint(
`${this.isNot ? '.not' : ''}.toHaveAccessibilityValue`,
'element',
stringify(expectedValue),
);
return formatMessage(
matcher,
`Expected the element ${this.isNot ? 'not to' : 'to'} have accessibility value`,
stringify(expectedValue),
'Received element with accessibility value',
stringify(removeUndefinedKeys(receivedValue)),
);
},
};
}