mirror of
https://github.com/callstack/react-native-testing-library.git
synced 2026-09-18 23:09:04 +08:00
feat: deprecate getByName helpers (#66)
This commit is contained in:
committed by
Kacper Wiszczuk
parent
909916e84c
commit
e407ff07ae
@@ -14,3 +14,27 @@ export const createLibraryNotSupportedError = (error: Error) =>
|
||||
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;
|
||||
};
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
// @flow
|
||||
import * as React from 'react';
|
||||
import prettyFormat from 'pretty-format';
|
||||
import { ErrorWithStack, createLibraryNotSupportedError } from './errors';
|
||||
import {
|
||||
ErrorWithStack,
|
||||
createLibraryNotSupportedError,
|
||||
logDeprecationWarning,
|
||||
} from './errors';
|
||||
|
||||
const filterNodeByType = (node, type) => node.type === type;
|
||||
|
||||
@@ -28,9 +32,9 @@ const prepareErrorMessage = error =>
|
||||
// Strip info about custom predicate
|
||||
error.message.replace(/ matching custom predicate[^]*/gm, '');
|
||||
|
||||
// TODO: deprecate getByName(string | type) in favor of getByType(type)
|
||||
export const getByName = (instance: ReactTestInstance) =>
|
||||
function getByNameFn(name: string | React.ComponentType<*>) {
|
||||
logDeprecationWarning('getByName', 'getByType');
|
||||
try {
|
||||
return typeof name === 'string'
|
||||
? instance.find(node => filterNodeByName(node, name))
|
||||
@@ -76,9 +80,9 @@ export const getByTestId = (instance: ReactTestInstance) =>
|
||||
}
|
||||
};
|
||||
|
||||
// TODO: deprecate getAllByName(string | type) in favor of getAllByType(type)
|
||||
export const getAllByName = (instance: ReactTestInstance) =>
|
||||
function getAllByNameFn(name: string | React.ComponentType<*>) {
|
||||
logDeprecationWarning('getAllByName', 'getAllByType');
|
||||
const results =
|
||||
typeof name === 'string'
|
||||
? instance.findAll(node => filterNodeByName(node, name))
|
||||
|
||||
@@ -11,10 +11,12 @@ import {
|
||||
getAllByText,
|
||||
getAllByProps,
|
||||
} from './getByAPI';
|
||||
import { logDeprecationWarning } from './errors';
|
||||
|
||||
export const queryByName = (instance: ReactTestInstance) => (
|
||||
name: string | React.ComponentType<*>
|
||||
) => {
|
||||
logDeprecationWarning('queryByName', 'getByName');
|
||||
try {
|
||||
return getByName(instance)(name);
|
||||
} catch (error) {
|
||||
@@ -65,6 +67,7 @@ export const queryByTestId = (instance: ReactTestInstance) => (
|
||||
export const queryAllByName = (instance: ReactTestInstance) => (
|
||||
name: string | React.ComponentType<*>
|
||||
) => {
|
||||
logDeprecationWarning('queryAllByName', 'getAllByName');
|
||||
try {
|
||||
return getAllByName(instance)(name);
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user