Skip to content

feat: add rerender as an alias for update #149

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 3 commits into from
Apr 14, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: API

This page gathers public API of `react-native-testing-library` along with usage examples.

## `render`
## `render`

- [`Example code`](https://github.com/callstack/react-native-testing-library/blob/master/src/__tests__/render.test.js)

Expand Down Expand Up @@ -98,6 +98,8 @@ A method returning an array of `ReactTestInstance`s with matching a React compon

### `update: (element: React.Element<any>) => void`

_Also available under `rerender` alias_

Re-render the in-memory tree with a new root element. This simulates a React update at the root. If the new element has the same type and key as the previous element, the tree will be updated; otherwise, it will re-mount a new tree. This is useful when testing for `componentDidUpdate` behavior, by passing updated props to the component.

[Example code](https://github.com/callstack/react-native-testing-library/blob/f96d782d26dd4815dbfd01de6ef7a647efd1f693/src/__tests__/act.test.js#L31-L37)
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,15 @@ test('getAllByProp, queryAllByProps', () => {

test('update', () => {
const fn = jest.fn();
const { getByName, update } = render(<Banana onUpdate={fn} />);
const { getByName, update, rerender } = render(<Banana onUpdate={fn} />);
const button = getByName('Button');

button.props.onPress();

update(<Banana onUpdate={fn} />);
rerender(<Banana onUpdate={fn} />);

expect(fn).toHaveBeenCalledTimes(2);
expect(fn).toHaveBeenCalledTimes(3);
});

test('unmount', () => {
Expand Down
1 change: 1 addition & 0 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default function render(
...getByAPI(instance),
...queryByAPI(instance),
update: updateWithAct(renderer),
rerender: updateWithAct(renderer), // alias for `update`
unmount: renderer.unmount,
toJSON: renderer.toJSON,
debug: debug(instance, renderer),
Expand Down
1 change: 1 addition & 0 deletions typings/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const debugFn = tree.debug();
const debugFnWithMessage = tree.debug('my message');

tree.update(<View />);
tree.rerender(<View />);
tree.unmount();

// fireEvent API tests
Expand Down
1 change: 1 addition & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface RenderOptions {

export interface RenderAPI extends GetByAPI, QueryByAPI {
update(nextElement: React.ReactElement<any>): void;
rerender(nextElement: React.ReactElement<any>): void;
unmount(nextElement?: React.ReactElement<any>): void;
toJSON(): ReactTestRendererJSON | null;
debug(message?: string): void;
Expand Down