Skip to content

larsthorup/react-test-renderer

Repository files navigation

@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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published