Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions src/__tests__/queryByApi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,13 @@ test('queryByText not found', () => {
).queryByText('SomethingElse')
).toBeFalsy();
});

test('queryByText nested <Text> in <Text>', () => {
expect(
render(
<Text>
Hello <Text>World!</Text>
</Text>
).queryByText('Hello World!')
).toBeTruthy();
});
28 changes: 24 additions & 4 deletions src/helpers/getByAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,37 @@ const getNodeByText = (node, text) => {
};

const getChildrenAsText = children => {
return React.Children.map(children, child => {
let textContent = [];

React.Children.map(children, child => {
if (typeof child === 'string') {
return child;
return textContent.push(child);
}

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

return '';
if (child.props.children) {
const { children } = child.props;

if (children instanceof Array) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use instanceof to check for array. Use .length

children.forEach(node => {
// eslint-disable-next-line
const { Text } = require('react-native');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Text is retrieved from the caller, let's pass it through function parameters, instead of requiring it in yet another place.

if (filterNodeByType(node, Text)) {
return textContent.push(node.props.children);
}
});
} else {
if (typeof children === 'string') {
return textContent.push(children);
}
}
}
});

return textContent;
};

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