@@ -11,58 +11,60 @@ import {
11
11
getAllByText ,
12
12
getAllByProps ,
13
13
} from './getByAPI' ;
14
- import { logDeprecationWarning } from './errors' ;
14
+ import { ErrorWithStack , logDeprecationWarning } from './errors' ;
15
15
16
- export const queryByName = ( instance : ReactTestInstance ) => (
17
- name : string | React . ComponentType < * >
18
- ) => {
19
- logDeprecationWarning ( 'queryByName' , 'getByName' ) ;
20
- try {
21
- return getByName ( instance ) ( name ) ;
22
- } catch ( error ) {
16
+ const createQueryByError = ( error : Error , callsite : Function ) => {
17
+ if ( error . message . includes ( 'No instances found' ) ) {
23
18
return null ;
24
19
}
20
+ throw new ErrorWithStack ( error . message , callsite ) ;
25
21
} ;
26
22
27
- export const queryByType = ( instance : ReactTestInstance ) => (
28
- type : React . ComponentType < * >
29
- ) => {
30
- try {
31
- return getByType ( instance ) ( type ) ;
32
- } catch ( error ) {
33
- return null ;
34
- }
35
- } ;
23
+ export const queryByName = ( instance : ReactTestInstance ) =>
24
+ function queryByNameFn ( name : string | React . ComponentType < * > ) {
25
+ logDeprecationWarning ( 'queryByName' , 'getByName' ) ;
26
+ try {
27
+ return getByName ( instance ) ( name ) ;
28
+ } catch ( error ) {
29
+ return createQueryByError ( error , queryByNameFn ) ;
30
+ }
31
+ } ;
36
32
37
- export const queryByText = ( instance : ReactTestInstance ) => (
38
- text : string | RegExp
39
- ) => {
40
- try {
41
- return getByText ( instance ) ( text ) ;
42
- } catch ( error ) {
43
- return null ;
44
- }
45
- } ;
33
+ export const queryByType = ( instance : ReactTestInstance ) =>
34
+ function queryByTypeFn ( type : React . ComponentType < * > ) {
35
+ try {
36
+ return getByType ( instance ) ( type ) ;
37
+ } catch ( error ) {
38
+ return createQueryByError ( error , queryByTypeFn ) ;
39
+ }
40
+ } ;
46
41
47
- export const queryByProps = ( instance : ReactTestInstance ) => ( props : {
48
- [ propName : string ] : any ,
49
- } ) => {
50
- try {
51
- return getByProps ( instance ) ( props ) ;
52
- } catch ( error ) {
53
- return null ;
54
- }
55
- } ;
42
+ export const queryByText = ( instance : ReactTestInstance ) =>
43
+ function queryByTextFn ( text : string | RegExp ) {
44
+ try {
45
+ return getByText ( instance ) ( text ) ;
46
+ } catch ( error ) {
47
+ return createQueryByError ( error , queryByTextFn ) ;
48
+ }
49
+ } ;
56
50
57
- export const queryByTestId = ( instance : ReactTestInstance ) => (
58
- testID : string
59
- ) => {
60
- try {
61
- return getByTestId ( instance ) ( testID ) ;
62
- } catch ( error ) {
63
- return null ;
64
- }
65
- } ;
51
+ export const queryByProps = ( instance : ReactTestInstance ) =>
52
+ function queryByPropsFn ( props : { [ propName : string ] : any } ) {
53
+ try {
54
+ return getByProps ( instance ) ( props ) ;
55
+ } catch ( error ) {
56
+ return createQueryByError ( error , queryByPropsFn ) ;
57
+ }
58
+ } ;
59
+
60
+ export const queryByTestId = ( instance : ReactTestInstance ) =>
61
+ function queryByTestIdFn ( testID : string ) {
62
+ try {
63
+ return getByTestId ( instance ) ( testID ) ;
64
+ } catch ( error ) {
65
+ return createQueryByError ( error , queryByTestIdFn ) ;
66
+ }
67
+ } ;
66
68
67
69
export const queryAllByName = ( instance : ReactTestInstance ) => (
68
70
name : string | React . ComponentType < * >
0 commit comments