[Vuejs]-Vue.js unit test w sinon, how to stub a method?

2👍

just stubbing the action with a Promise resolving the status ( true / false

    import LoginPage from '@/pages/LoginPage'
    import Vue from 'vue'
    import Vuex from 'vuex'
    import sinon from 'sinon'
    import { mount } from 'avoriaz'

    Vue.use(Vuex)

    describe('LoginPage.vue', () => {
      let actions
      let store
      let sandbox

      beforeEach(() => {
        sandbox = sinon.sandbox.create()
      })

      afterEach(() => {
        sandbox.restore()
      })

      it('calls store action login when the form is submitted, w good credentials', (done) => {
        actions = {
          login: sandbox.stub().returns(Promise.resolve(true))
        }
        store = new Vuex.Store({
          actions
        })
        const wrapper = mount(LoginPage, { store })
        const form = wrapper.find('form')[0]
        form.trigger('submit')
        wrapper.vm.$nextTick(() => {
          expect(actions.login.calledOnce).to.equal(true)
          expect(wrapper.data().hasError).to.equal(false)
          done()
        })
      })

      it('calls store action login when the form is submitted, w wrong credentials', (done) => {
        actions = {
          login: sandbox.stub().returns(Promise.resolve(false))
        }
        store = new Vuex.Store({
          actions
        })
        const wrapper = mount(LoginPage, { store })
        const form = wrapper.find('form')[0]
        form.trigger('submit')
        wrapper.vm.$nextTick(() => {
          expect(actions.login.calledOnce).to.equal(true)
          expect(wrapper.data().hasError).to.equal(true)
          done()
        })
      })
    })

Leave a comment