@@ -4,8 +4,9 @@ import prettyFormat from 'pretty-format';
4
4
import {
5
5
ErrorWithStack ,
6
6
createLibraryNotSupportedError ,
7
- logDeprecationWarning ,
8
7
prepareErrorMessage ,
8
+ printDeprecationWarning ,
9
+ printUnsafeWarning ,
9
10
} from './errors' ;
10
11
11
12
const filterNodeByType = ( node , type ) => node . type === type ;
@@ -68,9 +69,9 @@ const getTextInputNodeByDisplayValue = (node, value) => {
68
69
}
69
70
} ;
70
71
71
- export const getByName = ( instance : ReactTestInstance ) =>
72
+ export const getByName = ( instance : ReactTestInstance , warnFn ? : Function ) =>
72
73
function getByNameFn ( name : string | React . ComponentType < any > ) {
73
- logDeprecationWarning ( 'getByName' , 'getByType ') ;
74
+ warnFn && warnFn ( 'getByName' ) ;
74
75
try {
75
76
return typeof name === 'string'
76
77
? instance . find ( node => filterNodeByName ( node , name ) )
@@ -80,8 +81,9 @@ export const getByName = (instance: ReactTestInstance) =>
80
81
}
81
82
} ;
82
83
83
- export const getByType = ( instance : ReactTestInstance ) =>
84
+ export const getByType = ( instance : ReactTestInstance , warnFn ? : Function ) =>
84
85
function getByTypeFn ( type : React . ComponentType < any > ) {
86
+ warnFn && warnFn ( 'getByType' ) ;
85
87
try {
86
88
return instance . findByType ( type ) ;
87
89
} catch ( error ) {
@@ -120,8 +122,9 @@ export const getByDisplayValue = (instance: ReactTestInstance) =>
120
122
}
121
123
} ;
122
124
123
- export const getByProps = ( instance : ReactTestInstance ) =>
125
+ export const getByProps = ( instance : ReactTestInstance , warnFn ? : Function ) =>
124
126
function getByPropsFn ( props : { [ propName : string ] : any } ) {
127
+ warnFn && warnFn ( 'getByProps' ) ;
125
128
try {
126
129
return instance . findByProps ( props ) ;
127
130
} catch ( error ) {
@@ -138,9 +141,9 @@ export const getByTestId = (instance: ReactTestInstance) =>
138
141
}
139
142
} ;
140
143
141
- export const getAllByName = ( instance : ReactTestInstance ) =>
144
+ export const getAllByName = ( instance : ReactTestInstance , warnFn ? : Function ) =>
142
145
function getAllByNameFn ( name : string | React . ComponentType < any > ) {
143
- logDeprecationWarning ( 'getAllByName' , 'getAllByType ') ;
146
+ warnFn && warnFn ( 'getAllByName' ) ;
144
147
const results =
145
148
typeof name === 'string'
146
149
? instance . findAll ( node => filterNodeByName ( node , name ) )
@@ -151,8 +154,9 @@ export const getAllByName = (instance: ReactTestInstance) =>
151
154
return results ;
152
155
} ;
153
156
154
- export const getAllByType = ( instance : ReactTestInstance ) =>
157
+ export const getAllByType = ( instance : ReactTestInstance , warnFn ? : Function ) =>
155
158
function getAllByTypeFn ( type : React . ComponentType < any > ) {
159
+ warnFn && warnFn ( 'getAllByType' ) ;
156
160
const results = instance . findAllByType ( type ) ;
157
161
if ( results . length === 0 ) {
158
162
throw new ErrorWithStack ( 'No instances found' , getAllByTypeFn ) ;
@@ -200,8 +204,9 @@ export const getAllByDisplayValue = (instance: ReactTestInstance) =>
200
204
return results ;
201
205
} ;
202
206
203
- export const getAllByProps = ( instance : ReactTestInstance ) =>
207
+ export const getAllByProps = ( instance : ReactTestInstance , warnFn ? : Function ) =>
204
208
function getAllByPropsFn ( props : { [ propName : string ] : any } ) {
209
+ warnFn && warnFn ( 'getAllByProps' ) ;
205
210
const results = instance . findAllByProps ( props ) ;
206
211
if ( results . length === 0 ) {
207
212
throw new ErrorWithStack (
@@ -229,17 +234,23 @@ export const getAllByTestId = (instance: ReactTestInstance) =>
229
234
230
235
export const getByAPI = ( instance : ReactTestInstance ) => ( {
231
236
getByTestId : getByTestId ( instance ) ,
232
- getByName : getByName ( instance ) ,
233
- getByType : getByType ( instance ) ,
237
+ getByName : getByName ( instance , printDeprecationWarning ) ,
238
+ getByType : getByType ( instance , printUnsafeWarning ) ,
234
239
getByText : getByText ( instance ) ,
235
240
getByPlaceholder : getByPlaceholder ( instance ) ,
236
241
getByDisplayValue : getByDisplayValue ( instance ) ,
237
- getByProps : getByProps ( instance ) ,
242
+ getByProps : getByProps ( instance , printUnsafeWarning ) ,
238
243
getAllByTestId : getAllByTestId ( instance ) ,
239
- getAllByName : getAllByName ( instance ) ,
240
- getAllByType : getAllByType ( instance ) ,
244
+ getAllByName : getAllByName ( instance , printDeprecationWarning ) ,
245
+ getAllByType : getAllByType ( instance , printUnsafeWarning ) ,
241
246
getAllByText : getAllByText ( instance ) ,
242
247
getAllByPlaceholder : getAllByPlaceholder ( instance ) ,
243
248
getAllByDisplayValue : getAllByDisplayValue ( instance ) ,
244
- getAllByProps : getAllByProps ( instance ) ,
249
+ getAllByProps : getAllByProps ( instance , printUnsafeWarning ) ,
250
+
251
+ // Unsafe aliases
252
+ UNSAFE_getByType : getByType ( instance ) ,
253
+ UNSAFE_getAllByType : getAllByType ( instance ) ,
254
+ UNSAFE_getByProps : getByProps ( instance ) ,
255
+ UNSAFE_getAllByProps : getAllByProps ( instance ) ,
245
256
} ) ;
0 commit comments