-
Notifications
You must be signed in to change notification settings - Fork 272
/
matchLabelText.ts
58 lines (54 loc) · 1.25 KB
/
matchLabelText.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
56
57
58
import { ReactTestInstance } from 'react-test-renderer';
import { matches, TextMatch, TextMatchOptions } from '../../matches';
import {
getAccessibilityLabel,
getAccessibilityLabelledBy,
} from '../accessiblity';
import { findAll } from '../findAll';
import { matchTextContent } from './matchTextContent';
export function matchLabelText(
root: ReactTestInstance,
element: ReactTestInstance,
expectedText: TextMatch,
options: TextMatchOptions = {}
) {
return (
matchAccessibilityLabel(element, expectedText, options) ||
matchAccessibilityLabelledBy(
root,
getAccessibilityLabelledBy(element),
expectedText,
options
)
);
}
function matchAccessibilityLabel(
element: ReactTestInstance,
extpectedLabel: TextMatch,
options: TextMatchOptions
) {
return matches(
extpectedLabel,
getAccessibilityLabel(element),
options.normalizer,
options.exact
);
}
function matchAccessibilityLabelledBy(
root: ReactTestInstance,
nativeId: string | undefined,
text: TextMatch,
options: TextMatchOptions
) {
if (!nativeId) {
return false;
}
return (
findAll(
root,
(node) =>
node.props.nativeID === nativeId &&
matchTextContent(node, text, options)
).length > 0
);
}