question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Am I testing connected components correclty?

See original GitHub issue

Hey 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:closed
  • Created 8 years ago
  • Reactions:8
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

100reactions
gaearoncommented, Mar 21, 2016

I would just test mapStateToProps and mapDispatchToProps 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!

39reactions
atkinchriscommented, Nov 22, 2016

@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.

// Bad
const mapStateToProps = state => ({
  items: state.data.items
})

// Good
import { selectItems } from './store/selectors'

const mapStateToProps = state => ({
  items: selectItems(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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found