@@ -18,8 +18,7 @@ const getNodeByText = (node, text) => {
1818 try {
1919 // eslint-disable-next-line
2020 const { Text, TextInput } = require ( 'react-native' ) ;
21- const isTextComponent =
22- filterNodeByType ( node , Text ) || filterNodeByType ( node , TextInput ) ;
21+ const isTextComponent = filterNodeByType ( node , Text ) ;
2322 if ( isTextComponent ) {
2423 const textChildren = React . Children . map (
2524 node . props . children ,
@@ -54,6 +53,21 @@ const getTextInputNodeByPlaceholder = (node, placeholder) => {
5453 }
5554} ;
5655
56+ const getTextInputNodeByDisplayValue = ( node , value ) => {
57+ try {
58+ // eslint-disable-next-line
59+ const { TextInput } = require ( 'react-native' ) ;
60+ return (
61+ filterNodeByType ( node , TextInput ) &&
62+ ( typeof value === 'string'
63+ ? value === node . props . value
64+ : value . test ( node . props . value ) )
65+ ) ;
66+ } catch ( error ) {
67+ throw createLibraryNotSupportedError ( error ) ;
68+ }
69+ } ;
70+
5771export const getByName = ( instance : ReactTestInstance ) =>
5872 function getByNameFn ( name : string | React . ComponentType < * > ) {
5973 logDeprecationWarning ( 'getByName' , 'getByType' ) ;
@@ -95,6 +109,17 @@ export const getByPlaceholder = (instance: ReactTestInstance) =>
95109 }
96110 } ;
97111
112+ export const getByDisplayValue = ( instance : ReactTestInstance ) =>
113+ function getByDisplayValueFn ( placeholder : string | RegExp ) {
114+ try {
115+ return instance . find ( node =>
116+ getTextInputNodeByDisplayValue ( node , placeholder )
117+ ) ;
118+ } catch ( error ) {
119+ throw new ErrorWithStack ( prepareErrorMessage ( error ) , getByDisplayValueFn ) ;
120+ }
121+ } ;
122+
98123export const getByProps = ( instance : ReactTestInstance ) =>
99124 function getByPropsFn ( props : { [ propName : string ] : any } ) {
100125 try {
@@ -161,6 +186,20 @@ export const getAllByPlaceholder = (instance: ReactTestInstance) =>
161186 return results ;
162187 } ;
163188
189+ export const getAllByDisplayValue = ( instance : ReactTestInstance ) =>
190+ function getAllByDisplayValueFn ( value : string | RegExp ) {
191+ const results = instance . findAll ( node =>
192+ getTextInputNodeByDisplayValue ( node , value )
193+ ) ;
194+ if ( results . length === 0 ) {
195+ throw new ErrorWithStack (
196+ `No instances found with display value: ${ String ( value ) } ` ,
197+ getAllByDisplayValueFn
198+ ) ;
199+ }
200+ return results ;
201+ } ;
202+
164203export const getAllByProps = ( instance : ReactTestInstance ) =>
165204 function getAllByPropsFn ( props : { [ propName : string ] : any } ) {
166205 const results = instance . findAllByProps ( props ) ;
@@ -179,10 +218,12 @@ export const getByAPI = (instance: ReactTestInstance) => ({
179218 getByType : getByType ( instance ) ,
180219 getByText : getByText ( instance ) ,
181220 getByPlaceholder : getByPlaceholder ( instance ) ,
221+ getByDisplayValue : getByDisplayValue ( instance ) ,
182222 getByProps : getByProps ( instance ) ,
183223 getAllByName : getAllByName ( instance ) ,
184224 getAllByType : getAllByType ( instance ) ,
185225 getAllByText : getAllByText ( instance ) ,
186226 getAllByPlaceholder : getAllByPlaceholder ( instance ) ,
227+ getAllByDisplayValue : getAllByDisplayValue ( instance ) ,
187228 getAllByProps : getAllByProps ( instance ) ,
188229} ) ;
0 commit comments