react-nativereact-reduxjestjsenzymeredux-mock-store

Check if redux action has been called with enzyme


I am trying to test if a dispatch has been called from componentDidMount in my React Native project. My problem is that I cant figure out how to check if autoLogin() has been called.

test.js

import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';

const mockStore = configureMockStore([thunk]);

it('should be called once', () => {
    const wrapper = shallow(<SplashScreen store={mockStore()} />).dive();
});

index.js

class SplashScreen extends Component {
    componentDidMount() {
        this.props.autoLogin();
    }
}

export default connect(null, { autoLogin })(SplashScreen);

Solution

  • I finally figured out that async/await was the solution to my problem. Without await on wrapper.dive(), autoLogin would fire after my test had finished to run.

    it('should be called once', async () => {
        const autoLogin = jest.fn();
        const wrapper = shallow(<SplashScreen store={mockStore()} autoLogin={autoLogin} />);
        await wrapper.dive();
        expect(autoLogin.mock.calls.length).toBe(1);
    });