I have problem with mocking window.location.replace function under @vue/test-utils and JEST. I am pretty sure I mocked function correctly but JEST does not see a call. This is a fragment of code I am trying to test:
// function called by response interceptor
const redirectToLogin = async () => {
try {
VueCookie.delete('jwt')
const url = localStorage.getItem('base-url') || window.location.origin
window.location.replace(url + '/jwt/token');
console.log(window.location.replace) //it prints [Function: mockConstructor]
} catch (e) {
console.log(e)
}
}
And part of my test:
const location = {
origin: 'http://origin',
replace: jest.fn()
}
delete global.window.location
delete global.window.localStorage
global.window = Object.create(window)
global.window.location = location
global.window.localStorage = {
getItem: jest.fn(),
setItem: jest.fn()
}
api.interceptors.response.handlers.rejected[0]({ response: { data: { error: 'unauthorized', base_url: 'http://base' } } }) // calling interceptor
expect(location.replace).toHaveBeenCalledWith('someurl')
and I get
Expected mock function to have been called with: ["someurl"] But it was not called.
Aucun commentaire:
Enregistrer un commentaire