-
Notifications
You must be signed in to change notification settings - Fork 233
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
[renderHook] rerender with new props, new props are not consumed #964
Comments
Care needed to ensure that your 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 At the moment your props passed to the 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 |
This seems like a React Hooks Testing Library issue, I'm transferring this issue to the correct repository. |
Thanks @timdeschryver , do you have a link for that? |
@alextrastero you don't have to take any action, we've already transferred it to the correct repository. |
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 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. |
That worked for me:
|
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 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 |
Thanks, @cmorten. |
1 similar comment
Thanks, @cmorten. |
Passing new props to
rerender
doesn't work, please let me know if i'm doing anything wrong, thxpackage.json
hook
test
console
The text was updated successfully, but these errors were encountered: