imp: give reference to dispatch function in actions

This commit is contained in:
Alexandre Kühn
2019-04-08 22:58:45 +02:00
committed by Géry Debongnie
parent 7027ff1eb6
commit 3340401f54
2 changed files with 25 additions and 2 deletions
+3 -2
View File
@@ -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
);
+22
View File
@@ -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 = {};