Skip to content

Commit 960080d

Browse files
authored
imp: Re-alias deprecated matchers and warn (#261)
* imp: Re-alias deprecated matchers * Prepare for byName deletion
1 parent 62cda22 commit 960080d

File tree

4 files changed

+117
-56
lines changed

4 files changed

+117
-56
lines changed

src/helpers/errors.js

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,57 @@ export const createLibraryNotSupportedError = (error: Error) =>
1313
`Currently the only supported library to search by text is "react-native".\n\n${error.message}`
1414
);
1515

16+
export const prepareErrorMessage = (error: Error) =>
17+
// Strip info about custom predicate
18+
error.message.replace(/ matching custom predicate[^]*/gm, '');
19+
20+
export const createQueryByError = (error: Error, callsite: Function) => {
21+
if (error.message.includes('No instances found')) {
22+
return null;
23+
}
24+
throw new ErrorWithStack(error.message, callsite);
25+
};
26+
1627
const warned = {
1728
getByName: false,
1829
getAllByName: false,
1930
queryByName: false,
2031
queryAllByName: false,
32+
33+
getByProps: false,
34+
getAllByProps: false,
35+
queryByProps: false,
36+
queryAllByProps: false,
37+
38+
getByType: false,
39+
getAllByType: false,
40+
queryByType: false,
41+
queryAllByType: false,
2142
};
2243

23-
export const logDeprecationWarning = (
24-
deprecatedFnName: string,
25-
alternativeFnName: string
26-
) => {
27-
if (warned[deprecatedFnName]) {
44+
export function printDeprecationWarning(functionName: string) {
45+
if (warned[functionName]) {
2846
return;
2947
}
30-
console.warn(`Deprecation Warning:
3148

32-
"${deprecatedFnName}" is deprecated and will be removed in next major release. Please use "${alternativeFnName}" instead.
49+
console.warn(`
50+
Deprecation Warning:
51+
${functionName} is not recommended for use and will be deleted in react-native-testing-library 2.x.
52+
`);
3353

34-
Docs: https://github.com/callstack/react-native-testing-library#${alternativeFnName.toLowerCase()}-type-reactcomponenttype
35-
`);
54+
warned[functionName] = true;
55+
}
3656

37-
warned[deprecatedFnName] = true;
38-
};
57+
export function printUnsafeWarning(functionName: string) {
58+
if (warned[functionName]) {
59+
return;
60+
}
3961

40-
export const prepareErrorMessage = (error: Error) =>
41-
// Strip info about custom predicate
42-
error.message.replace(/ matching custom predicate[^]*/gm, '');
62+
console.warn(`
63+
Deprecation Warning:
64+
${functionName} is not recommended for use and has been renamed to UNSAFE_${functionName}.
65+
In react-native-testing-library 2.x only the UNSAFE_${functionName} name will work.
66+
`);
4367

44-
export const createQueryByError = (error: Error, callsite: Function) => {
45-
if (error.message.includes('No instances found')) {
46-
return null;
47-
}
48-
throw new ErrorWithStack(error.message, callsite);
49-
};
68+
warned[functionName] = true;
69+
}

src/helpers/getByAPI.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import prettyFormat from 'pretty-format';
44
import {
55
ErrorWithStack,
66
createLibraryNotSupportedError,
7-
logDeprecationWarning,
87
prepareErrorMessage,
8+
printDeprecationWarning,
9+
printUnsafeWarning,
910
} from './errors';
1011

1112
const filterNodeByType = (node, type) => node.type === type;
@@ -68,9 +69,9 @@ const getTextInputNodeByDisplayValue = (node, value) => {
6869
}
6970
};
7071

71-
export const getByName = (instance: ReactTestInstance) =>
72+
export const getByName = (instance: ReactTestInstance, warnFn?: Function) =>
7273
function getByNameFn(name: string | React.ComponentType<any>) {
73-
logDeprecationWarning('getByName', 'getByType');
74+
warnFn && warnFn('getByName');
7475
try {
7576
return typeof name === 'string'
7677
? instance.find(node => filterNodeByName(node, name))
@@ -80,8 +81,9 @@ export const getByName = (instance: ReactTestInstance) =>
8081
}
8182
};
8283

83-
export const getByType = (instance: ReactTestInstance) =>
84+
export const getByType = (instance: ReactTestInstance, warnFn?: Function) =>
8485
function getByTypeFn(type: React.ComponentType<any>) {
86+
warnFn && warnFn('getByType');
8587
try {
8688
return instance.findByType(type);
8789
} catch (error) {
@@ -120,8 +122,9 @@ export const getByDisplayValue = (instance: ReactTestInstance) =>
120122
}
121123
};
122124

123-
export const getByProps = (instance: ReactTestInstance) =>
125+
export const getByProps = (instance: ReactTestInstance, warnFn?: Function) =>
124126
function getByPropsFn(props: { [propName: string]: any }) {
127+
warnFn && warnFn('getByProps');
125128
try {
126129
return instance.findByProps(props);
127130
} catch (error) {
@@ -138,9 +141,9 @@ export const getByTestId = (instance: ReactTestInstance) =>
138141
}
139142
};
140143

141-
export const getAllByName = (instance: ReactTestInstance) =>
144+
export const getAllByName = (instance: ReactTestInstance, warnFn?: Function) =>
142145
function getAllByNameFn(name: string | React.ComponentType<any>) {
143-
logDeprecationWarning('getAllByName', 'getAllByType');
146+
warnFn && warnFn('getAllByName');
144147
const results =
145148
typeof name === 'string'
146149
? instance.findAll(node => filterNodeByName(node, name))
@@ -151,8 +154,9 @@ export const getAllByName = (instance: ReactTestInstance) =>
151154
return results;
152155
};
153156

154-
export const getAllByType = (instance: ReactTestInstance) =>
157+
export const getAllByType = (instance: ReactTestInstance, warnFn?: Function) =>
155158
function getAllByTypeFn(type: React.ComponentType<any>) {
159+
warnFn && warnFn('getAllByType');
156160
const results = instance.findAllByType(type);
157161
if (results.length === 0) {
158162
throw new ErrorWithStack('No instances found', getAllByTypeFn);
@@ -200,8 +204,9 @@ export const getAllByDisplayValue = (instance: ReactTestInstance) =>
200204
return results;
201205
};
202206

203-
export const getAllByProps = (instance: ReactTestInstance) =>
207+
export const getAllByProps = (instance: ReactTestInstance, warnFn?: Function) =>
204208
function getAllByPropsFn(props: { [propName: string]: any }) {
209+
warnFn && warnFn('getAllByProps');
205210
const results = instance.findAllByProps(props);
206211
if (results.length === 0) {
207212
throw new ErrorWithStack(
@@ -229,17 +234,23 @@ export const getAllByTestId = (instance: ReactTestInstance) =>
229234

230235
export const getByAPI = (instance: ReactTestInstance) => ({
231236
getByTestId: getByTestId(instance),
232-
getByName: getByName(instance),
233-
getByType: getByType(instance),
237+
getByName: getByName(instance, printDeprecationWarning),
238+
getByType: getByType(instance, printUnsafeWarning),
234239
getByText: getByText(instance),
235240
getByPlaceholder: getByPlaceholder(instance),
236241
getByDisplayValue: getByDisplayValue(instance),
237-
getByProps: getByProps(instance),
242+
getByProps: getByProps(instance, printUnsafeWarning),
238243
getAllByTestId: getAllByTestId(instance),
239-
getAllByName: getAllByName(instance),
240-
getAllByType: getAllByType(instance),
244+
getAllByName: getAllByName(instance, printDeprecationWarning),
245+
getAllByType: getAllByType(instance, printUnsafeWarning),
241246
getAllByText: getAllByText(instance),
242247
getAllByPlaceholder: getAllByPlaceholder(instance),
243248
getAllByDisplayValue: getAllByDisplayValue(instance),
244-
getAllByProps: getAllByProps(instance),
249+
getAllByProps: getAllByProps(instance, printUnsafeWarning),
250+
251+
// Unsafe aliases
252+
UNSAFE_getByType: getByType(instance),
253+
UNSAFE_getAllByType: getAllByType(instance),
254+
UNSAFE_getByProps: getByProps(instance),
255+
UNSAFE_getAllByProps: getAllByProps(instance),
245256
});

src/helpers/queryByAPI.js

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,25 @@ import {
1616
getAllByDisplayValue,
1717
getAllByProps,
1818
} from './getByAPI';
19-
import { logDeprecationWarning, createQueryByError } from './errors';
19+
import {
20+
createQueryByError,
21+
printDeprecationWarning,
22+
printUnsafeWarning,
23+
} from './errors';
2024

21-
export const queryByName = (instance: ReactTestInstance) =>
25+
export const queryByName = (instance: ReactTestInstance, warnFn?: Function) =>
2226
function queryByNameFn(name: string | React.ComponentType<any>) {
23-
logDeprecationWarning('queryByName', 'getByName');
27+
warnFn && warnFn('queryByName');
2428
try {
2529
return getByName(instance)(name);
2630
} catch (error) {
2731
return createQueryByError(error, queryByNameFn);
2832
}
2933
};
3034

31-
export const queryByType = (instance: ReactTestInstance) =>
35+
export const queryByType = (instance: ReactTestInstance, warnFn?: Function) =>
3236
function queryByTypeFn(type: React.ComponentType<any>) {
37+
warnFn && warnFn('queryByType');
3338
try {
3439
return getByType(instance)(type);
3540
} catch (error) {
@@ -64,8 +69,9 @@ export const queryByDisplayValue = (instance: ReactTestInstance) =>
6469
}
6570
};
6671

67-
export const queryByProps = (instance: ReactTestInstance) =>
72+
export const queryByProps = (instance: ReactTestInstance, warnFn?: Function) =>
6873
function queryByPropsFn(props: { [propName: string]: any }) {
74+
warnFn && warnFn('queryByProps');
6975
try {
7076
return getByProps(instance)(props);
7177
} catch (error) {
@@ -82,20 +88,23 @@ export const queryByTestId = (instance: ReactTestInstance) =>
8288
}
8389
};
8490

85-
export const queryAllByName = (instance: ReactTestInstance) => (
86-
name: string | React.ComponentType<any>
87-
) => {
88-
logDeprecationWarning('queryAllByName', 'getAllByName');
91+
export const queryAllByName = (
92+
instance: ReactTestInstance,
93+
warnFn?: Function
94+
) => (name: string | React.ComponentType<any>) => {
95+
warnFn && warnFn('queryAllByName');
8996
try {
9097
return getAllByName(instance)(name);
9198
} catch (error) {
9299
return [];
93100
}
94101
};
95102

96-
export const queryAllByType = (instance: ReactTestInstance) => (
97-
type: React.ComponentType<any>
98-
) => {
103+
export const queryAllByType = (
104+
instance: ReactTestInstance,
105+
warnFn?: Function
106+
) => (type: React.ComponentType<any>) => {
107+
warnFn && warnFn('queryAllByType');
99108
try {
100109
return getAllByType(instance)(type);
101110
} catch (error) {
@@ -133,9 +142,11 @@ export const queryAllByDisplayValue = (instance: ReactTestInstance) => (
133142
}
134143
};
135144

136-
export const queryAllByProps = (instance: ReactTestInstance) => (props: {
137-
[propName: string]: any,
138-
}) => {
145+
export const queryAllByProps = (
146+
instance: ReactTestInstance,
147+
warnFn?: Function
148+
) => (props: { [propName: string]: any }) => {
149+
warnFn && warnFn('queryAllByProps');
139150
try {
140151
return getAllByProps(instance)(props);
141152
} catch (error) {
@@ -155,17 +166,23 @@ export const queryAllByTestId = (instance: ReactTestInstance) => (
155166

156167
export const queryByAPI = (instance: ReactTestInstance) => ({
157168
queryByTestId: queryByTestId(instance),
158-
queryByName: queryByName(instance),
159-
queryByType: queryByType(instance),
169+
queryByName: queryByName(instance, printDeprecationWarning),
170+
queryByType: queryByType(instance, printUnsafeWarning),
160171
queryByText: queryByText(instance),
161172
queryByPlaceholder: queryByPlaceholder(instance),
162173
queryByDisplayValue: queryByDisplayValue(instance),
163-
queryByProps: queryByProps(instance),
174+
queryByProps: queryByProps(instance, printUnsafeWarning),
164175
queryAllByTestId: queryAllByTestId(instance),
165-
queryAllByName: queryAllByName(instance),
166-
queryAllByType: queryAllByType(instance),
176+
queryAllByName: queryAllByName(instance, printDeprecationWarning),
177+
queryAllByType: queryAllByType(instance, printUnsafeWarning),
167178
queryAllByText: queryAllByText(instance),
168179
queryAllByPlaceholder: queryAllByPlaceholder(instance),
169180
queryAllByDisplayValue: queryAllByDisplayValue(instance),
170-
queryAllByProps: queryAllByProps(instance),
181+
queryAllByProps: queryAllByProps(instance, printUnsafeWarning),
182+
183+
// Unsafe aliases
184+
UNSAFE_queryByType: queryByType(instance),
185+
UNSAFE_queryAllByType: queryAllByType(instance),
186+
UNSAFE_queryByProps: queryByProps(instance),
187+
UNSAFE_queryAllByProps: queryAllByProps(instance),
171188
});

typings/index.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ export interface GetByAPI {
1919
) => Array<ReactTestInstance>;
2020
getAllByDisplayValue: (value: string | RegExp) => Array<ReactTestInstance>;
2121
getAllByProps: (props: Record<string, any>) => Array<ReactTestInstance>;
22+
23+
24+
// Unsafe aliases
25+
UNSAFE_getByType: <P>(type: React.ComponentType<P>) => ReactTestInstance,
26+
UNSAFE_getAllByType: <P>(type: React.ComponentType<P>) => Array<ReactTestInstance>,
27+
UNSAFE_getByProps: (props: Record<string, any>) => ReactTestInstance,
28+
UNSAFE_getAllByProps: (props: Record<string, any>) => Array<ReactTestInstance>,
2229
}
2330

2431
export interface QueryByAPI {
@@ -48,6 +55,12 @@ export interface QueryByAPI {
4855
queryAllByProps: (
4956
props: Record<string, any>
5057
) => Array<ReactTestInstance> | [];
58+
59+
// Unsafe aliases
60+
UNSAFE_queryByType: <P>(type: React.ComponentType<P>) => ReactTestInstance | null,
61+
UNSAFE_queryAllByType: <P>(type: React.ComponentType<P>) => Array<ReactTestInstance> | [],
62+
UNSAFE_queryByProps: (props: Record<string, any>) => ReactTestInstance | null,
63+
UNSAFE_queryAllByProps: (props: Record<string, any>) => Array<ReactTestInstance> | [],
5164
}
5265

5366
type GetReturn = ReactTestInstance;

0 commit comments

Comments
 (0)