Skip to content

Allow setting a displayName for wrapper component created in renderHook #1396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/__tests__/renderHook.js
Original file line number Diff line number Diff line change
@@ -50,6 +50,27 @@ test('allows rerendering', () => {
expect(result.current).toEqual(['right', expect.any(Function)])
})

test('allows setting a displayName', () => {
let capturedElement = null

const spyWrapper = ({children}) => {
// Capture the hook element React creates
capturedElement = React.Children.only(children)
return <>{children}</>
}

const useMyLocalHook = jest.fn()

renderHook(useMyLocalHook, {
wrapper: spyWrapper,
displayName: 'CustomHookDisplayName',
})

expect(useMyLocalHook).toHaveBeenCalledTimes(1)

expect(capturedElement?.type?.displayName).toBe('CustomHookDisplayName')
})

test('allows wrapper components', async () => {
const Context = React.createContext('default')
function Wrapper({children}) {
6 changes: 5 additions & 1 deletion src/pure.js
Original file line number Diff line number Diff line change
@@ -318,7 +318,7 @@ function cleanup() {
}

function renderHook(renderCallback, options = {}) {
const {initialProps, ...renderOptions} = options
const {initialProps, displayName, ...renderOptions} = options

if (renderOptions.legacyRoot && typeof ReactDOM.render !== 'function') {
const error = new Error(
@@ -342,6 +342,10 @@ function renderHook(renderCallback, options = {}) {
return null
}

if (displayName !== undefined) {
TestComponent.displayName = displayName
}

const {rerender: baseRerender, unmount} = render(
<TestComponent renderCallbackProps={initialProps} />,
renderOptions,
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -254,6 +254,7 @@ export interface RenderHookOptions<
* to use the rerender utility to change the values passed to your hook.
*/
initialProps?: Props | undefined
displayName?: React.FunctionComponent['displayName']
}

/**