Skip to content

Commit 909f4cb

Browse files
committed
Replaced wrap function with specific context provider helper
1 parent 9f5cbb4 commit 909f4cb

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

src/useHook.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@ import invariant from 'invariant'
44
import uuid from 'uuid-v4'
55

66
const use = (hook, context) => (props) => {
7-
87
const HookHarness = (props) => {
98
context.result = hook(props)
109
return <div data-testid={context.id} />
1110
}
1211

13-
const { queryByTestId, rerender } = render(context.resolveComponent({ children: <HookHarness {...props} /> }))
12+
const { queryByTestId, rerender } = render(context.resolveComponent(<HookHarness {...props} />))
1413

1514
const container = queryByTestId(context.id)
1615

17-
invariant(container !== null, 'You must render children when wrapping the hook')
16+
invariant(container !== null, 'Failed to render wrapper component')
1817

19-
context.rerender = (newProps) => rerender(context.resolveComponent({ children: <HookHarness {...newProps} /> }))
18+
context.rerender = (newProps) => rerender(context.resolveComponent(<HookHarness {...newProps} />))
2019
context.flushEffects = () => context.rerender(props)
2120

22-
return context.result
21+
return context.result
2322
}
2423

2524
const update = (context) => (props) => {
@@ -38,25 +37,29 @@ const flushEffects = (context) => () => {
3837
return context.result
3938
}
4039

41-
const wrap = (hook, context) => (wrap) => {
42-
invariant(typeof wrap === 'function', 'wrap must be provided a function')
40+
const withContextProvider = (hook, context) => (ContextProvider, props) => {
4341
const { resolveComponent } = context
44-
return useHookAdvanced(hook, { ...context, resolveComponent: (props) => wrap({ children: resolveComponent(props) }) })
42+
return useHookAdvanced(hook, {
43+
...context,
44+
resolveComponent: (Component) => (
45+
<ContextProvider {...props}>{resolveComponent(Component)}</ContextProvider>
46+
)
47+
})
4548
}
4649

4750
const useHookAdvanced = (hook, context) => {
4851
return {
4952
use: use(hook, context),
53+
withContextProvider: withContextProvider(hook, context),
5054
update: update(context),
51-
flushEffects: flushEffects(context),
52-
wrap: wrap(hook, context)
55+
flushEffects: flushEffects(context)
5356
}
5457
}
5558

5659
export const useHook = (hook) => {
5760
const context = {
5861
id: uuid(),
59-
resolveComponent: ({ children }) => children
62+
resolveComponent: (Component) => Component
6063
}
6164
return useHookAdvanced(hook, context)
6265
}

test/useHook.test.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,30 @@ describe('useHook tests', () => {
2121
test('should handle useContext hooks', () => {
2222
const TestContext = createContext('foo')
2323

24-
const { use } = useHook(() => useContext(TestContext)).wrap(({ children }) => (
25-
<TestContext.Provider value='bar'>
26-
{children}
27-
</TestContext.Provider>
28-
))
24+
const { use } = useHook(() => useContext(TestContext)).withContextProvider(
25+
TestContext.Provider,
26+
{ value: 'bar' }
27+
)
2928

3029
const value = use()
3130

3231
expect(value).toBe('bar')
3332
})
3433

3534
test('should handle useEffect hooks', () => {
36-
3735
const sideEffect = { [1]: false, [2]: false }
38-
39-
const { use, flushEffects, update } = useHook(({ id }) => useEffect(() => {
40-
sideEffect[id] = true
41-
return () => {
42-
sideEffect[id] = false
43-
}
44-
}, [id]))
36+
37+
const { use, flushEffects, update } = useHook(({ id }) =>
38+
useEffect(
39+
() => {
40+
sideEffect[id] = true
41+
return () => {
42+
sideEffect[id] = false
43+
}
44+
},
45+
[id]
46+
)
47+
)
4548

4649
use({ id: 1 })
4750

0 commit comments

Comments
 (0)