1
1
// @flow
2
2
import * as React from 'react' ;
3
- import ErrorWithStack from './errorWithStack' ;
3
+ import prettyFormat from 'pretty-format' ;
4
+ import { ErrorWithStack , createLibraryNotSupportedError } from './errors' ;
4
5
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 ;
9
7
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 ) ;
15
11
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)
16
32
export const getByName = ( instance : ReactTestInstance ) =>
17
33
function getByNameFn ( name : string | React . ComponentType < * > ) {
18
34
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 ) ;
20
38
} catch ( error ) {
21
- throw new ErrorWithStack ( `Component not found.` , getByNameFn ) ;
39
+ throw new ErrorWithStack ( prepareErrorMessage ( error ) , getByNameFn ) ;
22
40
}
23
41
} ;
24
42
@@ -27,7 +45,7 @@ export const getByText = (instance: ReactTestInstance) =>
27
45
try {
28
46
return instance . find ( node => getNodeByText ( node , text ) ) ;
29
47
} catch ( error ) {
30
- throw new ErrorWithStack ( `Component not found.` , getByTextFn ) ;
48
+ throw new ErrorWithStack ( prepareErrorMessage ( error ) , getByTextFn ) ;
31
49
}
32
50
} ;
33
51
@@ -36,7 +54,7 @@ export const getByProps = (instance: ReactTestInstance) =>
36
54
try {
37
55
return instance . findByProps ( props ) ;
38
56
} catch ( error ) {
39
- throw new ErrorWithStack ( `Component not found.` , getByPropsFn ) ;
57
+ throw new ErrorWithStack ( prepareErrorMessage ( error ) , getByPropsFn ) ;
40
58
}
41
59
} ;
42
60
@@ -45,15 +63,19 @@ export const getByTestId = (instance: ReactTestInstance) =>
45
63
try {
46
64
return instance . findByProps ( { testID } ) ;
47
65
} catch ( error ) {
48
- throw new ErrorWithStack ( `Component not found.` , getByTestIdFn ) ;
66
+ throw new ErrorWithStack ( prepareErrorMessage ( error ) , getByTestIdFn ) ;
49
67
}
50
68
} ;
51
69
70
+ // TODO: deprecate getAllByName(string | type) in favor of getAllByType(type)
52
71
export const getAllByName = ( instance : ReactTestInstance ) =>
53
72
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 ) ;
55
77
if ( results . length === 0 ) {
56
- throw new ErrorWithStack ( `Components not found.` , getAllByNameFn ) ;
78
+ throw new ErrorWithStack ( 'No instances found' , getAllByNameFn ) ;
57
79
}
58
80
return results ;
59
81
} ;
@@ -62,7 +84,10 @@ export const getAllByText = (instance: ReactTestInstance) =>
62
84
function getAllByTextFn ( text : string | RegExp ) {
63
85
const results = instance . findAll ( node => getNodeByText ( node , text ) ) ;
64
86
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
+ ) ;
66
91
}
67
92
return results ;
68
93
} ;
@@ -71,7 +96,10 @@ export const getAllByProps = (instance: ReactTestInstance) =>
71
96
function getAllByPropsFn ( props : { [ propName : string ] : any } ) {
72
97
const results = instance . findAllByProps ( props ) ;
73
98
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
+ ) ;
75
103
}
76
104
return results ;
77
105
} ;
0 commit comments