-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathto-have-accessible-name.ts
44 lines (39 loc) · 1.24 KB
/
to-have-accessible-name.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
import type { ReactTestInstance } from 'react-test-renderer';
import { matcherHint } from 'jest-matcher-utils';
import { computeAccessibleName } from '../helpers/accessibility';
import type { TextMatch, TextMatchOptions } from '../matches';
import { matches } from '../matches';
import { checkHostElement, formatMessage } from './utils';
export function toHaveAccessibleName(
this: jest.MatcherContext,
element: ReactTestInstance,
expectedName?: TextMatch,
options?: TextMatchOptions,
) {
checkHostElement(element, toHaveAccessibleName, this);
const receivedName = computeAccessibleName(element);
const missingExpectedValue = arguments.length === 1;
let pass = false;
if (missingExpectedValue) {
pass = receivedName !== '';
} else {
pass =
expectedName != null
? matches(expectedName, receivedName, options?.normalizer, options?.exact)
: false;
}
return {
pass,
message: () => {
return [
formatMessage(
matcherHint(`${this.isNot ? '.not' : ''}.toHaveAccessibleName`, 'element', ''),
`Expected element ${this.isNot ? 'not to' : 'to'} have accessible name`,
expectedName,
'Received',
receivedName,
),
].join('\n');
},
};
}