Skip to content

Commit 74eb27c

Browse files
committed
move textContent out of module scope, fixup children check
1 parent 458b3d4 commit 74eb27c

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/__tests__/queryByApi.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ test('queryByText nested multiple <Text> in <Text>', () => {
5454
<Text>
5555
Hello{' '}
5656
<Text>
57-
World<Text>!</Text>
57+
World
58+
<Text>!{true}</Text>
5859
</Text>
5960
</Text>
6061
).queryByText('Hello World!')

src/helpers/getByAPI.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@ const filterNodeByName = (node, name) =>
1515
typeof node.type !== 'string' &&
1616
(node.type.displayName === name || node.type.name === name);
1717

18-
let textContent = [];
19-
2018
const getNodeByText = (node, text) => {
2119
try {
2220
// eslint-disable-next-line
23-
const { Text } = require('react-native');
24-
textContent = [];
21+
const { Text } = require('react-native');
2522
const isTextComponent = filterNodeByType(node, Text);
2623
if (isTextComponent) {
2724
const textChildren = getChildrenAsText(node.props.children, Text);
@@ -38,18 +35,20 @@ const getNodeByText = (node, text) => {
3835
}
3936
};
4037

41-
const getChildrenAsText = (children, textComponent) => {
42-
React.Children.map(children, child => {
38+
const getChildrenAsText = (children, TextComponent, textContent = []) => {
39+
React.Children.forEach(children, child => {
4340
if (typeof child === 'string') {
44-
return textContent.push(child);
41+
textContent.push(child);
42+
return;
4543
}
4644

4745
if (typeof child === 'number') {
48-
return textContent.push(child.toString());
46+
textContent.push(child.toString());
47+
return;
4948
}
5049

51-
if (child.props.children) {
52-
getChildrenAsText(child.props.children, textComponent);
50+
if (child?.props?.children) {
51+
getChildrenAsText(child.props.children, TextComponent, textContent);
5352
}
5453
});
5554

0 commit comments

Comments
 (0)