Skip to content

imp: Re-alias deprecated matchers and warn #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 40 additions & 20 deletions src/helpers/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
41 changes: 26 additions & 15 deletions src/helpers/getByAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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))
Expand All @@ -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);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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),
});
59 changes: 38 additions & 21 deletions src/helpers/queryByAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,25 @@ 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) {
return createQueryByError(error, queryByNameFn);
}
};

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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -82,20 +88,23 @@ 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) {
return [];
}
};

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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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),
});
13 changes: 13 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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 {
Expand Down Expand Up @@ -47,6 +54,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 QueryFn = (text: string | RegExp) => ReactTestInstance | null;
Expand Down