@@ -4,8 +4,9 @@ import prettyFormat from 'pretty-format';
44import {
55 ErrorWithStack ,
66 createLibraryNotSupportedError ,
7- logDeprecationWarning ,
87 prepareErrorMessage ,
8+ printDeprecationWarning ,
9+ printUnsafeWarning ,
910} from './errors' ;
1011
1112const filterNodeByType = ( node , type ) => node . type === type ;
@@ -68,9 +69,9 @@ const getTextInputNodeByDisplayValue = (node, value) => {
6869 }
6970} ;
7071
71- export const getByName = ( instance : ReactTestInstance ) =>
72+ export const getByName = ( instance : ReactTestInstance , warnFn ? : Function ) =>
7273 function getByNameFn ( name : string | React . ComponentType < any > ) {
73- logDeprecationWarning ( 'getByName' , 'getByType ') ;
74+ warnFn && warnFn ( 'getByName' ) ;
7475 try {
7576 return typeof name === 'string'
7677 ? instance . find ( node => filterNodeByName ( node , name ) )
@@ -80,8 +81,9 @@ export const getByName = (instance: ReactTestInstance) =>
8081 }
8182 } ;
8283
83- export const getByType = ( instance : ReactTestInstance ) =>
84+ export const getByType = ( instance : ReactTestInstance , warnFn ? : Function ) =>
8485 function getByTypeFn ( type : React . ComponentType < any > ) {
86+ warnFn && warnFn ( 'getByType' ) ;
8587 try {
8688 return instance . findByType ( type ) ;
8789 } catch ( error ) {
@@ -120,8 +122,9 @@ export const getByDisplayValue = (instance: ReactTestInstance) =>
120122 }
121123 } ;
122124
123- export const getByProps = ( instance : ReactTestInstance ) =>
125+ export const getByProps = ( instance : ReactTestInstance , warnFn ? : Function ) =>
124126 function getByPropsFn ( props : { [ propName : string ] : any } ) {
127+ warnFn && warnFn ( 'getByProps' ) ;
125128 try {
126129 return instance . findByProps ( props ) ;
127130 } catch ( error ) {
@@ -138,9 +141,9 @@ export const getByTestId = (instance: ReactTestInstance) =>
138141 }
139142 } ;
140143
141- export const getAllByName = ( instance : ReactTestInstance ) =>
144+ export const getAllByName = ( instance : ReactTestInstance , warnFn ? : Function ) =>
142145 function getAllByNameFn ( name : string | React . ComponentType < any > ) {
143- logDeprecationWarning ( 'getAllByName' , 'getAllByType ') ;
146+ warnFn && warnFn ( 'getAllByName' ) ;
144147 const results =
145148 typeof name === 'string'
146149 ? instance . findAll ( node => filterNodeByName ( node , name ) )
@@ -151,8 +154,9 @@ export const getAllByName = (instance: ReactTestInstance) =>
151154 return results ;
152155 } ;
153156
154- export const getAllByType = ( instance : ReactTestInstance ) =>
157+ export const getAllByType = ( instance : ReactTestInstance , warnFn ? : Function ) =>
155158 function getAllByTypeFn ( type : React . ComponentType < any > ) {
159+ warnFn && warnFn ( 'getAllByType' ) ;
156160 const results = instance . findAllByType ( type ) ;
157161 if ( results . length === 0 ) {
158162 throw new ErrorWithStack ( 'No instances found' , getAllByTypeFn ) ;
@@ -200,8 +204,9 @@ export const getAllByDisplayValue = (instance: ReactTestInstance) =>
200204 return results ;
201205 } ;
202206
203- export const getAllByProps = ( instance : ReactTestInstance ) =>
207+ export const getAllByProps = ( instance : ReactTestInstance , warnFn ? : Function ) =>
204208 function getAllByPropsFn ( props : { [ propName : string ] : any } ) {
209+ warnFn && warnFn ( 'getAllByProps' ) ;
205210 const results = instance . findAllByProps ( props ) ;
206211 if ( results . length === 0 ) {
207212 throw new ErrorWithStack (
@@ -229,17 +234,23 @@ export const getAllByTestId = (instance: ReactTestInstance) =>
229234
230235export const getByAPI = ( instance : ReactTestInstance ) => ( {
231236 getByTestId : getByTestId ( instance ) ,
232- getByName : getByName ( instance ) ,
233- getByType : getByType ( instance ) ,
237+ getByName : getByName ( instance , printDeprecationWarning ) ,
238+ getByType : getByType ( instance , printUnsafeWarning ) ,
234239 getByText : getByText ( instance ) ,
235240 getByPlaceholder : getByPlaceholder ( instance ) ,
236241 getByDisplayValue : getByDisplayValue ( instance ) ,
237- getByProps : getByProps ( instance ) ,
242+ getByProps : getByProps ( instance , printUnsafeWarning ) ,
238243 getAllByTestId : getAllByTestId ( instance ) ,
239- getAllByName : getAllByName ( instance ) ,
240- getAllByType : getAllByType ( instance ) ,
244+ getAllByName : getAllByName ( instance , printDeprecationWarning ) ,
245+ getAllByType : getAllByType ( instance , printUnsafeWarning ) ,
241246 getAllByText : getAllByText ( instance ) ,
242247 getAllByPlaceholder : getAllByPlaceholder ( instance ) ,
243248 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 ) ,
245256} ) ;
0 commit comments