mirror of
https://github.com/callstack/react-native-testing-library.git
synced 2026-09-18 23:09:04 +08:00
imp: Re-alias deprecated matchers and warn (#261)
* imp: Re-alias deprecated matchers * Prepare for byName deletion
This commit is contained in:
+44
-24
@@ -13,30 +13,6 @@ export const createLibraryNotSupportedError = (error: Error) =>
|
||||
`Currently the only supported library to search by text is "react-native".\n\n${error.message}`
|
||||
);
|
||||
|
||||
const warned = {
|
||||
getByName: false,
|
||||
getAllByName: false,
|
||||
queryByName: false,
|
||||
queryAllByName: false,
|
||||
};
|
||||
|
||||
export const logDeprecationWarning = (
|
||||
deprecatedFnName: string,
|
||||
alternativeFnName: string
|
||||
) => {
|
||||
if (warned[deprecatedFnName]) {
|
||||
return;
|
||||
}
|
||||
console.warn(`Deprecation Warning:
|
||||
|
||||
"${deprecatedFnName}" is deprecated and will be removed in next major release. Please use "${alternativeFnName}" instead.
|
||||
|
||||
Docs: https://github.com/callstack/react-native-testing-library#${alternativeFnName.toLowerCase()}-type-reactcomponenttype
|
||||
`);
|
||||
|
||||
warned[deprecatedFnName] = true;
|
||||
};
|
||||
|
||||
export const prepareErrorMessage = (error: Error) =>
|
||||
// Strip info about custom predicate
|
||||
error.message.replace(/ matching custom predicate[^]*/gm, '');
|
||||
@@ -47,3 +23,47 @@ export const createQueryByError = (error: Error, callsite: Function) => {
|
||||
}
|
||||
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 function printDeprecationWarning(functionName: string) {
|
||||
if (warned[functionName]) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.warn(`
|
||||
Deprecation Warning:
|
||||
${functionName} is not recommended for use and will be deleted in react-native-testing-library 2.x.
|
||||
`);
|
||||
|
||||
warned[functionName] = true;
|
||||
}
|
||||
|
||||
export function printUnsafeWarning(functionName: string) {
|
||||
if (warned[functionName]) {
|
||||
return;
|
||||
}
|
||||
|
||||
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.
|
||||
`);
|
||||
|
||||
warned[functionName] = true;
|
||||
}
|
||||
|
||||
+26
-15
@@ -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<any>) {
|
||||
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<any>) {
|
||||
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<any>) {
|
||||
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<any>) {
|
||||
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),
|
||||
});
|
||||
|
||||
+38
-21
@@ -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<any>) {
|
||||
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<any>) {
|
||||
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<any>
|
||||
) => {
|
||||
logDeprecationWarning('queryAllByName', 'getAllByName');
|
||||
export const queryAllByName = (
|
||||
instance: ReactTestInstance,
|
||||
warnFn?: Function
|
||||
) => (name: string | React.ComponentType<any>) => {
|
||||
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<any>
|
||||
) => {
|
||||
export const queryAllByType = (
|
||||
instance: ReactTestInstance,
|
||||
warnFn?: Function
|
||||
) => (type: React.ComponentType<any>) => {
|
||||
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),
|
||||
});
|
||||
|
||||
Vendored
+13
@@ -19,6 +19,13 @@ export interface GetByAPI {
|
||||
) => Array<ReactTestInstance>;
|
||||
getAllByDisplayValue: (value: string | RegExp) => Array<ReactTestInstance>;
|
||||
getAllByProps: (props: Record<string, any>) => Array<ReactTestInstance>;
|
||||
|
||||
|
||||
// Unsafe aliases
|
||||
UNSAFE_getByType: <P>(type: React.ComponentType<P>) => ReactTestInstance,
|
||||
UNSAFE_getAllByType: <P>(type: React.ComponentType<P>) => Array<ReactTestInstance>,
|
||||
UNSAFE_getByProps: (props: Record<string, any>) => ReactTestInstance,
|
||||
UNSAFE_getAllByProps: (props: Record<string, any>) => Array<ReactTestInstance>,
|
||||
}
|
||||
|
||||
export interface QueryByAPI {
|
||||
@@ -48,6 +55,12 @@ export interface QueryByAPI {
|
||||
queryAllByProps: (
|
||||
props: Record<string, any>
|
||||
) => Array<ReactTestInstance> | [];
|
||||
|
||||
// Unsafe aliases
|
||||
UNSAFE_queryByType: <P>(type: React.ComponentType<P>) => ReactTestInstance | null,
|
||||
UNSAFE_queryAllByType: <P>(type: React.ComponentType<P>) => Array<ReactTestInstance> | [],
|
||||
UNSAFE_queryByProps: (props: Record<string, any>) => ReactTestInstance | null,
|
||||
UNSAFE_queryAllByProps: (props: Record<string, any>) => Array<ReactTestInstance> | [],
|
||||
}
|
||||
|
||||
type GetReturn = ReactTestInstance;
|
||||
|
||||
Reference in New Issue
Block a user