[IMP] store: dispatch method returns data from action

This commit is contained in:
Alexandre Kühn
2019-08-27 10:18:31 +02:00
committed by Géry Debongnie
parent a3d563ebb4
commit 10cfe0b740
2 changed files with 42 additions and 3 deletions
+2 -3
View File
@@ -39,9 +39,8 @@ export class ConnectedComponent<T extends Env, P, S> extends Component<T, P, S>
return {}; return {};
} }
dispatch() { dispatch(name, ...payload) {
const [name, ...payload] = arguments; return (this.__owl__ as any).store.dispatch(name, ...payload);
(this.__owl__ as any).store.dispatch(name, ...payload);
} }
/** /**
+40
View File
@@ -1053,4 +1053,44 @@ describe("connected components and default values", () => {
expect(fixture.innerHTML).toBe("<div></div>"); expect(fixture.innerHTML).toBe("<div></div>");
expect(steps).toEqual(["on:update", "off:update"]); expect(steps).toEqual(["on:update", "off:update"]);
}); });
test("dispatch an action", async () => {
env.qweb.addTemplates(`
<templates>
<div t-name="App">
<t t-esc="storeProps.counter"/>
</div>
</templates>
`);
class App extends ConnectedComponent<any, any, any> {
static mapStoreToProps = function(state) {
return {
counter: state.counter
};
};
}
const state = {
counter: 0
};
const actions = {
inc({ state }) {
return ++state.counter;
}
};
const store = new Store({ state, actions });
(<any>env).store = store;
const app = new App(env);
await app.mount(fixture);
expect(fixture.innerHTML).toBe("<div>0</div>");
const res = app.dispatch('inc');
expect(res).toBe(1);
await nextTick();
expect(fixture.innerHTML).toBe("<div>1</div>");
});
}); });