如何使用jest和酶在组件内测试以下方法?

时间:2017-09-27 15:45:17

标签: reactjs unit-testing enzyme jest

这是我尝试测试的组件内部的方法,但是我收到此错误:{TypeError:无法读取属性'push'of undefined}

setQuery(newQueryElement) 
    //merge together the current query with the new query element
    var currentQuery = QueryString.parse(this.state.currentProjectQuery);
    Object.assign(currentQuery, newQueryElement);

    //now turn the query into a query string and navigate to it
    var queryString = QueryString.stringify(currentQuery);
    this.props.history.push({
        pathname: this.props.location.pathname,
        search: "?" + queryString
    });
}

1 个答案:

答案 0 :(得分:0)

您必须使用道具history模拟您的组件:

const history = []

const wrapper = shallow(<YourComponent history={history} />

然后你可以像这样测试你的history对象:

expect(history).toHaveLength(1)