Skip to content

Commit e407ff0

Browse files
thymikeeEsemesek
authored andcommitted
feat: deprecate getByName helpers (#66)
1 parent 909916e commit e407ff0

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

README.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,21 @@ type ReactTestInstance = {
105105
};
106106
```
107107

108-
### `getByName: (name: React.ComponentType<*>)`
108+
### `getByText: (text: string | RegExp)`
109109

110-
A method returning a `ReactTestInstance` with matching a React component type. Throws when no matches.
110+
A method returning a `ReactTestInstance` with matching text – may be a string or regular expression. Throws when no matches.
111111

112-
### `getAllByName: (name: React.ComponentType<*>)`
112+
### `getAllByText: (text: string | RegExp)`
113113

114-
A method returning an array of `ReactTestInstance`s with matching a React component type.
114+
A method returning an array of `ReactTestInstance`s with matching text – may be a string or regular expression.
115+
116+
### `getByProps: (props: { [propName: string]: any })`
117+
118+
A method returning a `ReactTestInstance` with matching props object. Throws when no matches.
119+
120+
### `getAllByProps: (props: { [propName: string]: any })`
121+
122+
A method returning an array of `ReactTestInstance`s with matching props object.
115123

116124
### `getByType: (type: React.ComponentType<*>)`
117125

@@ -121,21 +129,17 @@ A method returning a `ReactTestInstance` with matching a React component type. T
121129

122130
A method returning an array of `ReactTestInstance`s with matching a React component type.
123131

124-
### `getByText: (text: string | RegExp)`
125-
126-
A method returning a `ReactTestInstance` with matching text – may be a string or regular expression. Throws when no matches.
132+
### `[DEPRECATED] getByName: (name: React.ComponentType<*>)`
127133

128-
### `getAllByText: (text: string | RegExp)`
129-
130-
A method returning an array of `ReactTestInstance`s with matching text – may be a string or regular expression.
134+
A method returning a `ReactTestInstance` with matching a React component type. Throws when no matches.
131135

132-
### `getByProps: (props: { [propName: string]: any })`
136+
> This method has been **deprecated** because using it results in fragile tests that may break between minor React Native versions. It will be removed in next major release (v2.0). Use [`getByType`](#getbytype-type-reactcomponenttype) instead.
133137
134-
A method returning a `ReactTestInstance` with matching props object. Throws when no matches.
138+
### `[DEPRECATED] getAllByName: (name: React.ComponentType<*>)`
135139

136-
### `getAllByProps: (props: { [propName: string]: any })`
140+
A method returning an array of `ReactTestInstance`s with matching a React component type.
137141

138-
A method returning an array of `ReactTestInstance`s with matching props object.
142+
> This method has been **deprecated** because using it results in fragile tests that may break between minor React Native versions. It will be removed in next major release (v2.0). Use [`getAllByType`](#getallbytype-type-reactcomponenttype) instead.
139143
140144
### `update: (element: React.Element<*>) => void`
141145

src/helpers/errors.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,27 @@ export const createLibraryNotSupportedError = (error: Error) =>
1414
error.message
1515
}`
1616
);
17+
18+
const warned = {
19+
getByName: false,
20+
getAllByName: false,
21+
queryByName: false,
22+
queryAllByName: false,
23+
};
24+
25+
export const logDeprecationWarning = (
26+
deprecatedFnName: string,
27+
alternativeFnName: string
28+
) => {
29+
if (warned[deprecatedFnName]) {
30+
return;
31+
}
32+
console.warn(`Deprecation Warning:
33+
34+
"${deprecatedFnName}" is deprecated and will be removed in next major release. Please use "${alternativeFnName}" instead.
35+
36+
Docs: https://github.com/callstack/react-native-testing-library#${alternativeFnName.toLowerCase()}-type-reactcomponenttype
37+
`);
38+
39+
warned[deprecatedFnName] = true;
40+
};

src/helpers/getByAPI.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// @flow
22
import * as React from 'react';
33
import prettyFormat from 'pretty-format';
4-
import { ErrorWithStack, createLibraryNotSupportedError } from './errors';
4+
import {
5+
ErrorWithStack,
6+
createLibraryNotSupportedError,
7+
logDeprecationWarning,
8+
} from './errors';
59

610
const filterNodeByType = (node, type) => node.type === type;
711

@@ -28,9 +32,9 @@ const prepareErrorMessage = error =>
2832
// Strip info about custom predicate
2933
error.message.replace(/ matching custom predicate[^]*/gm, '');
3034

31-
// TODO: deprecate getByName(string | type) in favor of getByType(type)
3235
export const getByName = (instance: ReactTestInstance) =>
3336
function getByNameFn(name: string | React.ComponentType<*>) {
37+
logDeprecationWarning('getByName', 'getByType');
3438
try {
3539
return typeof name === 'string'
3640
? instance.find(node => filterNodeByName(node, name))
@@ -76,9 +80,9 @@ export const getByTestId = (instance: ReactTestInstance) =>
7680
}
7781
};
7882

79-
// TODO: deprecate getAllByName(string | type) in favor of getAllByType(type)
8083
export const getAllByName = (instance: ReactTestInstance) =>
8184
function getAllByNameFn(name: string | React.ComponentType<*>) {
85+
logDeprecationWarning('getAllByName', 'getAllByType');
8286
const results =
8387
typeof name === 'string'
8488
? instance.findAll(node => filterNodeByName(node, name))

src/helpers/queryByAPI.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import {
1111
getAllByText,
1212
getAllByProps,
1313
} from './getByAPI';
14+
import { logDeprecationWarning } from './errors';
1415

1516
export const queryByName = (instance: ReactTestInstance) => (
1617
name: string | React.ComponentType<*>
1718
) => {
19+
logDeprecationWarning('queryByName', 'getByName');
1820
try {
1921
return getByName(instance)(name);
2022
} catch (error) {
@@ -65,6 +67,7 @@ export const queryByTestId = (instance: ReactTestInstance) => (
6567
export const queryAllByName = (instance: ReactTestInstance) => (
6668
name: string | React.ComponentType<*>
6769
) => {
70+
logDeprecationWarning('queryAllByName', 'getAllByName');
6871
try {
6972
return getAllByName(instance)(name);
7073
} catch (error) {

0 commit comments

Comments
 (0)