|
1 | 1 | import Vue from 'vue/dist/vue.common.js' |
2 | | -import Vuex, { mapState, mapMutations, mapGetters, mapActions } from '../../dist/vuex.common.js' |
| 2 | +import Vuex, { mapState, mapMutations, mapGetters, mapActions, createNamespacedHelpers } from '../../dist/vuex.common.js' |
3 | 3 |
|
4 | 4 | describe('Helpers', () => { |
5 | 5 | it('mapState (array)', () => { |
@@ -321,4 +321,61 @@ describe('Helpers', () => { |
321 | 321 | vm.bar() |
322 | 322 | expect(b).toHaveBeenCalled() |
323 | 323 | }) |
| 324 | + |
| 325 | + it('createNamespacedHelpers', () => { |
| 326 | + const actionA = jasmine.createSpy() |
| 327 | + const actionB = jasmine.createSpy() |
| 328 | + const store = new Vuex.Store({ |
| 329 | + modules: { |
| 330 | + foo: { |
| 331 | + namespaced: true, |
| 332 | + state: { count: 0 }, |
| 333 | + getters: { |
| 334 | + isEven: state => state.count % 2 === 0 |
| 335 | + }, |
| 336 | + mutations: { |
| 337 | + inc: state => state.count++, |
| 338 | + dec: state => state.count-- |
| 339 | + }, |
| 340 | + actions: { |
| 341 | + actionA, |
| 342 | + actionB |
| 343 | + } |
| 344 | + } |
| 345 | + } |
| 346 | + }) |
| 347 | + const { |
| 348 | + mapState, |
| 349 | + mapGetters, |
| 350 | + mapMutations, |
| 351 | + mapActions |
| 352 | + } = createNamespacedHelpers('foo/') |
| 353 | + const vm = new Vue({ |
| 354 | + store, |
| 355 | + computed: { |
| 356 | + ...mapState(['count']), |
| 357 | + ...mapGetters(['isEven']) |
| 358 | + }, |
| 359 | + methods: { |
| 360 | + ...mapMutations(['inc', 'dec']), |
| 361 | + ...mapActions(['actionA', 'actionB']) |
| 362 | + } |
| 363 | + }) |
| 364 | + expect(vm.count).toBe(0) |
| 365 | + expect(vm.isEven).toBe(true) |
| 366 | + store.state.foo.count++ |
| 367 | + expect(vm.count).toBe(1) |
| 368 | + expect(vm.isEven).toBe(false) |
| 369 | + vm.inc() |
| 370 | + expect(store.state.foo.count).toBe(2) |
| 371 | + expect(store.getters['foo/isEven']).toBe(true) |
| 372 | + vm.dec() |
| 373 | + expect(store.state.foo.count).toBe(1) |
| 374 | + expect(store.getters['foo/isEven']).toBe(false) |
| 375 | + vm.actionA() |
| 376 | + expect(actionA).toHaveBeenCalled() |
| 377 | + expect(actionB).not.toHaveBeenCalled() |
| 378 | + vm.actionB() |
| 379 | + expect(actionB).toHaveBeenCalled() |
| 380 | + }) |
324 | 381 | }) |
0 commit comments