我想为 componentDidMount 上的 await API 调用编写一个测试用例,以获取成功和错误。
我添加了 API 调用的代码行为,请为此建议测试用例实现。
import {apiService} from './../../../_services'
import { Component } from 'react';
class MyComponent extends Component {
constructor(props) {
super(props);
this.state = {
contentId: 123,
userID: 1345
};
}
componentDidMount() {
this.getMappedContent()
.then(content => {
if (content) {
if (content.contentData && content.contentData.isLocked) {
if (content.contentData.lockedByEmail !== this.state.userID) {
this.setState({ isLocked, lockedBy })
}
}
this.setState({users: content.contentData})
}
})
.catch(error => {
})
}
getMappedContent = async () => {
if (!this.state.contentId) {
return; …Run Code Online (Sandbox Code Playgroud)