diff --git a/src/helpers/errors.js b/src/helpers/errors.js index f7bbf98db..edf108cbc 100644 --- a/src/helpers/errors.js +++ b/src/helpers/errors.js @@ -13,37 +13,57 @@ export const createLibraryNotSupportedError = (error: Error) => `Currently the only supported library to search by text is "react-native".\n\n${error.message}` ); +export const prepareErrorMessage = (error: Error) => + // Strip info about custom predicate + error.message.replace(/ matching custom predicate[^]*/gm, ''); + +export const createQueryByError = (error: Error, callsite: Function) => { + if (error.message.includes('No instances found')) { + return null; + } + throw new ErrorWithStack(error.message, callsite); +}; + const warned = { getByName: false, getAllByName: false, queryByName: false, queryAllByName: false, + + getByProps: false, + getAllByProps: false, + queryByProps: false, + queryAllByProps: false, + + getByType: false, + getAllByType: false, + queryByType: false, + queryAllByType: false, }; -export const logDeprecationWarning = ( - deprecatedFnName: string, - alternativeFnName: string -) => { - if (warned[deprecatedFnName]) { +export function printDeprecationWarning(functionName: string) { + if (warned[functionName]) { return; } - console.warn(`Deprecation Warning: - "${deprecatedFnName}" is deprecated and will be removed in next major release. Please use "${alternativeFnName}" instead. + console.warn(` + Deprecation Warning: + ${functionName} is not recommended for use and will be deleted in react-native-testing-library 2.x. + `); - Docs: https://github.com/callstack/react-native-testing-library#${alternativeFnName.toLowerCase()}-type-reactcomponenttype - `); + warned[functionName] = true; +} - warned[deprecatedFnName] = true; -}; +export function printUnsafeWarning(functionName: string) { + if (warned[functionName]) { + return; + } -export const prepareErrorMessage = (error: Error) => - // Strip info about custom predicate - error.message.replace(/ matching custom predicate[^]*/gm, ''); + console.warn(` + Deprecation Warning: + ${functionName} is not recommended for use and has been renamed to UNSAFE_${functionName}. + In react-native-testing-library 2.x only the UNSAFE_${functionName} name will work. + `); -export const createQueryByError = (error: Error, callsite: Function) => { - if (error.message.includes('No instances found')) { - return null; - } - throw new ErrorWithStack(error.message, callsite); -}; + warned[functionName] = true; +} diff --git a/src/helpers/getByAPI.js b/src/helpers/getByAPI.js index 9fb03e874..b0bf792ad 100644 --- a/src/helpers/getByAPI.js +++ b/src/helpers/getByAPI.js @@ -4,8 +4,9 @@ import prettyFormat from 'pretty-format'; import { ErrorWithStack, createLibraryNotSupportedError, - logDeprecationWarning, prepareErrorMessage, + printDeprecationWarning, + printUnsafeWarning, } from './errors'; const filterNodeByType = (node, type) => node.type === type; @@ -68,9 +69,9 @@ const getTextInputNodeByDisplayValue = (node, value) => { } }; -export const getByName = (instance: ReactTestInstance) => +export const getByName = (instance: ReactTestInstance, warnFn?: Function) => function getByNameFn(name: string | React.ComponentType) { - logDeprecationWarning('getByName', 'getByType'); + warnFn && warnFn('getByName'); try { return typeof name === 'string' ? instance.find(node => filterNodeByName(node, name)) @@ -80,8 +81,9 @@ export const getByName = (instance: ReactTestInstance) => } }; -export const getByType = (instance: ReactTestInstance) => +export const getByType = (instance: ReactTestInstance, warnFn?: Function) => function getByTypeFn(type: React.ComponentType) { + warnFn && warnFn('getByType'); try { return instance.findByType(type); } catch (error) { @@ -120,8 +122,9 @@ export const getByDisplayValue = (instance: ReactTestInstance) => } }; -export const getByProps = (instance: ReactTestInstance) => +export const getByProps = (instance: ReactTestInstance, warnFn?: Function) => function getByPropsFn(props: { [propName: string]: any }) { + warnFn && warnFn('getByProps'); try { return instance.findByProps(props); } catch (error) { @@ -138,9 +141,9 @@ export const getByTestId = (instance: ReactTestInstance) => } }; -export const getAllByName = (instance: ReactTestInstance) => +export const getAllByName = (instance: ReactTestInstance, warnFn?: Function) => function getAllByNameFn(name: string | React.ComponentType) { - logDeprecationWarning('getAllByName', 'getAllByType'); + warnFn && warnFn('getAllByName'); const results = typeof name === 'string' ? instance.findAll(node => filterNodeByName(node, name)) @@ -151,8 +154,9 @@ export const getAllByName = (instance: ReactTestInstance) => return results; }; -export const getAllByType = (instance: ReactTestInstance) => +export const getAllByType = (instance: ReactTestInstance, warnFn?: Function) => function getAllByTypeFn(type: React.ComponentType) { + warnFn && warnFn('getAllByType'); const results = instance.findAllByType(type); if (results.length === 0) { throw new ErrorWithStack('No instances found', getAllByTypeFn); @@ -200,8 +204,9 @@ export const getAllByDisplayValue = (instance: ReactTestInstance) => return results; }; -export const getAllByProps = (instance: ReactTestInstance) => +export const getAllByProps = (instance: ReactTestInstance, warnFn?: Function) => function getAllByPropsFn(props: { [propName: string]: any }) { + warnFn && warnFn('getAllByProps'); const results = instance.findAllByProps(props); if (results.length === 0) { throw new ErrorWithStack( @@ -229,17 +234,23 @@ export const getAllByTestId = (instance: ReactTestInstance) => export const getByAPI = (instance: ReactTestInstance) => ({ getByTestId: getByTestId(instance), - getByName: getByName(instance), - getByType: getByType(instance), + getByName: getByName(instance, printDeprecationWarning), + getByType: getByType(instance, printUnsafeWarning), getByText: getByText(instance), getByPlaceholder: getByPlaceholder(instance), getByDisplayValue: getByDisplayValue(instance), - getByProps: getByProps(instance), + getByProps: getByProps(instance, printUnsafeWarning), getAllByTestId: getAllByTestId(instance), - getAllByName: getAllByName(instance), - getAllByType: getAllByType(instance), + getAllByName: getAllByName(instance, printDeprecationWarning), + getAllByType: getAllByType(instance, printUnsafeWarning), getAllByText: getAllByText(instance), getAllByPlaceholder: getAllByPlaceholder(instance), getAllByDisplayValue: getAllByDisplayValue(instance), - getAllByProps: getAllByProps(instance), + getAllByProps: getAllByProps(instance, printUnsafeWarning), + + // Unsafe aliases + UNSAFE_getByType: getByType(instance), + UNSAFE_getAllByType: getAllByType(instance), + UNSAFE_getByProps: getByProps(instance), + UNSAFE_getAllByProps: getAllByProps(instance), }); diff --git a/src/helpers/queryByAPI.js b/src/helpers/queryByAPI.js index 9b692139a..e2c06b0a0 100644 --- a/src/helpers/queryByAPI.js +++ b/src/helpers/queryByAPI.js @@ -16,11 +16,15 @@ import { getAllByDisplayValue, getAllByProps, } from './getByAPI'; -import { logDeprecationWarning, createQueryByError } from './errors'; +import { + createQueryByError, + printDeprecationWarning, + printUnsafeWarning, +} from './errors'; -export const queryByName = (instance: ReactTestInstance) => +export const queryByName = (instance: ReactTestInstance, warnFn?: Function) => function queryByNameFn(name: string | React.ComponentType) { - logDeprecationWarning('queryByName', 'getByName'); + warnFn && warnFn('queryByName'); try { return getByName(instance)(name); } catch (error) { @@ -28,8 +32,9 @@ export const queryByName = (instance: ReactTestInstance) => } }; -export const queryByType = (instance: ReactTestInstance) => +export const queryByType = (instance: ReactTestInstance, warnFn?: Function) => function queryByTypeFn(type: React.ComponentType) { + warnFn && warnFn('queryByType'); try { return getByType(instance)(type); } catch (error) { @@ -64,8 +69,9 @@ export const queryByDisplayValue = (instance: ReactTestInstance) => } }; -export const queryByProps = (instance: ReactTestInstance) => +export const queryByProps = (instance: ReactTestInstance, warnFn?: Function) => function queryByPropsFn(props: { [propName: string]: any }) { + warnFn && warnFn('queryByProps'); try { return getByProps(instance)(props); } catch (error) { @@ -82,10 +88,11 @@ export const queryByTestId = (instance: ReactTestInstance) => } }; -export const queryAllByName = (instance: ReactTestInstance) => ( - name: string | React.ComponentType -) => { - logDeprecationWarning('queryAllByName', 'getAllByName'); +export const queryAllByName = ( + instance: ReactTestInstance, + warnFn?: Function +) => (name: string | React.ComponentType) => { + warnFn && warnFn('queryAllByName'); try { return getAllByName(instance)(name); } catch (error) { @@ -93,9 +100,11 @@ export const queryAllByName = (instance: ReactTestInstance) => ( } }; -export const queryAllByType = (instance: ReactTestInstance) => ( - type: React.ComponentType -) => { +export const queryAllByType = ( + instance: ReactTestInstance, + warnFn?: Function +) => (type: React.ComponentType) => { + warnFn && warnFn('queryAllByType'); try { return getAllByType(instance)(type); } catch (error) { @@ -133,9 +142,11 @@ export const queryAllByDisplayValue = (instance: ReactTestInstance) => ( } }; -export const queryAllByProps = (instance: ReactTestInstance) => (props: { - [propName: string]: any, -}) => { +export const queryAllByProps = ( + instance: ReactTestInstance, + warnFn?: Function +) => (props: { [propName: string]: any }) => { + warnFn && warnFn('queryAllByProps'); try { return getAllByProps(instance)(props); } catch (error) { @@ -155,17 +166,23 @@ export const queryAllByTestId = (instance: ReactTestInstance) => ( export const queryByAPI = (instance: ReactTestInstance) => ({ queryByTestId: queryByTestId(instance), - queryByName: queryByName(instance), - queryByType: queryByType(instance), + queryByName: queryByName(instance, printDeprecationWarning), + queryByType: queryByType(instance, printUnsafeWarning), queryByText: queryByText(instance), queryByPlaceholder: queryByPlaceholder(instance), queryByDisplayValue: queryByDisplayValue(instance), - queryByProps: queryByProps(instance), + queryByProps: queryByProps(instance, printUnsafeWarning), queryAllByTestId: queryAllByTestId(instance), - queryAllByName: queryAllByName(instance), - queryAllByType: queryAllByType(instance), + queryAllByName: queryAllByName(instance, printDeprecationWarning), + queryAllByType: queryAllByType(instance, printUnsafeWarning), queryAllByText: queryAllByText(instance), queryAllByPlaceholder: queryAllByPlaceholder(instance), queryAllByDisplayValue: queryAllByDisplayValue(instance), - queryAllByProps: queryAllByProps(instance), + queryAllByProps: queryAllByProps(instance, printUnsafeWarning), + + // Unsafe aliases + UNSAFE_queryByType: queryByType(instance), + UNSAFE_queryAllByType: queryAllByType(instance), + UNSAFE_queryByProps: queryByProps(instance), + UNSAFE_queryAllByProps: queryAllByProps(instance), }); diff --git a/typings/index.d.ts b/typings/index.d.ts index d9d83c4b4..33c4ff1e7 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -18,6 +18,13 @@ export interface GetByAPI { ) => Array; getAllByDisplayValue: (value: string | RegExp) => Array; getAllByProps: (props: Record) => Array; + + + // Unsafe aliases + UNSAFE_getByType:

(type: React.ComponentType

) => ReactTestInstance, + UNSAFE_getAllByType:

(type: React.ComponentType

) => Array, + UNSAFE_getByProps: (props: Record) => ReactTestInstance, + UNSAFE_getAllByProps: (props: Record) => Array, } export interface QueryByAPI { @@ -47,6 +54,12 @@ export interface QueryByAPI { queryAllByProps: ( props: Record ) => Array | []; + + // Unsafe aliases + UNSAFE_queryByType:

(type: React.ComponentType

) => ReactTestInstance | null, + UNSAFE_queryAllByType:

(type: React.ComponentType

) => Array | [], + UNSAFE_queryByProps: (props: Record) => ReactTestInstance | null, + UNSAFE_queryAllByProps: (props: Record) => Array | [], } type QueryFn = (text: string | RegExp) => ReactTestInstance | null;