Skip to content

Commit d867c6e

Browse files
committed
refactor: tweaks
1 parent 771f3a5 commit d867c6e

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed
Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,50 @@
11
import { ReactTestInstance } from 'react-test-renderer';
22
import { matches, TextMatch, TextMatchOptions } from '../../matches';
3+
import { findAll } from '../findAll';
34
import { matchTextContent } from './matchTextContent';
45

56
export function matchLabelText(
6-
rootInstance: ReactTestInstance,
7-
node: ReactTestInstance,
7+
root: ReactTestInstance,
8+
element: ReactTestInstance,
89
text: TextMatch,
910
options: TextMatchOptions = {}
1011
) {
1112
return (
12-
matchAccessibilityLabel(node, text, options) ||
13+
matchAccessibilityLabel(element, text, options) ||
1314
matchAccessibilityLabelledBy(
14-
rootInstance,
15-
node.props.accessibilityLabelledBy,
15+
root,
16+
element.props.accessibilityLabelledBy,
1617
text,
1718
options
1819
)
1920
);
2021
}
2122

22-
export function matchAccessibilityLabel(
23-
node: ReactTestInstance,
23+
function matchAccessibilityLabel(
24+
element: ReactTestInstance,
2425
text: TextMatch,
2526
options: TextMatchOptions
2627
) {
2728
const { exact, normalizer } = options;
28-
return matches(text, node.props.accessibilityLabel, normalizer, exact);
29+
return matches(text, element.props.accessibilityLabel, normalizer, exact);
2930
}
3031

31-
export function matchAccessibilityLabelledBy(
32-
rootInstance: ReactTestInstance,
33-
nativeID: string | undefined,
32+
function matchAccessibilityLabelledBy(
33+
root: ReactTestInstance,
34+
nativeId: string | undefined,
3435
text: TextMatch,
3536
options: TextMatchOptions
3637
) {
37-
if (!nativeID) {
38+
if (!nativeId) {
3839
return false;
3940
}
4041

41-
const { exact, normalizer } = options;
42-
return rootInstance
43-
.findAll((node) =>
44-
matches(nativeID, node.props?.nativeID, normalizer, exact)
45-
)
46-
.some((node) => matchTextContent(node, text));
42+
return (
43+
findAll(
44+
root,
45+
(element) =>
46+
element.props?.nativeID === nativeId &&
47+
matchTextContent(element, text, options)
48+
).length > 0
49+
);
4750
}

website/docs/Queries.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,8 @@ getByLabelText(
213213
```
214214

215215
Returns a `ReactTestInstance` with matching label:
216-
217-
- by matching `accessibilityLabel` prop
218-
- or by matching text content of text view referenced by `accessibilityLabelledBy` prop
216+
- either by matching [`accessibilityLabel`](https://reactnative.dev/docs/accessibility#accessibilitylabel) prop
217+
- or by matching text content of view referenced by [`accessibilityLabelledBy`](https://reactnative.dev/docs/accessibility#accessibilitylabelledby-android) prop
219218

220219
```jsx
221220
import { render, screen } from '@testing-library/react-native';

0 commit comments

Comments
 (0)