Am I testing connected components correclty?
See original GitHub issueHey folks!
Would like to know if I’m testing correclty my connected component, I’ve made some search, but still not clear for me and the way I’m doing right now doesn’t sounds correclty, thats why I’m asking helping for the jedi council hahaha
The problem: I want to know if action.fetchPurveyors is being called on fetchData that is passed on mapDispatchToProps:
const mapDispatchToProps = (dispatch) => {
return {
fetchData: () => {
dispatch(actions.fetchPurveyors());
}
};
};
export const PurveyorsViewContainer = connect(
mapStateToProps,
mapDispatchToProps
)(PurveyorsView);
So, on my unit test I’m doing this:
describe('Map Dispatch To Props', () => {
it('should call fetch data action', () => {
const dispatchSpy = sinon.spy();
// I'm replacing store's dispatch by an spy
store.dispatch = dispatchSpy;
// I'm justing passing the store as props instead of wrapping it in a Provider
const tree = sd.shallowRender(<PurveyorsViewContainer store={store}/>);
const vdom = tree.getRenderOutput();
// Here I'm calling fetchData that was passed as props by mapDispatchToProps
vdom.props.fetchData();
// And finally I'm checking if the dispatch was called with the action's parameters
const expectedAction = actions.fetchPurveyors();
const spyLastCall = dispatchSpy.args[0][0];
expect(spyLastCall.types).to.be.eql(expectedAction.types);
expect(spyLastCall.callAPI).to.be.ok;
});
});
Is there a better way of doing this? Am I doing an integration test instead of a simple unit test?
Thanks in advance!
Issue Analytics
- State:
- Created 8 years ago
- Reactions:8
- Comments:14 (3 by maintainers)
Top Results From Across the Web
How to test Redux-connected React components
Writing unit tests for Redux-connected React components can be tricky. This article explains how you can create your tests easily without ...
Read more >Testing React #9: testing Redux connected components
Testing React #9: testing Redux connected components ... 10K views 2 years ago Testing React with Jest and Enzyme series.
Read more >How to test a react connected component and what to test of ...
Test Connected Components by hijacking render() Our team has a testUtils wrapper that hijacks the react-testing-library render() method and ...
Read more >Unit Testing React Redux Connected Components
In this tutorial, I will show you how to unit test any custom ReactJS component that talks to a Redux store. As Redux...
Read more >Unit Testing Redux Connected Components | by Luke Pierotti
When trying to write a test for a connected component you will ... testing our component to make sure it is operating properly...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I would just test
mapStateToProps
andmapDispatchToProps
themselves to be honest. Otherwise you seem to be testing what React Redux already tests—that it works 😄 .You can find a discussion on this here: https://github.com/reactjs/redux/issues/1534.
Hope it helps!
@flushentitypacket; write selectors and use them in your
mapStateToProps
. They’ll allow you to get parts of state in your components, without the component needing to know the structure of state.Then, test your reducers, actions and selectors together - so your test starts with dispatching an action, and ends with expectations about the selector’s output.