diff --git a/src/__tests__/queryByApi.test.js b/src/__tests__/queryByApi.test.js index 82e89bf6e..e6241833d 100644 --- a/src/__tests__/queryByApi.test.js +++ b/src/__tests__/queryByApi.test.js @@ -1,6 +1,6 @@ // @flow import * as React from 'react'; -import { Text, Image } from 'react-native'; +import { Text, Image, View } from 'react-native'; import { render } from '..'; test('queryByText nested in at start', () => { @@ -114,3 +114,31 @@ test('queryByText nested deep in ', () => { ).queryByText('Hello World!') ).toBeTruthy(); }); + +test('queryByText nested in ', () => { + const CustomText = () => { + return Hello World; + }; + + expect( + render( + + + + ).queryByText('Hello World') + ).toBeTruthy(); +}); + +test('queryByText nested in ', () => { + const CustomText = () => { + return Hello World; + }; + + expect( + render( + + + + ).queryByText('Hello World') + ).toBeFalsy(); +}); diff --git a/src/helpers/getByAPI.js b/src/helpers/getByAPI.js index 41e74bdd5..69884187d 100644 --- a/src/helpers/getByAPI.js +++ b/src/helpers/getByAPI.js @@ -41,7 +41,7 @@ export type GetByAPI = {| getAllByPlaceholder: () => void, |}; -const filterNodeByType = (node, type) => node.type === type; +const filterNodeByType = (node, type) => node?.type === type; const getNodeByText = (node, text) => { try { @@ -56,6 +56,17 @@ const getNodeByText = (node, text) => { : text.test(textToTest); } } + + const isParentTextComponent = filterNodeByType(node.parent?.parent, Text); + if (isParentTextComponent && !isTextComponent) { + if (typeof node.children?.[0] === 'string') { + const textToTest = node.children.join(''); + return typeof text === 'string' + ? text === textToTest + : text.test(textToTest); + } + } + return false; } catch (error) { throw createLibraryNotSupportedError(error);