11// @flow
22import * as React from 'react' ;
3- import ErrorWithStack from './errorWithStack' ;
3+ import prettyFormat from 'pretty-format' ;
4+ import { ErrorWithStack , createLibraryNotSupportedError } from './errors' ;
45
5- const getNodeByName = ( node , name ) =>
6- node . type . name === name ||
7- node . type . displayName === name ||
8- node . type === name ;
6+ const filterNodeByType = ( node , type ) => node . type === type ;
97
10- const getNodeByText = ( node , text ) =>
11- ( getNodeByName ( node , 'Text' ) || getNodeByName ( node , 'TextInput' ) ) &&
12- ( typeof text === 'string'
13- ? text === node . props . children
14- : text . test ( node . props . children ) ) ;
8+ const filterNodeByName = ( node , name ) =>
9+ typeof node . type !== 'string' &&
10+ ( node . type . displayName === name || node . type . name === name ) ;
1511
12+ const getNodeByText = ( node , text ) => {
13+ try {
14+ // eslint-disable-next-line
15+ const { Text, TextInput } = require ( 'react-native' ) ;
16+ return (
17+ ( filterNodeByType ( node , Text ) || filterNodeByType ( node , TextInput ) ) &&
18+ ( typeof text === 'string'
19+ ? text === node . props . children
20+ : text . test ( node . props . children ) )
21+ ) ;
22+ } catch ( error ) {
23+ throw createLibraryNotSupportedError ( error ) ;
24+ }
25+ } ;
26+
27+ const prepareErrorMessage = error =>
28+ // Strip info about custom predicate
29+ error . message . replace ( / m a t c h i n g c u s t o m p r e d i c a t e [ ^ ] * / gm, '' ) ;
30+
31+ // TODO: deprecate getByName(string | type) in favor of getByType(type)
1632export const getByName = ( instance : ReactTestInstance ) =>
1733 function getByNameFn ( name : string | React . ComponentType < * > ) {
1834 try {
19- return instance . find ( node => getNodeByName ( node , name ) ) ;
35+ return typeof name === 'string'
36+ ? instance . find ( node => filterNodeByName ( node , name ) )
37+ : instance . findByType ( name ) ;
2038 } catch ( error ) {
21- throw new ErrorWithStack ( `Component not found.` , getByNameFn ) ;
39+ throw new ErrorWithStack ( prepareErrorMessage ( error ) , getByNameFn ) ;
2240 }
2341 } ;
2442
@@ -27,7 +45,7 @@ export const getByText = (instance: ReactTestInstance) =>
2745 try {
2846 return instance . find ( node => getNodeByText ( node , text ) ) ;
2947 } catch ( error ) {
30- throw new ErrorWithStack ( `Component not found.` , getByTextFn ) ;
48+ throw new ErrorWithStack ( prepareErrorMessage ( error ) , getByTextFn ) ;
3149 }
3250 } ;
3351
@@ -36,7 +54,7 @@ export const getByProps = (instance: ReactTestInstance) =>
3654 try {
3755 return instance . findByProps ( props ) ;
3856 } catch ( error ) {
39- throw new ErrorWithStack ( `Component not found.` , getByPropsFn ) ;
57+ throw new ErrorWithStack ( prepareErrorMessage ( error ) , getByPropsFn ) ;
4058 }
4159 } ;
4260
@@ -45,15 +63,19 @@ export const getByTestId = (instance: ReactTestInstance) =>
4563 try {
4664 return instance . findByProps ( { testID } ) ;
4765 } catch ( error ) {
48- throw new ErrorWithStack ( `Component not found.` , getByTestIdFn ) ;
66+ throw new ErrorWithStack ( prepareErrorMessage ( error ) , getByTestIdFn ) ;
4967 }
5068 } ;
5169
70+ // TODO: deprecate getAllByName(string | type) in favor of getAllByType(type)
5271export const getAllByName = ( instance : ReactTestInstance ) =>
5372 function getAllByNameFn ( name : string | React . ComponentType < * > ) {
54- const results = instance . findAll ( node => getNodeByName ( node , name ) ) ;
73+ const results =
74+ typeof name === 'string'
75+ ? instance . findAll ( node => filterNodeByName ( node , name ) )
76+ : instance . findAllByType ( name ) ;
5577 if ( results . length === 0 ) {
56- throw new ErrorWithStack ( `Components not found.` , getAllByNameFn ) ;
78+ throw new ErrorWithStack ( 'No instances found' , getAllByNameFn ) ;
5779 }
5880 return results ;
5981 } ;
@@ -62,7 +84,10 @@ export const getAllByText = (instance: ReactTestInstance) =>
6284 function getAllByTextFn ( text : string | RegExp ) {
6385 const results = instance . findAll ( node => getNodeByText ( node , text ) ) ;
6486 if ( results . length === 0 ) {
65- throw new ErrorWithStack ( `Components not found.` , getAllByTextFn ) ;
87+ throw new ErrorWithStack (
88+ `No instances found with text: ${ String ( text ) } ` ,
89+ getAllByTextFn
90+ ) ;
6691 }
6792 return results ;
6893 } ;
@@ -71,7 +96,10 @@ export const getAllByProps = (instance: ReactTestInstance) =>
7196 function getAllByPropsFn ( props : { [ propName : string ] : any } ) {
7297 const results = instance . findAllByProps ( props ) ;
7398 if ( results . length === 0 ) {
74- throw new ErrorWithStack ( `Components not found.` , getAllByPropsFn ) ;
99+ throw new ErrorWithStack (
100+ `No instances found with props:\n${ prettyFormat ( props ) } ` ,
101+ getAllByPropsFn
102+ ) ;
75103 }
76104 return results ;
77105 } ;
0 commit comments