Testing actions

Testing actions means testing that the action commits the expected mutations. We are not interested in the mutations themselves (not in unit tests at least) because they are already tested separately. We might, though, need to mock some dependencies.

To avoid any dependencies from Vue or Vuex (since we don't need them and they may pollute the tests), we create a new actions.js file inside the store directory. Install Axios with npm install axios. The actions.js file can look like the following:

import axios from 'axios'export const actions = {  downloadNew ({ commit }) {    axios.get('/myNewPosts')      .then(({ data }) => {        commit('ADD_ITEMS', data)      })  }}

To test for requirement number 2, we start by mocking the call to the server ...

Get Vue.js 2 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.