如何使用Jest测试API调用

时间:2019-07-04 09:11:07

标签: reactjs unit-testing jestjs

我正在尝试使用jest和react测试库通过API调用来测试此react组件


callApi = () => {
    fetch(this.getUrl())
    .then((result) => result.json())
        .then((jsonResult) => {
            this.setState({
                name: jsonResult.city.name,})
            for (var i = 0; i<jsonResult.list.length; i ++) {
                this.setState({
                    weather: [...this.state.weather, jsonResult.list[i].weather],
                    date: [...this.state.date, jsonResult.list[i].dt_txt],
                    clouds: [...this.state.clouds, jsonResult.list[i].clouds],
                    main: [...this.state.main, jsonResult.list[i].main],
                    wind: [...this.state.wind, jsonResult.list[i].wind],
                    rain: [...this.state.rain, jsonResult.list[i].rain],
                    snow: [...this.state.rain, jsonResult.list[i].rain]
            })}
            this.setState({
                list: jsonResult.list,
                name: jsonResult.city.name,})}
    )
}

0 个答案:

没有答案