Skip to content

Commit 9b3b766

Browse files
committed
allow setting a displayName for wrapper component created in renderHook
1 parent 9fc6a75 commit 9b3b766

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/__tests__/renderHook.js

+21
Original file line numberDiff line numberDiff 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+
5374
test('allows wrapper components', async () => {
5475
const Context = React.createContext('default')
5576
function Wrapper({children}) {

src/pure.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ function cleanup() {
318318
}
319319

320320
function 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,

types/index.d.ts

+2-1
Original file line numberDiff line numberDiff 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
/**

0 commit comments

Comments
 (0)