我正在使用jest和酶来测试我的组件。在这个组件中,我将在componentDidMount中请求数据,然后它将使用数据呈现视图。
class App extends React.Component {
async componentDidMount() {
const data = await getData(); // send a request to server to get data
this.setState({
name: data.name,
})
}
render() {
return (
<di
我有一个生命周期方法componentDidMount,它调用一个函数setDateOnMount,它将成为其中的间谍。我正在使用jest和酶来测试setDateOnMount是否运行,我得到了一个错误,它没有被调用。
我见过使用mount而不是shallow调用包装器的帖子,但我不能使用mount,因为我使用的是react-dates包,而且它与mount冲突,在mount中我无法运行它。
这是我的测试:
test('componentDidMount should be called with setDateOnMount', () => {
cons
我的组成部分:
componentDidMount() {
// Make HTTP reques with Axios
axios.get(APIConfig.api_profile()).then((res) => {
// Set state with result
this.setState(res.data);
console.log('I was triggered during componentDidMount')
console.log(res)
});
}
我的测试
基本上,我有一个在componentDidMount上调用多个API的屏幕,每个API都会触发一个网络请求和一个回调,如下所示
componentDidMount() {
new APIManager.fetchBooks(this.handleBooks);
new APIManager.fetchAuthor(this.handleAuthor);
new APIManager.fetchShops(this.handleShops);
}
...
handleBooks = (result) => {
this.setState({ books: r