[Vuejs]-Vue.js w vuex : mocked action not executed

0👍

Finally , I found a way to test it :

    import Vue from 'vue'
    import Vuex from 'vuex'
    import VueRouter from 'vue-router'

    import App from '@/App'

    import router from '@/router/index'

    import { mount } from 'avoriaz'
    import sinon from 'sinon'

    Vue.use(Vuex)
    Vue.use(VueRouter)

    describe('App.vue', () => {
      let actions
      let getters
      let store
      let sandbox
      let routerPush

      beforeEach(() => {
        sandbox = sinon.sandbox.create()
        getters = {
          isAuthenticated: (state) => {
            return state.isAuthenticated
          }
        }
        actions = {
          logout: sandbox.stub().returns(Promise.resolve(true))
        }
        store = new Vuex.Store({
          getters,
          state: {
            isAuthenticated: true,
            currentUserId: ''
          },
          actions
        })
        router
      })
      afterEach(() => {
        sandbox.restore()
      })

      it('calls logout method', (done) => {
        const wrapper = mount(App, { store, router })
        routerPush = sandbox.spy(wrapper.vm.$router, 'push')
        const logoutLink = wrapper.find('#logout a')[0]
        logoutLink.trigger('click')
        wrapper.vm.$nextTick(() => {
          expect(actions.logout.calledOnce).to.equal(true)
          actions.logout().then(() => {
            expect(routerPush).to.have.been.calledWith('/home')
          })
          done()
        })
      })
    })

Leave a comment