From 439db6c8235d753475a7bc354fcf43b2d39da592 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 14 Apr 2019 10:09:46 +0200 Subject: [PATCH] feat: add `rerender` as an alias for `update` (#149) ### Summary Users of `react-testing-library` use `rerender` wording instead of `update`. Let's make it easier for them by aliasing it. See: https://testing-library.com/docs/react-testing-library/api#rerender ### Test plan Added cases for JS tests and TS typings --- docs/API.md | 4 +++- src/__tests__/render.test.js | 5 +++-- src/render.js | 1 + typings/__tests__/index.test.tsx | 1 + typings/index.d.ts | 1 + 5 files changed, 9 insertions(+), 3 deletions(-) diff --git a/docs/API.md b/docs/API.md index 53ba8dca..b314684a 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 211847ac..891d6a78 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 b68690fc..8b1c0445 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 37d084cb..00da9207 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 88a2b29e..8838dd27 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;