Open
Description
What problem does this feature solve?
The officially documented way to test actions
- has a complex interface (5 positional, required arguments)
- is always asynchronous—even if the action under test is not
- requires copying a helper function from the documentation into a project's code base
- involves manually patching that helper because of some shortcomings (see for example Proposal: Enhancement for testAction helper method #939)
- offers no way to automatically update the copied helper once it changes in the documentation
What does the proposed API look like?
Recommend to use https://github.com/posva/vuex-mock-store instead.
// actions.js
import shop from '../api/shop'
export const getAllProducts = ({ commit }) => {
commit('REQUEST_PRODUCTS')
return shop.getProducts().then(products => {
commit('RECEIVE_PRODUCTS', products)
})
}
// actions.spec.js
import { Store } from 'vuex-mock-store'
const store = new Store()
afterEach(() => store.reset())
describe('actions', () => {
it('getAllProducts', done => {
actions.getAllProducts(store)
.then(() => {
expect(store.commit).toHaveBeenCalledTimes(2)
expect(store.commit).toHaveBeenCalledWith('REQUEST_PRODUCTS')
expect(store.commit).toHaveBeenCalledWith('RECEIVE_PRODUCTS', /* mocked response */)
})
.then(done)
.catch(done.fail)
})
see also vuejs/vue-test-utils#1060