How to Unit Test React-Redux Connected Components?
A prettier way to do this, is to export both your plain component, and the component wrapped in connect. The named export would be the component, the default is the wrapped component:
export class Sample extends Component { render() { let { verification } = this.props; return ( <h3>This is my awesome component.</h3> ); }}const select = (state) => { return { verification: state.verification }}export default connect(select)(Sample);
In this way you can import normally in your app, but when it comes to testing you can import your named export using import { Sample } from 'component'
.
The problem with the accepted answer is that we are exporting something unnecessarily just to be able to test it. And exporting a class just to test it is not a good idea in my opinion.
Here is a neater solution without the need of exporting anything but the connected component:
If you are using jest, you can mock connect
method to return three things:
- mapStateToProps
- mapDispatchToProps
- ReactComponent
Doing so is pretty simple. There are 2 ways: Inline mocks or global mocks.
1. Using inline mock
Add the following snippet before the test's describe function.
jest.mock('react-redux', () => { return { connect: (mapStateToProps, mapDispatchToProps) => (ReactComponent) => ({ mapStateToProps, mapDispatchToProps, ReactComponent }), Provider: ({ children }) => children }})
2. Using file mock
- Create a file
__mocks__/react-redux.js
in the root (where package.json is located) - Add the following snippet in the file.
module.exports = { connect: (mapStateToProps, mapDispatchToProps) => (ReactComponent) => ({ mapStateToProps, mapDispatchToProps, ReactComponent, }), Provider: ({children}) => children};
After mocking, you would be able to access all the above three using Container.mapStateToProps
,Container.mapDispatchToProps
and Container.ReactComponent
.
Container can be imported by simply doing
import Container from '<path>/<fileName>.container.js'
Hope it helps.
Note that if you use file mock. The mocked file will be used globally for all the test cases(unless you do jest.unmock('react-redux'))
before the test case.
Edit: I have written a detailed blog explaining the above in detail:
You can test your connected component and I think you should do so. You may want to test the unconnected component first, but I suggest that you will not have complete test coverage without also testing the connected component.
Below is an untested extract of what I do with Redux and Enzyme. The central idea is to use Provider to connect the state in test to the connected component in test.
import { Provider } from 'react-redux';import configureMockStore from 'redux-mock-store';import SongForm from '../SongForm'; // import the CONNECTED component// Use the same middlewares you use with Redux's applyMiddlewareconst mockStore = configureMockStore([ /* middlewares */ ]);// Setup the entire state, not just the part Redux passes to the connected component.const mockStoreInitialized = mockStore({ songs: { songsList: { songs: { songTags: { /* ... */ } } } }}); const nullFcn1 = () => null;const nullFcn2 = () => null;const nullFcn3 = () => null;const wrapper = mount( // enzyme <Provider store={store}> <SongForm screen="add" disabled={false} handleFormSubmit={nullFcn1} handleModifySong={nullFcn2} handleDeleteSong={nullFcn3} /> </Provider> );const formPropsFromReduxForm = wrapper.find(SongForm).props(); // enzymeexpect( formPropsFromReduxForm ).to.be.deep.equal({ screen: 'add', songTags: initialSongTags, disabled: false, handleFormSubmit: nullFcn1, handleModifySong: nullFcn2, handleDeleteSong: nullFcn3, });===== ../SongForm.jsimport React from 'react';import { connect } from 'react-redux';const SongForm = (/* object */ props) /* ReactNode */ => { /* ... */ return ( <form onSubmit={handleSubmit(handleFormSubmit)}> .... </form>};const mapStateToProps = (/* object */ state) /* object */ => ({ songTags: state.songs.songTags});const mapDispatchToProps = () /* object..function */ => ({ /* ... */ });export default connect(mapStateToProps, mapDispatchToProps)(SongForm)
You may want to create a store with pure Redux. redux-mock-store is just a light-weight version of it meant for testing.
You may want to use react-addons-test-utils instead of airbnb's Enzyme.
I use airbnb's chai-enzyme to have React-aware expect options. It was not needed in this example.