Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/__tests__/queryByApi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,31 @@ test('queryByText not found', () => {
).queryByText('SomethingElse')
).toBeFalsy();
});

test('queryByText nested multiple <Text> in <Text>', () => {
expect(
render(
<Text>
Hello{' '}
<Text>
World
<Text>!{true}</Text>
</Text>
</Text>
).queryByText('Hello World!')
).toBeTruthy();
});

test('queryByText nested <CustomText> in <Text>', () => {
const CustomText = ({ children }) => {
return <Text>{children}</Text>;
};

expect(
render(
<Text>
Hello <CustomText>World!</CustomText>
</Text>
).queryByText('Hello World!')
).toBeTruthy();
});
18 changes: 12 additions & 6 deletions src/helpers/getByAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const getNodeByText = (node, text) => {
const { Text } = require('react-native');
const isTextComponent = filterNodeByType(node, Text);
if (isTextComponent) {
const textChildren = getChildrenAsText(node.props.children);
const textChildren = getChildrenAsText(node.props.children, Text);
if (textChildren) {
const textToTest = textChildren.join('');
return typeof text === 'string'
Expand All @@ -35,18 +35,24 @@ const getNodeByText = (node, text) => {
}
};

const getChildrenAsText = children => {
return React.Children.map(children, child => {
const getChildrenAsText = (children, TextComponent, textContent = []) => {
React.Children.forEach(children, child => {
if (typeof child === 'string') {
return child;
textContent.push(child);
return;
}

if (typeof child === 'number') {
return child.toString();
textContent.push(child.toString());
return;
}

return '';
if (child?.props?.children) {
getChildrenAsText(child.props.children, TextComponent, textContent);
}
});

return textContent;
};

const getTextInputNodeByPlaceholder = (node, placeholder) => {
Expand Down
Loading