问题
I got a test which calls renderHook
like this:
const { result } = renderHook(() => useMyHook(useDispatch()), wrapper);
It's all good and working, and I can get the return value in result.current
..
It seems like I can't call another hook with renderHook
.. If I do the exact same thing, then it says that result
is already defined. If I change the name to something else, like "res", it says that res is undefined, like it's only working for the specific name "result".
So how can I call it more than one time?
回答1:
Since you should only be testing one hook per test, perhaps you have to unmount the current component first:
const { result, unmount } = renderHook(() => useMyHook(useDispatch()), wrapper);
// ... do what you need with result
unmount();
// call your new hook...
const { result } = renderHook(() => useNewHook(useDispatch()), wrapper);
来源:https://stackoverflow.com/questions/59478806/get-renderhooks-result-more-than-once