From ab08e84bffded8b34fca9aad022e3d219d556f6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20K=C3=BChn?= Date: Mon, 19 Aug 2019 15:42:18 +0200 Subject: [PATCH] [IMP] store: return data from dispatching an action --- src/store/store.ts | 7 +------ tests/store/store.test.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/store/store.ts b/src/store/store.ts index dcd58849..c5ba3f50 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -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 { diff --git a/tests/store/store.test.ts b/tests/store/store.test.ts index 128de616..28f125f2 100644 --- a/tests/store/store.test.ts +++ b/tests/store/store.test.ts @@ -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 = {};