How to test componentWillMount inside a connected React-Redux component?
<p>I have the following smart component which uses componentWillMount lifecycle method to make an async call to fetch data. I am writing tests for it but I am unable to test if the function gets called and if it does it is called before the component gets mounted. They are important cases to cover.</p>

<p>The code for the smart component is as follows:</p>

<pre><code>const mapStateToProps = (state) =&gt; {   const context = state.context;  return {
    isError: context.error,   }; };

const options = {   componentWillMount: (props) =&gt; {
    const { dispatch } = props;
    dispatch(fetchContextUser());   }, };

export function App(props) {   return (
    &lt;div className="app"&gt;
      { props.isError ? (&lt;ContextError /&gt;) : (&lt;Main /&gt;) }
      { props.children }
    &lt;/div&gt;   ); }

App.propTypes = {   children: PropTypes.object, // eslint-disable-line react/forbid-prop-types   isError: PropTypes.bool.isRequired, // eslint-disable-line react/forbid-prop-types };

export default connect(mapStateToProps, null)(functional(App, options));
</code></pre>

<p>I am using Enzyme, Chai and other mock adapters for testing this component. The test block is follows :</p>

<pre><code>describe.only('Render Connected Component', () =&gt; {   let store;   beforeEach(() =&gt; {
    store = mockStore({
      context: {
        error: false,
      },
    });   });   it('Should render connected components', () =&gt; {
    const connectedWrapper = mount(
      &lt;Provider store={store}&gt;
        &lt;ConnectedApp /&gt;
      &lt;/Provider&gt;
    );
    expect(connectedWrapper).to.be.present();   }); });
</code></pre>

<blockquote>
  <p>I just want to test two things :</p>
  
  <p>1.) fetchContextUser is called
  2.) fetchContextUser is called before the component gets mounted</p>
</blockquote>