Skip to content

Feat(breaking): remove deprecated functions #334

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 21 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 1 addition & 2 deletions src/__tests__/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,7 @@ test('update', () => {
const fn = jest.fn();
const { getByText, update, rerender } = render(<Banana onUpdate={fn} />);

const button = getByText('Change freshness!');
fireEvent.press(button);
fireEvent.press(getByText('Change freshness!'));

update(<Banana onUpdate={fn} />);
rerender(<Banana onUpdate={fn} />);
Expand Down
24 changes: 4 additions & 20 deletions src/helpers/getByAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,51 +185,35 @@ export const getAllByTestId = (instance: ReactTestInstance) =>
return results;
};

export const UNSAFE_getByType = (
instance: ReactTestInstance,
warnFn?: Function
) =>
export const UNSAFE_getByType = (instance: ReactTestInstance) =>
function getByTypeFn(type: React.ComponentType<any>) {
warnFn && warnFn('getByType');
try {
return instance.findByType(type);
} catch (error) {
throw new ErrorWithStack(prepareErrorMessage(error), getByTypeFn);
}
};

export const UNSAFE_getByProps = (
instance: ReactTestInstance,
warnFn?: Function
) =>
export const UNSAFE_getByProps = (instance: ReactTestInstance) =>
function getByPropsFn(props: { [propName: string]: any }) {
warnFn && warnFn('getByProps');
try {
return instance.findByProps(props);
} catch (error) {
throw new ErrorWithStack(prepareErrorMessage(error), getByPropsFn);
}
};

export const UNSAFE_getAllByType = (
instance: ReactTestInstance,
warnFn?: Function
) =>
export const UNSAFE_getAllByType = (instance: ReactTestInstance) =>
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);
}
return results;
};

export const UNSAFE_getAllByProps = (
instance: ReactTestInstance,
warnFn?: Function
) =>
export const UNSAFE_getAllByProps = (instance: ReactTestInstance) =>
function getAllByPropsFn(props: { [propName: string]: any }) {
warnFn && warnFn('getAllByProps');
const results = instance.findAllByProps(props);
if (results.length === 0) {
throw new ErrorWithStack(
Expand Down
2 changes: 1 addition & 1 deletion src/shallow.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export function shallowInternal(
export default function shallow(_: ReactTestInstance | React.Element<any>) {
throwRemovedFunctionError(
'shallow',
'migration-v2#removed-global-debug-functions'
'migration-v2#removed-global-shallow-function'
);
}
39 changes: 31 additions & 8 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,29 @@ interface GetByAPI {
) => Array<ReactTestInstance>;

// Removed
/**
* @deprecated This functions has been removed. Please use other queries.
*/
getByName: (name: React.ReactType | string) => ReactTestInstance;
/**
* @deprecated This functions has been renamed to `UNSAFE_getByType`.
*/
getByType: <P>(type: React.ComponentType<P>) => ReactTestInstance;
/**
* @deprecated This functions has been renamed to `UNSAFE_getByProps`.
*/
getByProps: (props: Record<string, any>) => ReactTestInstance;
/**
* @deprecated This functions has been removed. Please use other queries.
*/
getAllByName: (name: React.ReactType | string) => Array<ReactTestInstance>;
/**
* @deprecated This functions has been renamed to `UNSAFE_getAllByType`.
*/
getAllByType: <P>(type: React.ComponentType<P>) => Array<ReactTestInstance>;
/**
* @deprecated This functions has been renamed to `UNSAFE_getAllByProps`.
*/
getAllByProps: (props: Record<string, any>) => Array<ReactTestInstance>;
}

Expand Down Expand Up @@ -77,11 +95,6 @@ interface QueryByAPI {
) => Array<ReactTestInstance> | [];
}

export interface WaitForOptions {
timeout: number;
interval: number;
}

interface FindByAPI {
findByText: (
text: string | RegExp,
Expand Down Expand Up @@ -245,12 +258,22 @@ export declare const render: (
component: React.ReactElement<any>,
options?: RenderOptions
) => RenderAPI;
export declare const shallow: <P = {}>(
instance: ReactTestInstance | React.ReactElement<P>
) => { output: React.ReactElement<P> };

export declare const flushMicrotasksQueue: () => Promise<any>;
export declare const cleanup: () => void;
export declare const fireEvent: FireEventAPI;
export declare const waitFor: WaitForFunction;
export declare const act: (callback: () => void) => Thenable;
export declare const within: (instance: ReactTestInstance) => Queries;

/**
* @deprecated This functions has been removed. Please use `waitFor` function.
*/
export declare const waitForElement: WaitForFunction;

/**
* @deprecated This functions has been removed.
*/
export declare const shallow: <P = {}>(
instance: ReactTestInstance | React.ReactElement<P>
) => { output: React.ReactElement<P> };
39 changes: 28 additions & 11 deletions website/docs/Migration20.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,26 @@ Both changes should improve code readibility.
Please note that in many cases `waitFor` call can be replaced by proper use of `findBy` asynchonous queries resulting in more streamlined test code.
:::

## Removed global debug functions
## Removed global `debug` function

Global `debug()` and `shallow()` functions have been removed in favor of `debug()` method returned from `render()` function.
Global `debug()` function has been removed in favor of `debug()` method returned from `render()` function.

## Removed global `shallow` function

Global `shallow()` functions which has been previously deprecated has been removed.

Shallow rendering React component is usually not a good idea, so we decided to remove the API. However, if you find it useful or need to support legacy tests, feel free to implement it yourself. Here's a sample implementation:

```js
import ShallowRenderer from 'react-test-renderer/shallow';

export function shallow(instance: ReactTestInstance | React.Element<any>) {
const renderer = new ShallowRenderer();
renderer.render(React.createElement(instance.type, instance.props));

return { output: renderer.getRenderOutput() };
}
```

## Removed functions

Expand All @@ -52,16 +69,16 @@ Following query functions have been removed after being deprecated for more than
- `queryByName`
- `queryAllByName`

Following query functions are still available but have been prefixed with `UNSAFE_`:
The `*ByType` and `*ByProps` queries has been prefixed with `UNSAFE_`. You can safely rename them using global search/replace in your project:

- `UNSAFE_getByType`
- `UNSAFE_getAllByType`
- `UNSAFE_queryByType`
- `UNSAFE_queryAllByType`
- `UNSAFE_getByProps`
- `UNSAFE_getAllByProps`
- `UNSAFE_queryByProps`
- `UNSAFE_queryAllByProps`
- `getByType` -> `UNSAFE_getByType`
- `getAllByType` -> `UNSAFE_getAllByType`
- `queryByType` -> `UNSAFE_queryByType`
- `queryAllByType` -> `UNSAFE_queryAllByType`
- `getByProps` -> `UNSAFE_getByProps`
- `getAllByProps` -> `UNSAFE_getAllByProps`
- `queryByProps` -> `UNSAFE_queryByProps`
- `queryAllByProps` -> `UNSAFE_queryAllByProps`

## Some `byTestId` queries behavior changes

Expand Down