vuex-mock-context
Test mock for vuex context
Install
npm install vuex-mock-context -D
Why
For testing that vuex actions commit mutations and dispatch actions as expected.
See Composing Actions in the Vuex docs.
Usage
You have this vuex action
{ context; return context;}
You can test it like this
;; ;
Snapshot testing
If you are using Jest or some other framework that supports snapshot testing, capture and verify context.log
with a snapshot:
;
API
;
const mockContext = create(actionHandler)
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.
mockContext.commit()
Just like context.commit()
in vuex.
mockContext.dispatch()
Just like context.dispatch()
in vuex.
Return value is determined by actionHandler
, see above.
mockContext.log
Array of objects that represent context interactions. There are two types of interactions:
1. mutation
mutation: <args passed to commit>
For example
context;
becomes
mutation: 'DO_SOMETHING' value: 1
2. action
action: <args passed to dispatch>
For example
context;
becomes
action: 'save' id: 5
mockContext.state
mockContext.getters
mockContext.rootState
mockContext.rootGetters
All of these start off as an empty object. You can add properties as needed.
License
MIT