@@ -4,22 +4,21 @@ import invariant from 'invariant'
4
4
import uuid from 'uuid-v4'
5
5
6
6
const use = ( hook , context ) => ( props ) => {
7
-
8
7
const HookHarness = ( props ) => {
9
8
context . result = hook ( props )
10
9
return < div data-testid = { context . id } />
11
10
}
12
11
13
- const { queryByTestId, rerender } = render ( context . resolveComponent ( { children : < HookHarness { ...props } /> } ) )
12
+ const { queryByTestId, rerender } = render ( context . resolveComponent ( < HookHarness { ...props } /> ) )
14
13
15
14
const container = queryByTestId ( context . id )
16
15
17
- invariant ( container !== null , 'You must render children when wrapping the hook ' )
16
+ invariant ( container !== null , 'Failed to render wrapper component ' )
18
17
19
- context . rerender = ( newProps ) => rerender ( context . resolveComponent ( { children : < HookHarness { ...newProps } /> } ) )
18
+ context . rerender = ( newProps ) => rerender ( context . resolveComponent ( < HookHarness { ...newProps } /> ) )
20
19
context . flushEffects = ( ) => context . rerender ( props )
21
20
22
- return context . result
21
+ return context . result
23
22
}
24
23
25
24
const update = ( context ) => ( props ) => {
@@ -38,25 +37,29 @@ const flushEffects = (context) => () => {
38
37
return context . result
39
38
}
40
39
41
- const wrap = ( hook , context ) => ( wrap ) => {
42
- invariant ( typeof wrap === 'function' , 'wrap must be provided a function' )
40
+ const withContextProvider = ( hook , context ) => ( ContextProvider , props ) => {
43
41
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
+ } )
45
48
}
46
49
47
50
const useHookAdvanced = ( hook , context ) => {
48
51
return {
49
52
use : use ( hook , context ) ,
53
+ withContextProvider : withContextProvider ( hook , context ) ,
50
54
update : update ( context ) ,
51
- flushEffects : flushEffects ( context ) ,
52
- wrap : wrap ( hook , context )
55
+ flushEffects : flushEffects ( context )
53
56
}
54
57
}
55
58
56
59
export const useHook = ( hook ) => {
57
60
const context = {
58
61
id : uuid ( ) ,
59
- resolveComponent : ( { children } ) => children
62
+ resolveComponent : ( Component ) => Component
60
63
}
61
64
return useHookAdvanced ( hook , context )
62
65
}
0 commit comments