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-unit

v1.0.0

Published

Recursive shallow rendering and helpers for efficiently testing React components

Downloads

21

Readme

React Test Unit

Warning Currently in active development, not quite ready for prime time. There WILL be major break changes.

A set of utilities to make testing stateless components simple, readable and fast.

Usage

Install via NPM. For use with React 0.14.x only.

npm install react-test-unit --save-dev

React Test Unit provides a renderComponent function which accepts a React component and returns a shallowly rendered output tree.

Under the hood this uses the ReactShallowRenderer and recursively renders the view tree.

import { renderComponent } from 'react-test-unit'

const props = {
  ...
}

const component = renderComponent(Component, props)

With the component rendered you can make assertions about the output. Notice there is no need to render to the DOM.

To make assertions you'll need to select elements. React Test Unit provides querySelector and querySelectorAll functions. Single classes and ids are also supported.

import { renderComponent, querySelector, querySelectorAll } from 'react-test-unit'

const props = {
  ...
}

const component = renderComponent(Component, props)
const divs = querySelectorAll(component, 'div')

assert.equal(2, divs.length)

Finally you'll can make sure handlers are correctly bound with the dispatchEvent function.

import sinon from 'sinon'
import { renderComponent, querySelector, dispatchEvent } from 'react-test-unit'

const spy = sinon.spy()

const props = {
  onClickHandler: spy
}

const component = renderComponent(Component, props)
const button = querySelector(component, 'button')

dispatchEvent(button, 'onClick')

assert.equal(true, spy.called)

Checkout the example usage here (deprecated).

Special mention must go to React Shallow Testutils and React Unit for a lot of inspiration and concepts.

API

renderComponent

/**
* Recursively runs a shallow render on a React Component class.
*
* @method renderComponent
* @param {Function}(required) component A React Component
* @param {Object}(optional) props A props object to instantiate the component with
* @param {Object}(optional) context A context object to instantiate each component in the tree with
* @return {Object} Returns a fully formed render tree
*/

querySelectorAll

/**
* Searches a render tree for either class or tag.
*
* @method querySelectorAll
* @param {Object}(required) tree A rendered component tree
* @param {String}(required) selector Either a class ('.class') or tag ('div') to search for
* @return {Array} Returns an array of the found objects in the component tree
*/

querySelector

/**
* Searches a render tree for either class or tag and returns the first result.
*
* @method querySelector
* @param {Object}(required) tree A rendered component tree
* @param {String}(required) selector Either a class ('.class') or tag ('div') to search for
* @return {Object|Undefined} Returns either undefined or the found object in the component tree
*/

dispatchEvent

/**
* Calls the handler function for a given event
*
* @method dispatchEvent
* @param {Object}(required) tree A rendered component tree to call the handler on
* @param {String}(required) handler The handler to call
* @param {Args}(optional) arguments Any number of arguments to pass to the handler
* @return {?|False} Returns either the result of the handler or false if no handler exist
*/