【问题标题】:Check if redux action has been called with enzyme检查是否已使用酶调用了 redux 操作
【发布时间】:2018-04-19 14:17:32
【问题描述】:

我正在尝试测试是否在我的 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);

【问题讨论】:

  • 请添加mockStore函数的代码。
  • 我已经添加了您要求的代码。

标签: react-native react-redux jestjs enzyme redux-mock-store


【解决方案1】:

我终于发现 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);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多