File tree Expand file tree Collapse file tree 3 files changed +35
-6
lines changed Expand file tree Collapse file tree 3 files changed +35
-6
lines changed Original file line number Diff line number Diff line change 11import React , { useState , useEffect } from 'react'
22import 'jest-dom/extend-expect'
3- import { testHook , cleanup } from '../'
3+ import { testHook , cleanup , act } from '../'
44
55afterEach ( cleanup )
66
@@ -60,3 +60,18 @@ test('accepts wrapper option to wrap rendered hook with', () => {
6060 )
6161 expect ( actual ) . toBe ( 12 )
6262} )
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(
6161 }
6262}
6363
64- function TestHook ( { callback} ) {
65- callback ( )
64+ function TestHook ( { callback, children } ) {
65+ children ( callback ( ) )
6666 return null
6767}
6868
6969function testHook ( callback , options = { } ) {
70+ const result = {
71+ current : null ,
72+ }
7073 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+ )
7281 if ( options . wrapper ) {
7382 return React . createElement ( options . wrapper , null , hookRender )
7483 }
7584 return hookRender
7685 }
7786 const { unmount, rerender : rerenderComponent } = render ( toRender ( ) )
7887 return {
88+ result,
7989 unmount,
8090 rerender : ( ) => {
8191 rerenderComponent ( toRender ( ) )
Original file line number Diff line number Diff line change @@ -19,7 +19,8 @@ export type RenderResult<Q extends Queries = typeof queries> = {
1919 asFragment : ( ) => DocumentFragment
2020} & { [ P in keyof Q ] : BoundFunction < Q [ P ] > }
2121
22- export type HookResult = {
22+ export type HookResult < TResult > = {
23+ result : React . MutableRefObject < TResult >
2324 rerender : ( ) => void
2425 unmount : ( ) => boolean
2526}
@@ -52,7 +53,10 @@ export function render<Q extends Queries>(
5253/**
5354 * Renders a test component that calls back to the test.
5455 */
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 >
5660
5761/**
5862 * Unmounts React trees that were mounted with render.
You can’t perform that action at this time.
0 commit comments