Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
87 changes: 57 additions & 30 deletions src/__tests__/queryByApi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,74 @@ import React from 'react';
import { Text, Image } from 'react-native';
import { render } from '..';

test('queryByText nested <Image> in <Text> at start', () => {
expect(
render(
<Text>
<Image source={{}} />
Hello
</Text>
).queryByText('Hello')
).toBeTruthy();
});
// test('queryByText nested <Image> in <Text> at start', () => {
// expect(
// render(
// <Text>
// <Image source={{}} />
// Hello
// </Text>
// ).queryByText('Hello')
// ).toBeTruthy();
// });

test('queryByText nested <Image> in <Text> at end', () => {
expect(
render(
<Text>
Hello
<Image source={{}} />
</Text>
).queryByText('Hello')
).toBeTruthy();
});
// test('queryByText nested <Image> in <Text> at end', () => {
// expect(
// render(
// <Text>
// Hello
// <Image source={{}} />
// </Text>
// ).queryByText('Hello')
// ).toBeTruthy();
// });

test('queryByText nested <Image> in <Text> in middle', () => {
// test('queryByText nested <Image> in <Text> in middle', () => {
// expect(
// render(
// <Text>
// Hello
// <Image source={{}} />
// World
// </Text>
// ).queryByText('HelloWorld')
// ).toBeTruthy();
// });

// test('queryByText not found', () => {
// expect(
// render(
// <Text>
// Hello
// <Image source={{}} />
// </Text>
// ).queryByText('SomethingElse')
// ).toBeFalsy();
// });

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

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

expect(
render(
<Text>
Hello
<Image source={{}} />
Hello <CustomText>World!</CustomText>
</Text>
).queryByText('SomethingElse')
).toBeFalsy();
).queryByText('Hello World!')
).toBeTruthy();
});
21 changes: 14 additions & 7 deletions src/helpers/getByAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ const filterNodeByName = (node, name) =>
typeof node.type !== 'string' &&
(node.type.displayName === name || node.type.name === name);

let textContent = [];

const getNodeByText = (node, text) => {
try {
// eslint-disable-next-line
const { Text } = require('react-native');
const { Text } = require('react-native');
textContent = [];
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 +38,22 @@ const getNodeByText = (node, text) => {
}
};

const getChildrenAsText = children => {
return React.Children.map(children, child => {
const getChildrenAsText = (children, textComponent) => {
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) {
getChildrenAsText(child.props.children, textComponent);
}
});

return textContent;
};

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