Skip to content

Commit 9f6ca56

Browse files
AugustinLFthymikeemdjastrzebski
authored
fix: Return closest Text matching a query (#489)
* fix: Return closest Text matching a query * Add test for another edge case * Fix whitespaces * adjust comment * Update src/helpers/getByAPI.js Co-authored-by: Maciej Jastrzebski <[email protected]> * update test * use queryBy Co-authored-by: Michał Pierzchała <[email protected]> Co-authored-by: Maciej Jastrzebski <[email protected]>
1 parent 2536b8b commit 9f6ca56

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

src/__tests__/queryByApi.test.js

+47-8
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,68 @@ test('queryByText not found', () => {
4848
).toBeFalsy();
4949
});
5050

51-
test('queryByText nested multiple <Text> in <Text>', () => {
51+
test('queryByText nested text across multiple <Text> in <Text>', () => {
52+
const { queryByText } = render(
53+
<Text nativeID="1">
54+
Hello{' '}
55+
<Text nativeID="2">
56+
World
57+
<Text>!{true}</Text>
58+
</Text>
59+
</Text>
60+
);
61+
62+
expect(queryByText('Hello World!')?.props.nativeID).toBe('1');
63+
});
64+
65+
test('queryByText with nested Text components return the closest Text', () => {
66+
const NestedTexts = () => (
67+
<Text nativeID="1">
68+
<Text nativeID="2">My text</Text>
69+
</Text>
70+
);
71+
72+
const { queryByText } = render(<NestedTexts />);
73+
74+
expect(queryByText('My text')?.props.nativeID).toBe('2');
75+
});
76+
77+
test('queryByText with nested Text components each with text return the lowest one', () => {
78+
const NestedTexts = () => (
79+
<Text nativeID="1">
80+
bob
81+
<Text nativeID="2">My text</Text>
82+
</Text>
83+
);
84+
85+
const { queryByText } = render(<NestedTexts />);
86+
87+
expect(queryByText('My text')?.props.nativeID).toBe('2');
88+
});
89+
90+
test('queryByText nested <CustomText> in <Text>', () => {
91+
const CustomText = ({ children }) => {
92+
return <Text>{children}</Text>;
93+
};
94+
5295
expect(
5396
render(
5497
<Text>
55-
Hello{' '}
56-
<Text>
57-
World
58-
<Text>!{true}</Text>
59-
</Text>
98+
Hello <CustomText>World!</CustomText>
6099
</Text>
61100
).queryByText('Hello World!')
62101
).toBeTruthy();
63102
});
64103

65-
test('queryByText nested <CustomText> in <Text>', () => {
104+
test('queryByText nested deep <CustomText> in <Text>', () => {
66105
const CustomText = ({ children }) => {
67106
return <Text>{children}</Text>;
68107
};
69108

70109
expect(
71110
render(
72111
<Text>
73-
Hello <CustomText>World!</CustomText>
112+
<CustomText>Hello</CustomText> <CustomText>World!</CustomText>
74113
</Text>
75114
).queryByText('Hello World!')
76115
).toBeTruthy();

src/helpers/getByAPI.js

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ const getChildrenAsText = (children, TextComponent, textContent = []) => {
4444
}
4545

4646
if (child?.props?.children) {
47+
// Bail on traversing text children down the tree if current node (child)
48+
// has no text. In such situations, react-test-renderer will traverse down
49+
// this tree in a separate call and run this query again. As a result, the
50+
// query will match the deepest text node that matches requested text.
51+
if (filterNodeByType(child, TextComponent) && textContent.length === 0) {
52+
return;
53+
}
54+
4755
getChildrenAsText(child.props.children, TextComponent, textContent);
4856
}
4957
});

0 commit comments

Comments
 (0)