diff --git a/docs/API.md b/docs/API.md index 53ba8dca3..b314684a7 100644 --- a/docs/API.md +++ b/docs/API.md @@ -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) @@ -98,6 +98,8 @@ A method returning an array of `ReactTestInstance`s with matching a React compon ### `update: (element: React.Element) => 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) diff --git a/src/__tests__/render.test.js b/src/__tests__/render.test.js index 211847acb..891d6a789 100644 --- a/src/__tests__/render.test.js +++ b/src/__tests__/render.test.js @@ -237,14 +237,15 @@ test('getAllByProp, queryAllByProps', () => { test('update', () => { const fn = jest.fn(); - const { getByName, update } = render(); + const { getByName, update, rerender } = render(); const button = getByName('Button'); button.props.onPress(); update(); + rerender(); - expect(fn).toHaveBeenCalledTimes(2); + expect(fn).toHaveBeenCalledTimes(3); }); test('unmount', () => { diff --git a/src/render.js b/src/render.js index b68690fca..8b1c0445d 100644 --- a/src/render.js +++ b/src/render.js @@ -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), diff --git a/typings/__tests__/index.test.tsx b/typings/__tests__/index.test.tsx index 37d084cbc..00da9207b 100644 --- a/typings/__tests__/index.test.tsx +++ b/typings/__tests__/index.test.tsx @@ -99,6 +99,7 @@ const debugFn = tree.debug(); const debugFnWithMessage = tree.debug('my message'); tree.update(); +tree.rerender(); tree.unmount(); // fireEvent API tests diff --git a/typings/index.d.ts b/typings/index.d.ts index 88a2b29e6..8838dd271 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -43,6 +43,7 @@ export interface RenderOptions { export interface RenderAPI extends GetByAPI, QueryByAPI { update(nextElement: React.ReactElement): void; + rerender(nextElement: React.ReactElement): void; unmount(nextElement?: React.ReactElement): void; toJSON(): ReactTestRendererJSON | null; debug(message?: string): void;