如何在更新状态

时间:2016-11-25 13:35:59

标签: unit-testing reactjs testing enzyme

我很难理解如何在函数中测试我的动作:

    componentDidMount() {
    $.ajax(
        {
            url: this.props.url,
            dataType: 'json',
            cache: false,
            success: function (data) {
                if (this.props.url === "./information.json")
                    this.props.updateInfosAction(data.transport);
                else
                    this.props.updateNotifsAction(data.transport);
            }.bind(this),
            error: function (xhr, status, err) {
                console.error(this.props.url, status, err.toString());
            }.bind(this)
        }
    );
}

这是我的测试,以确保ComponentDidMount被调用(它有效):

    it('Should call component did mount', () => {
   expect(GetInfo.prototype.componentDidMount.calledOnce);
});

但我不知道如何实际测试行动。

1 个答案:

答案 0 :(得分:0)

用于测试异步数据获取,使用nock之类的东西来模拟请求是相当常见的:

https://github.com/node-nock/nock