I would like to test an action , my action contains an api call
my action in vuex
File containts actions in my store ( store 'ex.js' ) exActions.js
export const exActions = {
getExFromApi({ commit, rootGetters }, { id }) {
getApiExById({ access_token: rootGetters.auth.token, id }).then(res => {
commit('SET_INIT_EX', res)
commit('SET_CURRENT_EX', res)
}).catch(error => {
console.warn(error)
})
},
File containt call api exAPi.js
export const getApiExById= (params, group) => {
// call return promise
}
My File test
// import api
const api = require('../../../src/api/exAPi')
let store = null
describe('Unit Test', function() {
beforeEach(() => {
const localVue = createLocalVue()
localVue.use(Vuex)
store = new Vuex.Store({
modules: {
exStore
}
})
})
context('test : action -> getExFromApi', function() {
it('1', async function() {
let rootGetters = {
auth: {
token: 'aaa'
}
}
let commit = cy.spy()
// mock call api , return a promise defined
cy.stub(api, 'getApiExById').resolved('foo')
// start the method
await store.dispatch('getExFromApi', ({commit, rootGetters}, {id: '1'}))
// expect 'foo' value because cy.stub api return 'foo'
expect(commit.args).to.deep.equal([
['SET_INIT_EX', 'foo'],
['SET_CURRENT_EX', 'foo']
])
cy.stub.restore()
start file with node -> ./node_modules/.bin/cypress open-ct The problem, the 'getExFromApi' start but launch the real call api and not the mock.
I do anything wrong ? thanks you for help.