React testing library renderHook
renderHook
is a convenience wrapper for testing hooks.
This is so that you don't need to make dummy components for testing hooks.
It takes in a callback which calls your hook, and returns a custom RenderHookResult.
The RenderHookResult contains a result
key, which is a ref of the result of your hook call.
tsx
// Yoinked from the documentation
import { renderHook } from '@testing-library/react'
test('returns logged in user', () => {
const { result, rerender } = renderHook((props = {}) => props, {
initialProps: { name: 'Alice' },
});
expect(result.current).toEqual({ name: 'Alice' });
rerender();
expect(result.current).toEqual({ name: undefined });
})