From b211e751407d17e2934b191f868f6fbd8d704ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Sun, 3 Nov 2019 23:54:54 +0100 Subject: [PATCH] [IMP] store: display error if no store found --- src/store.ts | 3 +++ tests/store_hooks.test.ts | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/store.ts b/src/store.ts index 53de6779..ccb2a1e7 100644 --- a/src/store.ts +++ b/src/store.ts @@ -84,6 +84,9 @@ const isStrictEqual = (a, b) => a === b; export function useStore(selector, options: SelectorOptions = {}): any { const component: Component = 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; diff --git a/tests/store_hooks.test.ts b/tests/store_hooks.test.ts index 729dcc08..e2fc836c 100644 --- a/tests/store_hooks.test.ts +++ b/tests/store_hooks.test.ts @@ -48,6 +48,22 @@ describe("connecting a component to store", () => { expect(fixture.innerHTML).toBe("
hello
"); }); + test("throw error if no store is found", async () => { + class App extends Component { + static template = xml`
`; + 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 = {