@@ -2,6 +2,7 @@ import type { ReactTestInstance } from 'react-test-renderer';
22import * as React from 'react' ;
33import { createLibraryNotSupportedError } from '../helpers/errors' ;
44import { filterNodeByType } from '../helpers/filterNodeByType' ;
5+ import { isHostElement } from '../helpers/component-tree' ;
56import { matches , TextMatch } from '../matches' ;
67import type { NormalizerFn } from '../matches' ;
78import { makeQueries } from './makeQueries' ;
@@ -58,37 +59,65 @@ const getChildrenAsText = (
5859const getNodeByText = (
5960 node : ReactTestInstance ,
6061 text : TextMatch ,
62+ TextComponent : React . ComponentType ,
6163 options : TextMatchOptions = { }
6264) => {
63- try {
64- const { Text } = require ( 'react-native' ) ;
65- const isTextComponent = filterNodeByType ( node , Text ) ;
66- if ( isTextComponent ) {
67- const textChildren = getChildrenAsText ( node . props . children , Text ) ;
68- if ( textChildren ) {
69- const textToTest = textChildren . join ( '' ) ;
70- const { exact, normalizer } = options ;
71- return matches ( text , textToTest , normalizer , exact ) ;
72- }
65+ const isTextComponent = filterNodeByType ( node , TextComponent ) ;
66+ if ( isTextComponent ) {
67+ const textChildren = getChildrenAsText ( node . props . children , TextComponent ) ;
68+ if ( textChildren ) {
69+ const textToTest = textChildren . join ( '' ) ;
70+ const { exact, normalizer } = options ;
71+ return matches ( text , textToTest , normalizer , exact ) ;
7372 }
74- return false ;
75- } catch ( error ) {
76- throw createLibraryNotSupportedError ( error ) ;
7773 }
74+ return false ;
7875} ;
7976
77+ function getCompositeParent (
78+ element : ReactTestInstance ,
79+ compositeType : React . ComponentType
80+ ) {
81+ if ( ! isHostElement ( element ) ) return null ;
82+
83+ let current = element . parent ;
84+ while ( ! isHostElement ( current ) ) {
85+ // We're at the top of the tree
86+ if ( ! current ) {
87+ return null ;
88+ }
89+
90+ if ( filterNodeByType ( current , compositeType ) ) {
91+ return current ;
92+ }
93+ current = current . parent ?? null ;
94+ }
95+
96+ return null ;
97+ }
98+
8099const queryAllByText = (
81100 instance : ReactTestInstance
82101) : ( (
83102 text : TextMatch ,
84103 options ?: TextMatchOptions
85104) => Array < ReactTestInstance > ) =>
86105 function queryAllByTextFn ( text , options ) {
87- const results = instance . findAll ( ( node ) =>
88- getNodeByText ( node , text , options )
89- ) ;
106+ try {
107+ const { Text } = require ( 'react-native' ) ;
108+ const rootInstance = isHostElement ( instance )
109+ ? getCompositeParent ( instance , Text ) ?? instance
110+ : instance ;
90111
91- return results ;
112+ if ( ! rootInstance ) return [ ] ;
113+ const results = rootInstance . findAll ( ( node ) =>
114+ getNodeByText ( node , text , Text , options )
115+ ) ;
116+
117+ return results ;
118+ } catch ( error ) {
119+ throw createLibraryNotSupportedError ( error ) ;
120+ }
92121 } ;
93122
94123const getMultipleError = ( text : TextMatch ) =>
0 commit comments