In Vue, I want to check if an action in my store is correctly calling another action using Jest's spyOn
, I tried it different ways but it doesn't seem to work, here's my code:
// index.js
getRecipes ({ dispatch }) {
const fruits = ['apple', 'banana', 'pear']
fruits.forEach((fruit) => {
dispatch('getRecipe', fruit)
})
},
async getRecipe ({ commit }) {
const recipe = await recipesService.fetchRecipe(payload)
commit(SET_RECIPE, { recipe })
},
// index.spec.js
test('getRecipes calls getRecipe 3 times, each with the right fruit', () => {
const commit = jest.fn()
const dispatch = jest.fn()
const spy = spyOn(actions, 'getRecipe')
const result = actions.getRecipes({ commit, dispatch })
expect(spy).toHaveBeenCalledTimes(3)
expect(spy).toHaveBeenCalledWith('apple')
})
But when I run the tests, this is the output I get:
Expected spy to have been called three times, but it was called zero times.
I have other places where I want to test these kind of integrations (an action calling another one), but it still gives me this error.