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

react-container-component

v1.0.3

Published

React containers components for easy unit testing

Readme

react-container-component

React containers components for easy unit testing

Why should I use this?

  • It enforces good design patterns:
    • Container components for the logic and presentational components for the rendering
    • Reusable Components (by promoting the use of propTypes)
  • It makes unit testing your container components easier by facilitating test doubles
  • It makes the unit tests of your containers run faster because besides "mocking" the container's dependencies, it doesn't render the container's child components (and children of the children, and so on) even if you mount it.

How to install

npm install react-container-component --save

How to use it

1 . Your container components should extend react-container-component Container instead of React Component:

import { Container } from 'react-container-component';

class PhotosContainer extends Container {

2 . Your presentational component should specify all the properties it needs by setting its propTypes (well you should do it either you use react-container-component Container or not, propTypes are a good practice :). Example:

const Photos = (props) => <h1>He have {props.photos.length} photos</h1>
//The container will automatically pass the following props to the presentational component
Photos.propTypes = {
  photos: React.PropTypes.string
}

3 . The constructor of your container should call this.setComponent(Photos), passing the presentational component we want to render. Example:

class PhotosContainer extends Container {
  constructor() {
     super();
     this.setComponent(Photos);
   }

  componentDidMount() {
    this.props.getPhotos()
  }
}

export default connect(
  state => { photos: state.photos },
  { getPhotos: actions.getPhotos }
)(PhotosContainer)

See the react-redux example

4 . No render method

Rendering the 'view' is not the concern of the container, that is the concern of the presentational component. Therefore the only thing you should write in the render method of the container is i) what component you want to render and ii) which props you want to pass down. And that is already done by the react-container-component Container class.

5 . Write a test for your container

import React from 'react';
import { createStore } from 'redux';
import { createFakeComponent, Context } from 'react-container-component';
import { expect } from 'chai';
import { mount } from 'enzyme';
import PhotosContainer from '../../../src/containers/PhotosContainer';
import sinon from 'sinon';
import TestUtils from "react-addons-test-utils";

describe('Photos container', () => {
    it(`should fetch photos and pass them down to the child component`, () => {

		const props = {};
		const FakePhotos = createFakeComponent(test);
		const photos = [{
		  "albumId": 1,
		  "id": 1,
		  "title": "accusamus beatae ad facilis cum similique qui sunt",
		  "url": "http://placehold.it/600/92c952",
		  "thumbnailUrl": "http://placehold.it/150/30ac17",
		}];
		const store = createStore(()=>{});
		sinon.stub(store, 'getState').returns({ photos });

		//Example using Enzyme
		const container = mount(
		  <PhotosContainer
			component={FakePhotos}
		  />,
		  { context: { store: store }}
		);

		//Example using TestUtils and the generic Context component
		TestUtils.renderIntoDocument(
		<Context store={store}>
		  <PhotosContainer
		    component={FakePhotos}
		  />
		</Context>
		);

		expect(props.photos).to.be.deep.equal(photos);
	})
})

Examples

Check this folder /examples

Resources