File tree Expand file tree Collapse file tree 3 files changed +28
-2
lines changed Expand file tree Collapse file tree 3 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -50,6 +50,27 @@ test('allows rerendering', () => {
5050 expect ( result . current ) . toEqual ( [ 'right' , expect . any ( Function ) ] )
5151} )
5252
53+ test ( 'allows setting a displayName' , ( ) => {
54+ let capturedElement = null
55+
56+ const spyWrapper = ( { children } ) => {
57+ // Capture the hook element React creates
58+ capturedElement = React . Children . only ( children ) ;
59+ return < > { children } </ > ;
60+ }
61+
62+ const useMyLocalHook = jest . fn ( )
63+
64+ renderHook ( useMyLocalHook , {
65+ wrapper : spyWrapper ,
66+ displayName : 'CustomHookDisplayName' ,
67+ } )
68+
69+ expect ( useMyLocalHook ) . toHaveBeenCalledTimes ( 1 )
70+
71+ expect ( capturedElement ?. type ?. displayName ) . toBe ( 'CustomHookDisplayName' )
72+ } ) ;
73+
5374test ( 'allows wrapper components' , async ( ) => {
5475 const Context = React . createContext ( 'default' )
5576 function Wrapper ( { children} ) {
Original file line number Diff line number Diff line change @@ -318,7 +318,7 @@ function cleanup() {
318318}
319319
320320function renderHook ( renderCallback , options = { } ) {
321- const { initialProps, ...renderOptions } = options
321+ const { initialProps, displayName , ...renderOptions } = options
322322
323323 if ( renderOptions . legacyRoot && typeof ReactDOM . render !== 'function' ) {
324324 const error = new Error (
@@ -342,6 +342,10 @@ function renderHook(renderCallback, options = {}) {
342342 return null
343343 }
344344
345+ if ( displayName !== undefined ) {
346+ TestComponent . displayName = displayName ;
347+ }
348+
345349 const { rerender : baseRerender , unmount} = render (
346350 < TestComponent renderCallbackProps = { initialProps } /> ,
347351 renderOptions ,
Original file line number Diff line number Diff line change @@ -253,7 +253,8 @@ export interface RenderHookOptions<
253253 * The argument passed to the renderHook callback. Can be useful if you plan
254254 * to use the rerender utility to change the values passed to your hook.
255255 */
256- initialProps ?: Props | undefined
256+ initialProps ?: Props | undefined ,
257+ displayName ?: React . FunctionComponent [ 'displayName' ] ,
257258}
258259
259260/**
You can’t perform that action at this time.
0 commit comments