diff --git a/src/store.ts b/src/store.ts index a03e314b..ef3a81fb 100644 --- a/src/store.ts +++ b/src/store.ts @@ -91,8 +91,9 @@ export class Store extends EventBus { this.actions[action]( { commit: this.commit.bind(this), - state: this.state, - env: this.env + dispatch: this.dispatch.bind(this), + env: this.env, + state: this.state }, payload ); diff --git a/tests/store.test.ts b/tests/store.test.ts index ac6be5fb..84e163a2 100644 --- a/tests/store.test.ts +++ b/tests/store.test.ts @@ -41,6 +41,28 @@ describe("basic use", () => { expect(store.state.n).toBe(15); }); + test("can dispatch an action in an action", () => { + const state = { n: 1 }; + const mutations = { + inc(state, delta) { + state.n += delta; + } + }; + const actions = { + inc({ commit }, delta) { + commit("inc", delta); + }, + inc100({ dispatch }) { + dispatch("inc", 100); + } + }; + const store = new Store({ state, mutations, actions }); + + expect(store.state.n).toBe(1); + store.dispatch("inc100"); + expect(store.state.n).toBe(101); + }); + test("env is given to actions", () => { expect.assertions(1); const someEnv = {};