I am working on writing test cases for a project. I am writing the test for my Container. The container has a function which is as shown below:
getContactDetails = (reqObject) => {
app.outageCenterService.getContact(reqObject).then(
response => {
app.logger.getLogger().info('Below is the response...');
app.logger.getLogger().info(this.state.contactDetails);
this.setState({contactDetails: response.contactDetails},()=>{});
if (this.state.contactDetails.isContactPresent) {
this.setState({ isVisible: true });
} else {
this.setState({ isVisible: false });
}
},
reject => {
app.logger.getLogger().info(reject);
}
);
}
While running the test,in the function,the line app.outageCenterService.getContact(reqObject)
throws an error saying TypeError: Cannot read property 'getContact' of undefined
. I understand its because outageCenterService
is globally defined and jest/enzyme is not able to find it. But I don't know how to solve this issue.
My test looks something like this:
describe('test the OutageAlert Component', () => {
let outageAlert, errorHandlerFn;
errorHandlerFn=jest.fn();
getContactFn=jest.fn();
outageAlert = shallow(<OutageAlertComponent errorHandler={errorHandlerFn} getContact={getContactFn} />);
});
Can anyone please help me with this on how to write the test case for this scenario?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…