fix: *ByText would call toString() on objects and only match on [object Object] (#290)

This commit is contained in:
Jeroen Vannevel
2020-05-01 12:54:12 +01:00
committed by GitHub
parent 360a53d533
commit 83b28b227d
2 changed files with 65 additions and 6 deletions
+49
View File
@@ -0,0 +1,49 @@
// @flow
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 end', () => {
expect(
render(
<Text>
Hello
<Image source={{}} />
</Text>
).queryByText('Hello')
).toBeTruthy();
});
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();
});
+16 -6
View File
@@ -18,14 +18,10 @@ const filterNodeByName = (node, name) =>
const getNodeByText = (node, text) => {
try {
// eslint-disable-next-line
const { Text, TextInput } = require('react-native');
const { Text } = require('react-native');
const isTextComponent = filterNodeByType(node, Text);
if (isTextComponent) {
const textChildren = React.Children.map(
node.props.children,
// In some cases child might be undefined or null
child => (child !== undefined && child !== null ? child.toString() : '')
);
const textChildren = getChildrenAsText(node.props.children);
if (textChildren) {
const textToTest = textChildren.join('');
return typeof text === 'string'
@@ -39,6 +35,20 @@ const getNodeByText = (node, text) => {
}
};
const getChildrenAsText = children => {
return React.Children.map(children, child => {
if (typeof child === 'string') {
return child;
}
if (typeof child === 'number') {
return child.toString();
}
return '';
});
};
const getTextInputNodeByPlaceholder = (node, placeholder) => {
try {
// eslint-disable-next-line