renderHook unable to rerender props on the wrapper · Issue #1372 · testing-library/testing-library-docs · GitHub
Skip to content

renderHook unable to rerender props on the wrapper #1372

Open

Description

@DNR500

Describe the bug

Sorry I don't know if I should list this as a bug or as a feature request with the renderHook now being supported react hook testing library. It used to possible to test how context and hooks worked together with react-hooks-testing library - you could render a wrapper with and initial props and then pass new props to the wrapper using rerender.

Here is the example on react hooks testing library site

This was really useful as it allowed you to test how updates from your providers via context could effect the output from your hooks.

To Reproduce Steps to reproduce the behaviour:
This is the implementation as it was on react hooks testing library - I do understand how that how the initial props for the wrapper as changed but I'm using this more to draw attention to how rerender used to work.

import { renderHook, act } from '@testing-library/react-hooks'
import { CounterStepProvider, useCounter } from './counter'

test('should use custom step when incrementing', () => {
  const wrapper = ({ children, step }) => (
    <CounterStepProvider step={step}>{children}</CounterStepProvider>
  )
  const { result, rerender } = renderHook(() => useCounter(), {
    wrapper,
    initialProps: {
      step: 2
    }
  })

  act(() => {
    result.current.increment()
  })

  expect(result.current.count).toBe(2)

  /**
   * For react hook testing library this could be use to update the provider props and therefore affect a hooks return values via context
   * in react testing library these values seem to be just passed to the renderHook callback
   */
  rerender({ step: 8 })

  act(() => {
    result.current.increment()
  })

  expect(result.current.count).toBe(10)
})

Expected behaviour
There should be some way to rerender the wrapper for the hook you are testing with different prop values or maybe a documented example of how to test context and hooks in this way if it already exists. Is this possible with the current implementation?

Activity

  1. eps1lon commented on Feb 29, 2024

    @eps1lon
    Member

    It's not possible just like it's not possible to call render with props for the wrapper. The initialProps are passed to the render callback which we have documented in https://testing-library.com/docs/react-testing-library/api/#renderhook-options

    The naming of initialProps is unfortunate and we're open to better suggestions.

    renderHook is a convenience helper for the simple cases. For more involved cases, you're better of rewriting your test.

    It might even be better to not use renderHook at all and test the component using that hook. But it's hard to give clear directions since your example is trivial and I wouldn't even recommend testing this step counter hook which looks like a low-level utility that may be refactored at any time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions