When I do a shallow of a component, a method_1()
get called in componentDidMount()
which call an utility method_2()
and finally it makes an call to method_3()
which is a promise based request.
It throws the below error when running npm test
Example: Loginn.spec.js:
const wrapper = shallow(<Login />)
Error: UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
Flow of the code given below.
Login.js:
import { AuthService } from '../../react-auth';
componentWillMount() {
const config = {
environment: 'qa',
google: true
}
const updateLoginState = function (isBool, currentContext) {
currentContext.updateState('authStatusCheck', isBool);
}
authService.init(config, this, updateLoginState)
}
Auth.js
import UmsLib from './umsLib'
export default class AuthService{
init(customOptions, loginContext, updateLoginState) {
// some code.
UmsLib.getStatus(customOptions, loginContext, updateLoginState);
};
}
ums.js
import ums from ‘user-management’;
export class UmsLib{
getStatus(config, loginContext, updateLoginState) {
ums.init(this.configOptions)
.then(user => {
console.log("promise-----------------")
if (user && ums.isReady) {
return updateLoginState(true, loginContext);
}
})
.catch(error => {
throw Error('Failed calling UMS Library', error);
})
}
}
I have added try/catch to throw all the possible error and also tried handling promise in the test case, but it seems I am doing wrong something somewhere. Any suggestion would be appreciated.