[IMP] store: display error if no store found

This commit is contained in:
Géry Debongnie
2019-11-03 23:54:54 +01:00
committed by aab-odoo
parent 8d1dd06340
commit b211e75140
2 changed files with 19 additions and 0 deletions
+3
View File
@@ -84,6 +84,9 @@ const isStrictEqual = (a, b) => a === b;
export function useStore(selector, options: SelectorOptions = {}): any {
const component: Component<any, any> = Component.current!;
const store = options.store || (component.env.store as Store);
if (!(store instanceof Store)) {
throw new Error(`No store found when connecting '${component.constructor.name}'`);
}
let result = selector(store.state, component.props);
const hashFn = store.observer.revNumber.bind(store.observer);
let revNumber = hashFn(result) || result;
+16
View File
@@ -48,6 +48,22 @@ describe("connecting a component to store", () => {
expect(fixture.innerHTML).toBe("<div><span>hello</span></div>");
});
test("throw error if no store is found", async () => {
class App extends Component<any, any> {
static template = xml`<div></div>`;
todos = useStore(state => state.todos);
}
let error;
try {
new App();
} catch (e) {
error = e;
}
expect(error).toBeDefined();
expect(error.message).toBe("No store found when connecting 'App'");
});
test("can use useStore twice in a component", async () => {
const state = { a: 1, b: 2 };
const actions = {