@larsthorup/react-test-renderer
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

@larsthorup/react-test-renderer

A custom React renderer for faster testing of React components

Install

npm install @larsthorup/react-test-renderer

Use

Import:

import { TestRenderer } from '@larsthorup/react-test-renderer';

Render:

const renderer = await TestRenderer.create(<App />);

Find by text, type or props:

// return object if found
expect(renderer.findByType('div')).toBeDefined();

// return undefined if not found
expect(renderer.findByType('blink')).not.toBeDefined();

// find by text
expect(renderer.findByText('Save').type).toBe('button');

// find all
expect(renderer.findAllByProps({ type: 'checkbox' })).toHaveLength(5);

Inspect type and props:

expect(renderer.findByProps({ placeholder: 'title' }).type).toBe('input');
expect(renderer.findByProps({ placeholder: 'title' }).props.value).toBe('Swim');

Trigger event handlers:

import { change, click, submit } from "@larsthorup/react-test-renderer";
...
await click(renderer.findByText("Login"));
await change(renderer.findByProps({ placeholder: 'password' }), { target: { value: 'secret' } });
await submit(renderer.findByType('form'), { preventDefault: () => {} });

Publish

npm version minor
npm publish
git push

Package Sidebar

Install

npm i @larsthorup/react-test-renderer

Weekly Downloads

6

Version

1.0.0

License

MIT

Unpacked Size

19.5 kB

Total Files

15

Last publish

Collaborators

  • larsthorup