mirror of
https://github.com/callstack/react-native-testing-library.git
synced 2026-09-18 23:09:04 +08:00
fix: *ByText would call toString() on objects and only match on [object Object] (#290)
This commit is contained in:
@@ -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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user