|
@@ -1,5 +1,5 @@
|
|
|
import Vue from 'vue/dist/vue.common.js'
|
|
|
-import Vuex, { mapState, mapMutations, mapGetters, mapActions } from '../../dist/vuex.common.js'
|
|
|
+import Vuex, { mapState, mapMutations, mapGetters, mapActions, createNamespacedHelpers } from '../../dist/vuex.common.js'
|
|
|
|
|
|
describe('Helpers', () => {
|
|
|
it('mapState (array)', () => {
|
|
@@ -321,4 +321,61 @@ describe('Helpers', () => {
|
|
|
vm.bar()
|
|
|
expect(b).toHaveBeenCalled()
|
|
|
})
|
|
|
+
|
|
|
+ it('createNamespacedHelpers', () => {
|
|
|
+ const actionA = jasmine.createSpy()
|
|
|
+ const actionB = jasmine.createSpy()
|
|
|
+ const store = new Vuex.Store({
|
|
|
+ modules: {
|
|
|
+ foo: {
|
|
|
+ namespaced: true,
|
|
|
+ state: { count: 0 },
|
|
|
+ getters: {
|
|
|
+ isEven: state => state.count % 2 === 0
|
|
|
+ },
|
|
|
+ mutations: {
|
|
|
+ inc: state => state.count++,
|
|
|
+ dec: state => state.count--
|
|
|
+ },
|
|
|
+ actions: {
|
|
|
+ actionA,
|
|
|
+ actionB
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ const {
|
|
|
+ mapState,
|
|
|
+ mapGetters,
|
|
|
+ mapMutations,
|
|
|
+ mapActions
|
|
|
+ } = createNamespacedHelpers('foo/')
|
|
|
+ const vm = new Vue({
|
|
|
+ store,
|
|
|
+ computed: {
|
|
|
+ ...mapState(['count']),
|
|
|
+ ...mapGetters(['isEven'])
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ ...mapMutations(['inc', 'dec']),
|
|
|
+ ...mapActions(['actionA', 'actionB'])
|
|
|
+ }
|
|
|
+ })
|
|
|
+ expect(vm.count).toBe(0)
|
|
|
+ expect(vm.isEven).toBe(true)
|
|
|
+ store.state.foo.count++
|
|
|
+ expect(vm.count).toBe(1)
|
|
|
+ expect(vm.isEven).toBe(false)
|
|
|
+ vm.inc()
|
|
|
+ expect(store.state.foo.count).toBe(2)
|
|
|
+ expect(store.getters['foo/isEven']).toBe(true)
|
|
|
+ vm.dec()
|
|
|
+ expect(store.state.foo.count).toBe(1)
|
|
|
+ expect(store.getters['foo/isEven']).toBe(false)
|
|
|
+ vm.actionA()
|
|
|
+ expect(actionA).toHaveBeenCalled()
|
|
|
+ expect(actionB).not.toHaveBeenCalled()
|
|
|
+ vm.actionB()
|
|
|
+ expect(actionB).toHaveBeenCalled()
|
|
|
+ })
|
|
|
})
|