File tree 3 files changed +35
-6
lines changed
3 files changed +35
-6
lines changed Original file line number Diff line number Diff line change 1
1
import React , { useState , useEffect } from 'react'
2
2
import 'jest-dom/extend-expect'
3
- import { testHook , cleanup } from '../'
3
+ import { testHook , cleanup , act } from '../'
4
4
5
5
afterEach ( cleanup )
6
6
@@ -60,3 +60,18 @@ test('accepts wrapper option to wrap rendered hook with', () => {
60
60
)
61
61
expect ( actual ) . toBe ( 12 )
62
62
} )
63
+ test ( 'returns result ref with latest result from hook execution' , ( ) => {
64
+ function useCounter ( { initialCount = 0 , step = 1 } = { } ) {
65
+ const [ count , setCount ] = React . useState ( initialCount )
66
+ const increment = ( ) => setCount ( c => c + step )
67
+ const decrement = ( ) => setCount ( c => c - step )
68
+ return { count, increment, decrement}
69
+ }
70
+
71
+ const { result} = testHook ( useCounter )
72
+ expect ( result . current . count ) . toBe ( 0 )
73
+ act ( ( ) => {
74
+ result . current . increment ( )
75
+ } )
76
+ expect ( result . current . count ) . toBe ( 1 )
77
+ } )
Original file line number Diff line number Diff line change @@ -61,21 +61,31 @@ function render(
61
61
}
62
62
}
63
63
64
- function TestHook ( { callback} ) {
65
- callback ( )
64
+ function TestHook ( { callback, children } ) {
65
+ children ( callback ( ) )
66
66
return null
67
67
}
68
68
69
69
function testHook ( callback , options = { } ) {
70
+ const result = {
71
+ current : null ,
72
+ }
70
73
const toRender = ( ) => {
71
- const hookRender = < TestHook callback = { callback } />
74
+ const hookRender = (
75
+ < TestHook callback = { callback } >
76
+ { res => {
77
+ result . current = res
78
+ } }
79
+ </ TestHook >
80
+ )
72
81
if ( options . wrapper ) {
73
82
return React . createElement ( options . wrapper , null , hookRender )
74
83
}
75
84
return hookRender
76
85
}
77
86
const { unmount, rerender : rerenderComponent } = render ( toRender ( ) )
78
87
return {
88
+ result,
79
89
unmount,
80
90
rerender : ( ) => {
81
91
rerenderComponent ( toRender ( ) )
Original file line number Diff line number Diff line change @@ -19,7 +19,8 @@ export type RenderResult<Q extends Queries = typeof queries> = {
19
19
asFragment : ( ) => DocumentFragment
20
20
} & { [ P in keyof Q ] : BoundFunction < Q [ P ] > }
21
21
22
- export type HookResult = {
22
+ export type HookResult < TResult > = {
23
+ result : React . MutableRefObject < TResult >
23
24
rerender : ( ) => void
24
25
unmount : ( ) => boolean
25
26
}
@@ -52,7 +53,10 @@ export function render<Q extends Queries>(
52
53
/**
53
54
* Renders a test component that calls back to the test.
54
55
*/
55
- export function testHook ( callback : ( ) => void , options ?: Partial < HookOptions > ) : HookResult
56
+ export function testHook < T > (
57
+ callback : ( ) => T ,
58
+ options ?: Partial < HookOptions > ,
59
+ ) : HookResult < T >
56
60
57
61
/**
58
62
* Unmounts React trees that were mounted with render.
You can’t perform that action at this time.
0 commit comments