Skip to content

renderHook support for multiple parameters #1350

Open
@danielrentz

Description

@danielrentz

Describe the feature you'd like:

Testing hooks should support hooks with multiple parameters. Currently, the option "initialProps" of renderHook, and the "rerender" function it returns are supporting only a single parameter.

Suggested implementation:

Obviously, for backwards compatibility, the option "initialProps" cannot be changed. Therefore, I suggest to add a new option "initialArgs" that is an array and renders the hook to be tested by spreading that array.

The function "rerender" returned from "renderHook" will take multiple parameters and forward them to the hook as well.

import useHook from "..."

const { result, rerender } = renderHook(useHook, { initialArgs: [1, 2, 3] })
rerender(3, 4, 5)

It should be an error (exception) to pass both options "initialProps" and "initialArgs" at the same time.

Describe alternatives you've considered:

As a workaround, I currently wrap my multi-arg hooks in a wrapper hook taking an array as "props", but that is cumbersome:

import useHook from "..."

function useTestHook(args) {
    return useHook(...args)
}

const { result, rerender } = renderHook(useTestHook, { initialProps: [1, 2, 3] })
rerender([3, 4, 5])

Teachability, Documentation, Adoption, Migration Strategy:

Activity

changed the title [-]renderHook support for multple parameters[/-] [+]renderHook support for multiple parameters[/+] on Aug 22, 2024
amanreddy77

amanreddy77 commented on Oct 14, 2024

@amanreddy77

hey @danielrentz can I take this up?

danielrentz

danielrentz commented on Oct 15, 2024

@danielrentz
Author

@amanreddy77 Sure! I did not start to prepare something by myself yet.

imevanc

imevanc commented on Mar 9, 2025

@imevanc

@danielrentz - is anyone working on this?

danielrentz

danielrentz commented on Mar 10, 2025

@danielrentz
Author

I hoped @amanreddy77 was... Otherwise I will try by myself.

imevanc

imevanc commented on Mar 10, 2025

@imevanc

@danielrentz - happy with me to give it a go?

danielrentz

danielrentz commented on Mar 11, 2025

@danielrentz
Author

@imevanc Sorry what do you mean exactly, do you want to start working on this, or do you want me to start? :)

imevanc

imevanc commented on Mar 11, 2025

@imevanc

I'd like to start working on this, @danielrentz

danielrentz

danielrentz commented on Mar 11, 2025

@danielrentz
Author

Nice, thanks!

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

      Participants

      @danielrentz@imevanc@amanreddy77

      Issue actions

        renderHook support for multiple parameters · Issue #1350 · testing-library/react-testing-library