mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[IMP] store: display error if no store found
This commit is contained in:
@@ -84,6 +84,9 @@ const isStrictEqual = (a, b) => a === b;
|
|||||||
export function useStore(selector, options: SelectorOptions = {}): any {
|
export function useStore(selector, options: SelectorOptions = {}): any {
|
||||||
const component: Component<any, any> = Component.current!;
|
const component: Component<any, any> = Component.current!;
|
||||||
const store = options.store || (component.env.store as Store);
|
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);
|
let result = selector(store.state, component.props);
|
||||||
const hashFn = store.observer.revNumber.bind(store.observer);
|
const hashFn = store.observer.revNumber.bind(store.observer);
|
||||||
let revNumber = hashFn(result) || result;
|
let revNumber = hashFn(result) || result;
|
||||||
|
|||||||
@@ -48,6 +48,22 @@ describe("connecting a component to store", () => {
|
|||||||
expect(fixture.innerHTML).toBe("<div><span>hello</span></div>");
|
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 () => {
|
test("can use useStore twice in a component", async () => {
|
||||||
const state = { a: 1, b: 2 };
|
const state = { a: 1, b: 2 };
|
||||||
const actions = {
|
const actions = {
|
||||||
|
|||||||
Reference in New Issue
Block a user