In Vuex, Actions are analagous to Reducers in Redux. They are functions which can contain asynchronous
operations, but they do not manipulate any state in the store
directly. Instead, they can only commit
mutations, which are synchronous functions that replace an
existing store value with a new one.
Since Actions can potentially contain some complicated logic (like deciding which mutation or series of mutations should be committed), they are a good candidate for unit tests. Here is an example of how to do this following the suggestions in the Vuex docs.
Actions commit mutations rather than returning values or directly modifying the store, so we will write tests to ensure that an action commits the correct sequence of mutations from a starting state.
The Vuex documentation provides an example helper function for this purpose.
These examples are taken from a project currently in production, a user interface for a hotel booking service. Users have the ability to view a calendar of available dates for a trip. Clicking on days in the calendar updates the global date selection state which is used for a variety of other features (calculating length of stay, total price, etc).
- booking.js: Vuex module for our current area of concern
- booking.spec.js: the test file
- testAction.js: helper for testing actions