Git Product home page Git Product logo

Comments (9)

cmorten avatar cmorten commented on June 12, 2024 4

Update: Ah I was too slow, as you’ve posted!


The intended pattern is:

const { result, rerender } = renderHook((props = {}) => useMyHook(props), {
  initialProps,
});

expect(result.current).toEqual(firstResult);

rerender(newProps);

expect(result.current).toEqual(secondResult)

There should be no need to workaround - the key is to not hardcode the initialProps being passed to your hook, but instead allow props to be passed to the renderHook callback so passing values into rerender works as expected.

See the example in https://testing-library.com/docs/react-testing-library/api/#renderhook-options-initialprops, though appreciate it perhaps isn’t the most clear as the “hook” in the example is the identity function of reflecting the props back rather than something explicitly use* as most folks will implement.

from react-hooks-testing-library.

wellermiranda avatar wellermiranda commented on June 12, 2024 3

That worked for me:

const { rerender } = renderHook(useOverview, { initialProps });

from react-hooks-testing-library.

cmorten avatar cmorten commented on June 12, 2024 2

Care needed to ensure that your renderHook is configured such that it knows what to do with the new props you pass into the rerender call.

E.g. something along the lines of (not tested)…

  it('triggers a change', () => {
    const initialProps = {
      condition: false,
      ref: {
        current: {
          focus: vi.fn(),
        },
      },
    };

    const { rerender } = renderHook(({ condition, ref }) => useTriggerFocus({ condition, ref }), { initialProps });

    rerender({ condition: true, ref: undefined });
  });
});

See the docs https://testing-library.com/docs/react-testing-library/api#rerender-1 for the syntax on how to make sure new props are used. Here I’ve also made use of the initialProps option https://testing-library.com/docs/react-testing-library/api#renderhook-options-initialprops.

At the moment your props passed to the rerender and being ignored as the renderHook callback is hardcoded to use those you defined at the beginning of your test.

P.S. your future queries for React Testing Library may be better placed in https://github.com/testing-library/react-testing-library or the discord than here as this is the Dom Testing Library repo! 🙃

P.P.S. take care when using a ref with useEffect - in a lot of use cases it doesn’t necessarily work, see https://epicreact.dev/why-you-shouldnt-put-refs-in-a-dependency-array/

from react-hooks-testing-library.

timdeschryver avatar timdeschryver commented on June 12, 2024

This seems like a React Hooks Testing Library issue, I'm transferring this issue to the correct repository.

from react-hooks-testing-library.

alextrastero avatar alextrastero commented on June 12, 2024

Thanks @timdeschryver , do you have a link for that?

from react-hooks-testing-library.

timdeschryver avatar timdeschryver commented on June 12, 2024

@alextrastero you don't have to take any action, we've already transferred it to the correct repository.
You can find it here, https://github.com/testing-library/react-hooks-testing-library

from react-hooks-testing-library.

mpeyper avatar mpeyper commented on June 12, 2024

Hey, sorry I missed the notification that this one was transferred here.

Sorry to play ping pong again with your issue, but can you confirm if you are using renderHook from @testing-library/react-hooks (pre React 18, this repo) or @testing-library/react (React 18, RTL repo).

If it’s the latter, I’ll transfer it to the RTL repo.

Edit: quickly looking at the issue as if it was for our version, I’d say the solution presented by @cmorten above is correct.

from react-hooks-testing-library.

wellermiranda avatar wellermiranda commented on June 12, 2024

Thanks, @cmorten.

from react-hooks-testing-library.

NotNikita avatar NotNikita commented on June 12, 2024

Thanks, @cmorten.

from react-hooks-testing-library.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.