testing-library / testing-library/testing-library-docs

renderHook unable to rerender props on the wrapper

Open
#1,372 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
479
Forks
740
PR merge metrics
No merged PRs in 30d

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the current renderHook wrapper and rerender behavior with the linked react-hooks-testing-library context example. Determine whether wrapper props can be updated or whether the documentation needs an alternative; done means a supported approach or a clear documented limitation and example.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
documentation
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.