Test mock for vuex context
npm install vuex-mock-context

Test mock for vuex context
npm install vuex-mock-context -D
For testing that vuex actions commit mutations and dispatch actions as expected.
See Composing Actions in the Vuex docs.
You have this vuex action
``js`
save(context, payload) {
context.commit('INCREMENT_SAVE_COUNT');
return context.dispatch('update', payload);
}
You can test it like this
`js
import {create} from 'vuex-mock-context';
import actions from './actions';
test('save action increments count and then updates', function() {
// set up mock context
const context = create();
// invoke action handler
return actions.save(context, {value: 'whatever'})
.then(() => {
// verify context interactions
assert.deepEquals(context.log, [
{mutation: ['INCREMENT_SAVE_COUNT']},
{action: ['update', {value: 'whatever'}]}
]);
});
});
`
If you are using Jest or some other framework that supports snapshot testing, capture and verify context.log with a snapshot:
`js`
expect(context.log).toMatchSnapshot();
`js`
import {create} from 'vuex-mock-context';$3
Create a mock context.
actionHandler is an optional function that receives all parameters sent to dispatch() and returns a Promise which will be the return value of dispatch(). Defaults to function that returns a resolved Promise with undefined value.
Just like context.commit() in vuex.
Just like context.dispatch() in vuex.
Return value is determined by actionHandler, see above.
Array of objects that represent context interactions. There are two types of interactions:
#### 1. mutation
`js`
{mutation: [
For example
`js`
context.commit('DO_SOMETHING', {value: 1});
becomes
`js`
{mutation: ['DO_SOMETHING', {value: 1}]}
#### 2. action
`js`
{action: [
For example
`js`
context.dispatch('save', {id: 5});
becomes
`js``
{action: ['save', {id: 5}]}
All of these start off as an empty object. You can add properties as needed.
MIT