Created
September 23, 2019 23:54
-
-
Save up209d/5395d5cfa5d9d07ad8b83a795c0b6b02 to your computer and use it in GitHub Desktop.
Jest Async Test - Axios Mock - Rejection case
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const sendForgotPasswordEmail = email => { | |
return axios | |
.put(forgotPasswordResetUrl, { email, isForgotPasswordEmail: true, deactivate: true }, { customSuccessMessage: '' }) | |
.then(response => { | |
return response; | |
}) | |
.catch(error => { | |
throw error; | |
}); | |
}; | |
// Test rejection case | |
describe('sendForgotPasswordEmail', () => { | |
it('should send correctly', async () => { | |
instance.sendForgotPasswordEmail('[email protected]'); | |
expect(axiosPutMock).toHaveBeenCalledTimes(1); | |
expect(axiosPutMock).toHaveBeenCalledWith( | |
'identify/api/v1/resetPassword/user', | |
{ deactivate: true, email: '[email protected]', isForgotPasswordEmail: true }, | |
{ customSuccessMessage: '' } | |
); | |
axios.put = jest.fn().mockRejectedValue('Request Error'); | |
try { | |
await instance.sendForgotPasswordEmail('[email protected]'); | |
} catch (error) { | |
expect(axios.put).toHaveBeenCalledTimes(1); | |
expect(error).toEqual('Request Error'); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment