Skip to content
Mock react-redux for testing
Branch: master
Clone or download
Latest commit 5a397b1 Feb 5, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib export as comonjs package and add react as peer dependency Mar 10, 2017
src fix index file Mar 10, 2017
.babelrc
.gitignore [muzi] add .idea to .gitignore Jan 30, 2018
LICENSE Initial commit Mar 10, 2017
README.md Update README.md Feb 5, 2018
package.json [muzi] adding babel-preset-react and babel-preset-stage-0 as dependen… Jan 30, 2018
webpack.config.js export as comonjs package and add react as peer dependency Mar 10, 2017
yarn.lock export as comonjs package and add react as peer dependency Mar 10, 2017

README.md

redux-react-mock

Mock the react-redux package.

Installation

yarn add --dev react-redux-mock
npm install --save-dev react-redux-mock

Usage

Let's assume that you have an connected component with this structure.

import React from 'react';
import { connect } from 'react-react';

const Text = ({ content }) => <p>{content}</p>;
const contentSelector = state => state.content;

const ConnectedText = compose(
  connect(state => {
    content: contentSelector(state),
  }),
)(Text);

export default ConnectedText;

This is how you can test the component above with react-redux-mock?

jest.mock('react-redux', () => require('react-redux-mock'));
import { __setState } from 'react-redux';
import { mount } from 'enzyme';

describe('Text component', () => {
  it('renders correctly', () => {
    __setState({
      content: 'foo',
    });
    const component = mount(<ConnectedText />);
    expect(component).toMatchSnapshot();
  });
});

Notice that you can render the connected component itself without defining what is the store and the Provider component.

Have question?

I'm happy to answer all questions from you at @khanght.
Happy coding, forks!

You can’t perform that action at this time.