From 8d8b62b780f34dbf9d9644f449136906b65a4991 Mon Sep 17 00:00:00 2001 From: Maciej Jastrzebski Date: Tue, 19 May 2020 14:10:24 +0200 Subject: [PATCH 1/4] Adapt docs changes from `prepare-2` branch --- website/docs/Queries.md | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/website/docs/Queries.md b/website/docs/Queries.md index a4b8992a9..1e3554291 100644 --- a/website/docs/Queries.md +++ b/website/docs/Queries.md @@ -203,23 +203,26 @@ const element = getByA11yValue({ min: 40 }); The interface is the same as for other queries, but we won't provide full names so that they're harder to find by search engines. -### `UNSAFE_ByType`, `ByType` +### `UNSAFE_ByType` -> Note: added in v1.4 +> UNSAFE_getByType, UNSAFE_getAllByType, UNSAFE_queryByType, UNSAFE_queryAllByType -> This method has been **deprecated** and has been prepended with `UNSAFE_` prefix. In react-native-testing-library 2.x only the prefixed version will work. +Returns a `ReactTestInstance` with matching a React component type. -A method returning a `ReactTestInstance` with matching a React component type. Throws when no matches. +:::caution +This method has been marked unsafe, since it requires knowledge about implementation details of the component. Use responsibly. +::: -### `UNSAFE_ByProps`, `ByProps` +### `UNSAFE_ByProps` -> This method has been **deprecated** and has been prepended with `UNSAFE_` prefix. In react-native-testing-library 2.x only the prefixed version will work. +> UNSAFE_getByProps, UNSAFE_getAllByProps, UNSAFE_queryByProps, UNSAFE_queryAllByProps -A method returning a `ReactTestInstance` with matching props object +Returns a `ReactTestInstance` with matching props object. -### `ByName` +:::caution +This method has been marked unsafe, since it requires knowledge about implementation details of the component. Use responsibly. +::: -> This method has been **deprecated** because using it results in fragile tests that may break between minor React Native versions. **DON'T USE IT**. It will be removed in next major release (v2.0). Use the other alternatives, such as [`getByText`](#bytext) instead. It's listed here only for back-compat purposes for early adopters of the library -> A method returning a `ReactTestInstance` with matching a React component type. Throws when no matches. +### `ByName` From d24313597287d66953f5dd74f09fcd33178db7e4 Mon Sep 17 00:00:00 2001 From: Maciej Jastrzebski Date: Tue, 19 May 2020 14:14:17 +0200 Subject: [PATCH 2/4] WIP --- src/helpers/errors.js | 7 ++++--- website/docs/Queries.md | 2 -- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/helpers/errors.js b/src/helpers/errors.js index edf108cbc..ada5a2269 100644 --- a/src/helpers/errors.js +++ b/src/helpers/errors.js @@ -60,9 +60,10 @@ export function printUnsafeWarning(functionName: string) { } 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. + Deprecation Warning: + Warning: + ${functionName} promotes testing implementation details and is not recommended. + It may be removed in the future. Please test observable outcomes of rendering components. `); warned[functionName] = true; diff --git a/website/docs/Queries.md b/website/docs/Queries.md index 1e3554291..a0c0fd705 100644 --- a/website/docs/Queries.md +++ b/website/docs/Queries.md @@ -223,6 +223,4 @@ Returns a `ReactTestInstance` with matching props object. This method has been marked unsafe, since it requires knowledge about implementation details of the component. Use responsibly. ::: -### `ByName` - From bcda975e46bfd26bb74a013c6b48d52aead8a0e3 Mon Sep 17 00:00:00 2001 From: Maciej Jastrzebski Date: Tue, 19 May 2020 16:20:05 +0200 Subject: [PATCH 3/4] Removed unused `printUnsafeWarning` function # Conflicts: # src/helpers/errors.js --- src/helpers/errors.js | 37 ++++++++----------------------------- 1 file changed, 8 insertions(+), 29 deletions(-) diff --git a/src/helpers/errors.js b/src/helpers/errors.js index ada5a2269..3a030ac3d 100644 --- a/src/helpers/errors.js +++ b/src/helpers/errors.js @@ -24,22 +24,7 @@ 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, -}; +const warned = {}; export function printDeprecationWarning(functionName: string) { if (warned[functionName]) { @@ -54,17 +39,11 @@ export function printDeprecationWarning(functionName: string) { warned[functionName] = true; } -export function printUnsafeWarning(functionName: string) { - if (warned[functionName]) { - return; - } - - console.warn(` - Deprecation Warning: - Warning: - ${functionName} promotes testing implementation details and is not recommended. - It may be removed in the future. Please test observable outcomes of rendering components. - `); - - warned[functionName] = true; +export function throwRemovedFunctionError( + functionName: string, + docsRef: string +) { + throw new Error( + `${functionName} has been removed in version 2.0.\n\nPlease consult: https://callstack.github.io/react-native-testing-library/docs/${docsRef}` + ); } From b7fd8db2b18d4f936f8b6a586c838b3eec78ae00 Mon Sep 17 00:00:00 2001 From: Maciej Jastrzebski Date: Tue, 19 May 2020 16:21:45 +0200 Subject: [PATCH 4/4] Code review changes --- src/helpers/getByAPI.js | 1 - src/helpers/queryByAPI.js | 6 +----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/helpers/getByAPI.js b/src/helpers/getByAPI.js index cc6079607..c937da0c9 100644 --- a/src/helpers/getByAPI.js +++ b/src/helpers/getByAPI.js @@ -6,7 +6,6 @@ import { createLibraryNotSupportedError, prepareErrorMessage, printDeprecationWarning, - printUnsafeWarning, } from './errors'; const filterNodeByType = (node, type) => node.type === type; diff --git a/src/helpers/queryByAPI.js b/src/helpers/queryByAPI.js index e2c06b0a0..c16bc822f 100644 --- a/src/helpers/queryByAPI.js +++ b/src/helpers/queryByAPI.js @@ -16,11 +16,7 @@ import { getAllByDisplayValue, getAllByProps, } from './getByAPI'; -import { - createQueryByError, - printDeprecationWarning, - printUnsafeWarning, -} from './errors'; +import { createQueryByError, printDeprecationWarning } from './errors'; export const queryByName = (instance: ReactTestInstance, warnFn?: Function) => function queryByNameFn(name: string | React.ComponentType) {