-
Notifications
You must be signed in to change notification settings - Fork 232
How to use the rerender API ? #18
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
Comments
Hi @DavidBabel (great name for JS dev!), You're so close with your test. The issue is that the props are not passed through the the hook callback so it is using const hook = renderHook((isVisible) => useRenderedAt(isVisible), {
initialProps: false
}); allows the test to pass. We should document this |
Yeah that's why i was a bit confused about that. Thanks for your answer, it totally makes sense now. I did not have the energy to check in the code yesterday :p Great project anyway Edit: I actually reopened. Do i have to use const hook = renderHook(isVisible => useRenderedAt(isVisible), {
initialProps: false
});
hook.rerender(true);
const timestamp = hook.result.current;
expect(timestamp).not.toBe(Infinity);
hook.rerender(true);
expect(hook.result.current).toBe(timestamp); |
Ok i find out. So for googlers, the answer is no. Ne need for async since the hooks is synchrone. so this works perfectly : test('should return render time when viewable and never reupdate it', () => {
const hook = renderHook(isVisible => useRenderedAt(isVisible), {
initialProps: false
});
hook.rerender(true);
const timestamp = hook.result.current;
expect(timestamp).not.toBe(Infinity);
hook.rerender(true);
expect(hook.result.current).toBe(timestamp);
hook.rerender(false);
expect(hook.result.current).toBe(timestamp);
}); |
@DavidBabel thanks for writing this down. I find this rerender API not intuitive and complicated for something like that :( |
Glad it helps |
@goldo, I'd be interested in hearing your thoughts in #56 about why you find this rerender API not intuitive and complicated. @DavidBabel you're welcome to leave your thoughts too :) |
@mpeyper I think mainly because you have to recreate a function and map the params of it to the params of the function of your hook.
Here, I guess It would have been better to keep the exact same function signature for example ? |
Generally, and what the examples in the new draft docs have, I would use an object for the props so that you can name the value to add a bit of traceability to the value and it's usage test('useMyHook', () => {
const hook = renderHook(({ number }) => useMyHook({timeout, ref, number}), {
initialProps: { number: false }
});
hook.rerender({ number: 5 });
}); But I totally understand that this still is not as explicit as you might want. It's probably a case of it being apparent to me, the author of the library, but not so much for fresh eyes seeing the API for the first time.
You mean like test('useMyHook', () => {
const { rerender } = renderHook(() => useMyHook({timeout, ref, false}));
rerender(() => useMyHook({timeout, ref, 5}));
}); or test('useMyHook', () => {
renderHook(() => useMyHook({timeout, ref, false}));
renderHook(() => useMyHook({timeout, ref, 5}));
}); Or something else? |
something like this ? |
I would like to move this discussion to #56, but I'll answer here for continuity.
I'm not sure where if its: test('useMyHook', () => {
const timeout = 100 // or whatever
const ref = { current: '' } // or whatever
let number = false
const hook = renderHook(()=> useMyHook({ timeout, ref, number }));
number = 5
hook.rerender({ number });
}); Then passing the updated value to test('useMyHook', () => {
const timeout = 100 // or whatever
const ref = { current: '' } // or whatever
let number = false
const hook = renderHook(()=> useMyHook({ timeout, ref, number }));
number = 5
hook.rerender();
}); This would work with the current API. If the intention was something more like: test('useMyHook', () => {
const timeout = 100 // or whatever
const ref = { current: '' } // or whatever
const hook = renderHook(()=> useMyHook({ timeout, ref, number: false }));
hook.rerender({ number: 5 });
}); Then I don't think this is possible as the hook callback has no idea about the value passed to I still think I may be completely misunderstanding what you are saying though. |
@mpeyper sorry for the delay |
Hey guys,
I'm sorry, i'm not very confortable with other testing libraries, and i cannot get how to test my hooks with this library.
I have a very simple hook, which wait for visibility to store a timestamp of render time, which will never update after that :
I'm trying to test it this way :
What am i doing wrong ?
The text was updated successfully, but these errors were encountered: