Skip to content

Commit

Permalink
fix: support nested actions and mutations
Browse files Browse the repository at this point in the history
Fix #7
  • Loading branch information
ronnievdc authored and posva committed Jan 9, 2019
1 parent ad914e0 commit 113ed22
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ exports.Store = class Store {

return {
context: {
dispatch: (...args) => {
args[0] = key + args[0]
this.dispatch(...args)
},
commit: (...args) => {
args[0] = key + args[0]
this.commit(...args)
},
// make sure we reuse this proxy
_modulesNamespaceMap: this._modulesNamespaceMap,
// pass the right state
Expand Down
34 changes: 22 additions & 12 deletions tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,20 @@ describe('Store Mock', () => {
})

describe('using Helper', () => {
const mocks = {
$store: new Store({
state: {
counter: 0,
module: { mCounter: 1, nested: { mmCounter: 3 } },
},
const store = new Store({
state: {
counter: 0,
module: { mCounter: 1, nested: { mmCounter: 3 } },
},

getters: {
getter: 1,
// getter: (...args) => (console.log(...args), 1),
'mdolue/mGetter': () => 2,
},
}),
getters: {
getter: 1,
// getter: (...args) => (console.log(...args), 1),
'mdolue/mGetter': () => 2,
},
})
const mocks = {
$store: store,
}

it('works', () => {
Expand All @@ -83,6 +84,15 @@ describe('Store Mock', () => {
expect(wrapper.vm.mmCounter).toBe(3)

expect(wrapper.vm.getter).toBe(1)

wrapper.vm.action('a')
expect(store.dispatch).toHaveBeenCalledWith('action', 'a')
wrapper.vm.mAction('b')
expect(store.dispatch).toHaveBeenCalledWith('module/mAction', 'b')
wrapper.vm.mutation('c')
expect(store.commit).toHaveBeenCalledWith('mutation', 'c')
wrapper.vm.mMutation('d')
expect(store.commit).toHaveBeenCalledWith('module/mMutation', 'd')
})

it('throws with non-defined state', () => {
Expand Down

0 comments on commit 113ed22

Please sign in to comment.