mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[IMP] store: return data from dispatching an action
This commit is contained in:
committed by
Géry Debongnie
parent
cd9cac1246
commit
ab08e84bff
+1
-6
@@ -99,12 +99,7 @@ export class Store extends EventBus {
|
|||||||
},
|
},
|
||||||
...payload
|
...payload
|
||||||
);
|
);
|
||||||
if (result instanceof Promise) {
|
return result;
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
result.then(() => resolve());
|
|
||||||
result.catch(reject);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
commit(type: string, ...payload: any): any {
|
commit(type: string, ...payload: any): any {
|
||||||
|
|||||||
@@ -160,6 +160,36 @@ describe("basic use", () => {
|
|||||||
expect(store.state.n).toBe(13);
|
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", () => {
|
test("env is given to actions", () => {
|
||||||
expect.assertions(1);
|
expect.assertions(1);
|
||||||
const someEnv = <Env>{};
|
const someEnv = <Env>{};
|
||||||
|
|||||||
Reference in New Issue
Block a user