File tree 3 files changed +28
-2
lines changed
3 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -50,6 +50,27 @@ test('allows rerendering', () => {
50
50
expect ( result . current ) . toEqual ( [ 'right' , expect . any ( Function ) ] )
51
51
} )
52
52
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
+
53
74
test ( 'allows wrapper components' , async ( ) => {
54
75
const Context = React . createContext ( 'default' )
55
76
function Wrapper ( { children} ) {
Original file line number Diff line number Diff line change @@ -318,7 +318,7 @@ function cleanup() {
318
318
}
319
319
320
320
function renderHook ( renderCallback , options = { } ) {
321
- const { initialProps, ...renderOptions } = options
321
+ const { initialProps, displayName , ...renderOptions } = options
322
322
323
323
if ( renderOptions . legacyRoot && typeof ReactDOM . render !== 'function' ) {
324
324
const error = new Error (
@@ -342,6 +342,10 @@ function renderHook(renderCallback, options = {}) {
342
342
return null
343
343
}
344
344
345
+ if ( displayName !== undefined ) {
346
+ TestComponent . displayName = displayName ;
347
+ }
348
+
345
349
const { rerender : baseRerender , unmount} = render (
346
350
< TestComponent renderCallbackProps = { initialProps } /> ,
347
351
renderOptions ,
Original file line number Diff line number Diff line change @@ -253,7 +253,8 @@ export interface RenderHookOptions<
253
253
* The argument passed to the renderHook callback. Can be useful if you plan
254
254
* to use the rerender utility to change the values passed to your hook.
255
255
*/
256
- initialProps ?: Props | undefined
256
+ initialProps ?: Props | undefined ,
257
+ displayName ?: React . FunctionComponent [ 'displayName' ] ,
257
258
}
258
259
259
260
/**
You can’t perform that action at this time.
0 commit comments