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 🙏

© 2025 – Pkg Stats / Ryan Hefner

expanded-react-test-utils

v1.0.2

Published

Additional unit testing utilities for unit testing React applications

Downloads

19

Readme

Expanded ReactJS Test Utilities

Additional functions beyond the existing ReactJS test utilities to make testing quicker and easier. Adds the following functionality:

  • Find a component based on CSS selector instead of just tag name or class. Supports classes, IDs, tag names, pseudo selectors, and attribute selectors.
  • Fully mock child components yet still provide the ability to verify props passed to children
  • Easily render components that rely on react-router
  • ...plus others

Setup

npm install --save-dev expanded-react-test-utils

From within your unit test files:

var ExpandedReactTestUtils = require('expanded-react-test-utils');

Testing Methods

mockReactComponent

//Mock single component
JasmineSpy mockReactComponent(ReactComponent component, object additionalProps)
//Mock multiple components
JasmineSpy mockReactComponent(object mocks)

Requires global Jasmine include for spies

Fully mocks a component given it's name and replaces it with an empty component of the same name at render time. Also allows you to append any additional props to the component which will overwrite values passed to child. This method is ideally called once before all component unit tests are run.

Once mocked, use the findRenderedDOMComponentWithSelector to find instances of the mocked component in the rendered tree. You can then use the results of that function to assert that the props you pass to child components are correct.

Example
beforeAll(function(){
    //Mock out a the 'Item' React component, and add the provided className to all found instances
    ExpandedReactTestUtils.mockReactComponent('Item');
    //Can also be written as:
    ExpandedReactTestUtils.mockReactComponent({
        Item: {className: 'mocked-item-class'}
    });
});

beforeEach(function(){
    itemList = ReactTestUtils.renderIntoDocument(<ItemList />);
    //All instances of <Item/> within the <ItemList /> component 
    //will now be replaced by empty <div> elements, but will continue 
    //to keep the same props 
});

describe('item tests', function(){
    it('renders correct number of Item components', function(){
        var items = ExpandedReactTestUtils.findRenderedDOMComponentWithSelector(
            itemList, 
            'ItemList'
        );
        expect(items.length).toEqual(3);
    });
});

getRouterComponent

ReactComponent getRouterComponent(Router, ReactComponent component, object props, string path)

Similar to the existing renderIntoDocument method, but wraps component within a mock Router so all router mixins and functionality work properly. Must provide instance of react-router Router component to mock.

Example
beforeEach(function(){
    itemList = ExpandedReactTestUtils.getRouterComponent(ItemList, {count: 3}, 'results');
    //Render the <ItemList/> component into the DOM, but wrap it in a 
    //mocked router. The path provided will be the route to be matched.
});

renderPartial

ReactComponent renderPartial(ReactElement partial)

With the changes introduced in React 0.14, you can no longer pass in a ReactElement into TestUtils.renderIntoDocument. Instead a React Component must be passed. This method wraps your ReactElement in a mock component and returns the rendered result.

Example
it('renders proper markup', function(){
    var result = ExpandedReactTestUtils.renderPartial(<div className="test"></div>);
    expect(ExpandedReactTestUtils.findComponentCountWithClassname(result, 'test')).toBeTrue();
});

scryRenderedDOMComponentsWithSelector

array scryRenderedDOMComponentsWithSelector(ReactComponent tree, string selector)

Find all instances of components in the provided tree that match the provided CSS selector. Read the CSS Selector Syntax Support page for details on what types of selectors are supported.

Example
it('contains proper icon classes', function(){
    //Get the list of all elements matching the selector
    var icons = ExpandedReactTestUtils.scryRenderedDOMComponentsWithSelector(
        itemList, 
        'span.user-item'
    );

    expect(icons.length).toEqual(3);
    expect(icons[0].props.title).toEqual('Failed request');
});

findRenderedDOMComponentWithSelector

ReactComponent findRenderedDOMComponentWithSelector(ReactComponent tree, string selector)

Find a single component in the provided tree that matches the provided CSS selector. Will throw an error if zero or more than 1 component is found. Read the CSS Selector Syntax Support page for details on what types of selectors are supported.

Example
it('contains proper icon classes', function(){
    //Find the correct submit button via selector and simulate a click event
    var submitButton = ExpandedReactTestUtils.findRenderedDOMComponentWithSelector(
        itemList, 
        '.submit-button'
    );

    ReactTestUtils.Simulate.click(submitButton);
});

findComponentCountWithClassname

bool findComponentCountWithClassname(ReactComponent tree, string className, int count=1)

Used to ensure that the correct number of elements with the provided class name are present in the provided tree. Provides a quick way to ensure that the right number of elements are present. The count defaults to 1 if not provided.

Example
it('contains proper icon classes', function(){
    //Ensure that this tree contains 3 elements with fa-user class
    expect(ExpandedReactTestUtils.findComponentCountWithClassname(
        itemList, 
        'fa-user', 
        3
    )).toEqual(true);
});

findComponentCountWithTag

bool findComponentCountWithTag(ReactComponent tree, string tagName, int count=1)

Used to ensure that the correct number of elements with the provided tag name are present in the provided tree. Provides a quick way to ensure that the right number of elements with a tag are present. The count defaults to 1 if not provided.

Example

it('contains correct number of span tags', function(){
    //Assert that there are no failure elements in the tree
    expect(ExpandedReactTestUtils.findComponentCountWithTag(
        itemList, 
        'span', 
        3
    )).toEqual(true);
});

findComponentCountWithSelector

bool findComponentCountWithSelector(ReactComponent tree, string selector, int count=1)

Used to ensure that the correct number of elements with the provided CSS selector are present in the provided tree. Provides a quick way to ensure that the right number of elements are present. The count defaults to 1 if not provided. Read the CSS Selector Syntax Support page for details on what types of selectors are supported.

Example
it('contains proper icon classes', function(){
    //Assert that there are no failure elements in the tree
    expect(ExpandedReactTestUtils.findComponentCountWithSelector(
        itemList, 
        'span.failure', 
        0
    )).toEqual(true);
});

License

MIT