I have a React component which is displaying count of data. In componentDidMount; getChannel (it is an axios get call api) function returns count and then this.setState update workedOnDataTotalElementCount value. I am trying to write a test for componentDidMount and the api call. How can I write a test for updating a workedOnDataTotalElementCount after api call ? I am using Jest(^21.2.1) and Enzyme(3.1.1).
class DocumentFromCounterCompany extends Component {
constructor(props) {
super(props)
this.state = {
workedOnDataTotalElementCount: 0
}
}
componentDidMount() {
getChannel().then(res => {
if (!res) return
else {
this.setState({ workedOnDataTotalElementCount: res.data.channel })
}
})
}
I use axios in api calls. My api code is:
export function getChannel(params) {
return GetAjaxAsync({
url: `api/indexcenter/documents?${params}`,
isDirectCall: true,
onStart: () => {
return {
fetching: true
}
},
onSuccess: data => {
return {
channel: data
}
},
onError: err => {
return {
error: err
}
},
onFinally: response => {
return {
fetching: false
}
}
})
}
getChannelcome from? Show the code - slideshowp2