简体   繁体   中英

Trying unit testing store, How to make it work?

I have this store.js file

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export const mutations = {
  increment: state => state.count++,
  Changeloading(state, status) { state.loading = status },
  ChangeUserGroup(state, newGroup) { state.userGroup = newGroup; },
  ChangeOriginalRole(state, newRole) { state.originalRole = newRole; }
}
export default new Vuex.Store({
  state: {
    count: 0,
    loading: false, //header, TeamLead, TeamMember
    listUserGroup: [],
    userRole: "",
    originalRole: "",
    userGroup: {}
  },
  mutations,
 ...
})

In my testing file store.spec.js

import { expect } from 'chai'
import mutations from '@/store'

// destructure assign `mutations`
const { increment } = mutations

describe('mutations', () => {
  it('INCREMENT', () => {
    // mock state
    const state = { count: 0 }
    // apply mutation
    increment(state)
    // assert result
    expect(state.count).to.equal(1)
  })
})

This is the result i am getting:

mutations 1) INCREMENT

0 passing (75ms) 1 failing

1) mutations INCREMENT: TypeError: increment is not a function

at Context.increment (dist\\webpack:\\tests\\unit\\store.spec.js:13:5)

EDIT (4/16/2019)

I walk down one more step. I saw here that I should "export" all the components in my store.js like:

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export const mutations = {...};

export const state = {...};

export const actions = {...};

export const getters = {...};

export default new Vuex.Store({
  state,
  mutations,
  actions,
  getters
});

But even in that way... I'm getting the ugly (testing fail message)

0 passing (61ms) 1 failing

1) mutations increment: TypeError: increment is not a function at Context.increment (dist\\webpack:\\tests\\unit\\store.spec.js:12:5)

I finally found the answer for this one: In my test file I have

import { expect } from 'chai'
import mutations from '@/store'

right way to import mutations should be...

import { expect } from 'chai'
import {mutations} from '@/store'

The case was resolved :) by { }

Regards,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM