mardi 15 octobre 2019

How to detect call in mocked, dependency library by JEST?

I have module called "api". It is a axios object with defined interceptors, like below:

import axios from 'axios'
import VueCookie from 'vue-cookie'

const api = axios.create(...)
api.interceptors.response.use(config => { ... }, error => {
   if (error.response.data.error === 'some-error') {
       VueCookie.set('error', 'some-error')
   }
})

export default api

now I want to detect if VueCookie set method has been called with 'error' and 'some-error'. How can I do it in my test?

Now I have mocked VueCookie like below but it I do not know how to get access to this mock and trigger expect().toHaveBeenCalledWith() on that. Solution below does not work.

const cookies = jest.mock('vue-cookie', () => {
            return {
                get: (key) => {
                    const cookies = {
                        timezone: 'Warsaw',
                        jwt: 'foo'
                    }

                    return cookies[key]
                }
            }
        })

...

it('test', () => {
    // calling interceptor
    expect(cookies).toHaveBeenCalledWith('error', 'some-error')
})

Regards

Aucun commentaire:

Enregistrer un commentaire