@@ -18,8 +18,7 @@ const getNodeByText = (node, text) => {
18
18
try {
19
19
// eslint-disable-next-line
20
20
const { Text, TextInput } = require ( 'react-native' ) ;
21
- const isTextComponent =
22
- filterNodeByType ( node , Text ) || filterNodeByType ( node , TextInput ) ;
21
+ const isTextComponent = filterNodeByType ( node , Text ) ;
23
22
if ( isTextComponent ) {
24
23
const textChildren = React . Children . map (
25
24
node . props . children ,
@@ -54,6 +53,21 @@ const getTextInputNodeByPlaceholder = (node, placeholder) => {
54
53
}
55
54
} ;
56
55
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
+
57
71
export const getByName = ( instance : ReactTestInstance ) =>
58
72
function getByNameFn ( name : string | React . ComponentType < * > ) {
59
73
logDeprecationWarning ( 'getByName' , 'getByType' ) ;
@@ -95,6 +109,17 @@ export const getByPlaceholder = (instance: ReactTestInstance) =>
95
109
}
96
110
} ;
97
111
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
+
98
123
export const getByProps = ( instance : ReactTestInstance ) =>
99
124
function getByPropsFn ( props : { [ propName : string ] : any } ) {
100
125
try {
@@ -161,6 +186,20 @@ export const getAllByPlaceholder = (instance: ReactTestInstance) =>
161
186
return results ;
162
187
} ;
163
188
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
+
164
203
export const getAllByProps = ( instance : ReactTestInstance ) =>
165
204
function getAllByPropsFn ( props : { [ propName : string ] : any } ) {
166
205
const results = instance . findAllByProps ( props ) ;
@@ -179,10 +218,12 @@ export const getByAPI = (instance: ReactTestInstance) => ({
179
218
getByType : getByType ( instance ) ,
180
219
getByText : getByText ( instance ) ,
181
220
getByPlaceholder : getByPlaceholder ( instance ) ,
221
+ getByDisplayValue : getByDisplayValue ( instance ) ,
182
222
getByProps : getByProps ( instance ) ,
183
223
getAllByName : getAllByName ( instance ) ,
184
224
getAllByType : getAllByType ( instance ) ,
185
225
getAllByText : getAllByText ( instance ) ,
186
226
getAllByPlaceholder : getAllByPlaceholder ( instance ) ,
227
+ getAllByDisplayValue : getAllByDisplayValue ( instance ) ,
187
228
getAllByProps : getAllByProps ( instance ) ,
188
229
} ) ;
0 commit comments