检查是否已使用酶调用 redux 操作

Check if redux action has been called with enzyme

我正在尝试测试是否已从我的 React Native 项目中的 componentDidMount 调用了分派。 我的问题是我不知道如何检查是否已调用 autoLogin()。

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);

我终于发现 async/await 是我的问题的解决方案。 如果不等待 wrapper.dive(),autoLogin 将在我的测试完成到 运行.

后触发
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);
});