RMT
RMT

Reputation: 1162

Shallow test component, complain about required props gotten from mapStateToProps and mapDispatchToProps

I would like to shallow test my component, and I know that the shallow test only test one level deep. My problem is that I get warnings because my component is requireing props that is received by mapStateToProps and mapDispatchToProps. This makes me want to know how to properly send this props down to my shallow rendered component without duplicate the methods. I am using jest and enzyme.

Upvotes: 0

Views: 115

Answers (1)

Tr1et
Tr1et

Reputation: 895

Let's call your component is ComponentA wrapped in HOC connect of React-Redux (assuming you are using it as there are mapStateToProps and mapDispatchToProps in your question). The traditional way for testing this component is:

  1. Get the wrapped component: The connect have a static property named WrappedComponent, so you get it out like this:

    const ExtractedComponent = ComponentA.WrappedComponent;
    
  2. Mock the functions and props and pass it like normal props into the extracted component:

    const wrapper = mount(<ExtractedComponent connectFunctionA={mockA} ... />);
    
  3. Test it like normal component! :)

Upvotes: 2

Related Questions