npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-test-engine

v1.1.1

Published

Unit test utils for react components

Downloads

14

Readme

react-test-engine

NPM dependencies status

Unit test utils for react components

Api reference

Examples

Installation

npm install react-test-engine react-test-renderer react-is --save-dev

or

yarn add react-test-engine react-test-renderer react-is --dev

Quickstart

Let's test a component. I'm using vitest, but you can use your favourite test framework

import type {
	ReactElement,
} from "react";

type Props = {
	callback: (foo: number, bar: string) => void;
	children?: string;
};

function Component({
	callback,
	children = undefined,
}: Props): ReactElement | null {
	const onClick = useCallback(() => {
		callback(1, "2");
	}, [callback]);

	return (
		<div className="my-wrapper">
			<button className="my-button" type="button" onClick={onClick}>
				{children}
			</button>
		</div>
	);
}

At first, we have to define stubs for required props of the component

import { vi } from "vitest";

const defaultProps: Props = {
	callback: vi.fn(),
};

Then let's describe accsessors of rendered components. In this case, only button is needed. Let's call it "targetButton"

import { create } from "react-test-engine";

const render = create(Component, defaultProps, {
	queries: {
		targetButton: {
			component: "button",
			className: "my-button",
		},
	},
});

A boilerplate is ready. Let's write a test that checks for the correct render of the children

import { expect, test } from "vitest";

test("should render children correctly", () => {
	const engine = render({
		children: "Children for our test",
	});

	expect(engine.accessors.targetButton.getProps().children).toBe("Children for our test");
});

A method getProps is used here, but you can use other methods. The full list:

  • get - returns an element or throw an error if element is not found or there are more than one element for curreny query;
  • getProps - returns props of the element or throw an error if element is not found or there are more than one element for curreny query;
  • getAll - returns array of all matched elements or throw an error if there are no elements for curreny query;
  • query - returns an element or null if element is not found or throw an error if there are no matched elements for curreny query;
  • queryAll - returns array of all matched elements.

react-shallow-search is used.

engine.accessors.targetButton.getProps().children is too long. We can simplify it:

import { create } from "react-test-engine";

const render = create(Component, defaultProps, {
	queries: {
		targetButton: {
			component: "button",
			className: "my-button",
		},
	},
	// !!!!!!!!!!!!!!!
	// ADDED `properties` SECTION
	properties: {
		targetChildren: ["targetButton", "children"],
	},
});

Then change a test:

import { expect, test } from "vitest";

test("should render children correctly", () => {
	const engine = render({
		children: "Children for our test",
	});

	expect(engine.getProperty("targetChildren")).toBe("Children for our test");
});

Then let's test a callback. We can get it by props and check if this defined by ourselves, but there's an easy way. Let's change definition a little

import { create } from "react-test-engine";

const render = create(Component, defaultProps, {
	queries: {
		targetButton: {
			component: "button",
			className: "my-button",
		},
	},
	properties: {
		targetChildren: ["targetButton", "children"],
	},
	// !!!!!!!!!!!!!!!
	// ADDED `callbacks` SECTION
	callbacks: {
		onClickTarget: ["targetButton", "onClick"],
	},
});

The first value of the tupple is the key of queries. The second value is the key of props

Let's write a test for the callback:

import type { MouseEvent } from "react";
import { expect, test, vi } from "vitest";

test("should call callback correctly", () => {
	const callback = vi.fn();

	const engine = render({
		callback,
	});

	const event = {};

	engine.getCallback("onClickTarget")(
		event as MouseEvent<HTMLButtonElement>,
	);

	expect(callback).toHaveBeenCalledTimes(1);
	expect(callback).toHaveBeenCalledWith(1, "2");
});