title: How to test React state after calling a component method that updates state - using Enzyme
body: <p>Enzyme newbie here. I am trying to test if the state of a React component is being updated after calling a method on that component.</p>
  
  <p>This is a snippet of the component that I am testing:</p>
  
  <pre class="lang-js prettyprint-override"><code>class App extends React.Component {
    constructor(props) {
      super(props);
    }
  
    state = {
      recipes: {},
      user: null
    };
  
    handleUnauth = () =&gt; {
      this.setState({
        user: null
      });
    };
  
    render() {
     //render children, pass down methods as props etc...
    }
  
  }
  </code></pre>
  
  <p>Below is the test:</p>
  
  <pre class="lang-js prettyprint-override"><code>import createRouterContext from 'react-router-test-context';
  import { shallow, mount } from 'enzyme';
  import expect from 'expect';
  import React from 'react';
  
  import App from 'App';  //import with webpack alias
  
  describe('App', () =&gt; {
  
    let wrapper, context;
  
    beforeEach(() =&gt; {
      context = createRouterContext();
      wrapper = mount(&lt;App/&gt;, { context });
    });
  
    it('should remove logged in user details on logout', () =&gt; {
      const user = {
        uid: 'abc123',
        name: 'John Doe'
      };
  
      wrapper.setState({ user }, () =&gt; {
        wrapper.instance().handleUnauth();
        const userState = wrapper.state('user');
        expect(userState).toEqual(null);
      });
  
    });
  
  });
  </code></pre>
  
  <p>My test fails with the following error:</p>
  
  <p><a href="https://i.stack.imgur.com/OfQmS.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/OfQmS.jpg" alt="enter image description here"></a></p>
  
  <p>I understand that updating of state is not synchronous but I am not sure if that has something to do with this or if there are better ways to test this using Enzyme. Would be super grateful if someone can please point me in the right direction. Oh and I tried this by calling <code>wrapper.update()</code> right after calling <code>wrapper.instance().handleUnauth()</code> in the test but this too didn't work.</p>