I have checked at least a couple of tutorials on mocking and testing Vuex actions but I wasn't able to successfully implement them on my own. It would always result having toHaveBeenCalled
to false
though I followed most of the steps provided on the links above. You can mock Actions without having to replicate their actual functionality using jest.fn()
right? I don't get why I still can't successfully pull this off.
store.js
export default new Vuex.Store({
state: {
currentSequence: ''
},
actions: {
runGenerator({ commit, state }, currentSequence) {
// do something with currentSequence
}
}
})
Home.vue (Take note that this not the entire code for this component but I listed the important ones including the submit.prevent method, the html that contains the form and where the vuex action is called)
<template>
<v-app id="inspire">
<v-form @submit.prevent="setNextVal" id="sequence-form">
<!-- form contents here -->
</v-form>
</v-app>
</template>
<script>
import { mapActions } from 'vuex'
export default {
methods: {
setNextVal() {
this.runGenerator(this.form.currentSequence)
this.form.currentValue = this.getCurrentValue
},
...mapActions([
'runGenerator'
]),
}
}
</script>
store.spec.js
import { shallowMount, createLocalVue } from '@vue/test-utils'
import Vue from 'vue'
import Vuex from 'vuex'
import Vuetify from 'vuetify'
import Home from '@/views/Home.vue'
const localVue = createLocalVue()
localVue.use(Vuex)
Vue.use(Vuetify)
describe('Home.vue', () => {
let actions
let store
beforeEach(() => {
actions = {
runGenerator: jest.fn()
}
store = new Vuex.Store({
state: {
currentSequence: ''
},
actions
})
})
it('Dispatches "runGenerator" when submit.prevent has been triggered', () => {
const wrapper = shallowMount(Home, { store, localVue })
const form = wrapper.find('#sequence-form')
form.trigger('submit.prevent')
expect(actions.runGenerator).toHaveBeenCalled()
})
})
After running the test I get the following error:
expect(jest.fn()).toHaveBeenCalled() Expected mock function to have been called, but it was not called
What did I miss?? Please enlighten me guys.. I kept reading the reference provided online and I can't seem to find some other solutions online.