Skip to content

Commit cb4d8e1

Browse files
Throw error when called hook without provider (cerebral#354)
Throw error when called hook without provider
2 parents 36d47e2 + 721127c commit cb4d8e1

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

packages/node_modules/overmind-react/src/index.test.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,35 @@ describe('React', () => {
367367
expect(renderCount).toBe(1)
368368
expect(tree).toMatchSnapshot()
369369
})
370+
test('should throw an error without provider', () => {
371+
expect.assertions(1)
372+
373+
const config = {
374+
state: {
375+
foo: 'bar',
376+
},
377+
}
378+
379+
type IConfig = {
380+
state: {
381+
foo: typeof config.state.foo
382+
}
383+
}
384+
385+
const useApp = createHook<IConfig>();
386+
387+
const FooComponent = () => {
388+
const { state } = useApp();
389+
390+
return <h1>{state.foo}</h1>
391+
}
392+
393+
expect(() => {
394+
renderer
395+
.create(
396+
<FooComponent />
397+
)
398+
.toJSON()
399+
}).toThrow(Error)
400+
})
370401
})

packages/node_modules/overmind-react/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export const createHook = <Config extends IConfiguration>(): (() => {
9797
return () => {
9898
const overmind = react.useContext(context) as Overmind<Config>
9999

100-
if (!overmind) {
100+
if (!(overmind as any).mode) {
101101
throwMissingContextError()
102102
}
103103

0 commit comments

Comments
 (0)