[IMP] store: return data from dispatching an action

This commit is contained in:
Alexandre Kühn
2019-08-19 15:42:18 +02:00
committed by Géry Debongnie
parent cd9cac1246
commit ab08e84bff
2 changed files with 31 additions and 6 deletions
+1 -6
View File
@@ -99,12 +99,7 @@ export class Store extends EventBus {
},
...payload
);
if (result instanceof Promise) {
return new Promise((resolve, reject) => {
result.then(() => resolve());
result.catch(reject);
});
}
return result;
}
commit(type: string, ...payload: any): any {
+30
View File
@@ -160,6 +160,36 @@ describe("basic use", () => {
expect(store.state.n).toBe(13);
});
test("return data from dispatching an action", async () => {
const state = { n: 1 };
const mutations = {
inc({ state }, delta) {
state.n += delta;
},
setN({ state }, n) {
state.n = n;
}
};
const actions = {
async dosomething({ commit, dispatch }) {
const val = await dispatch("setTo10");
commit("inc", val);
},
async setTo10({ commit }) {
await Promise.resolve();
commit("setN", 10);
return 5;
}
};
const store = new Store({ state, mutations, actions });
expect(store.state.n).toBe(1);
store.dispatch("dosomething");
expect(store.state.n).toBe(1);
await nextTick();
expect(store.state.n).toBe(15);
});
test("env is given to actions", () => {
expect.assertions(1);
const someEnv = <Env>{};