A small utility function to quickly test React hooks.
Add react-render-hook
in your dependencies:
yarn add @seaofvoices/react-render-hook
Or if you are using npm
:
npm install @seaofvoices/react-render-hook
function renderHook(hook, ...): {
result: Ref,
rerender: (...) -> (),
unmount: () -> ()
}
A function that takes a hook and its initial arguments and returns a result
ref, a function to re-render and a function to unmount.
In a jest test, it looks like this:
local function useCustomHook(input: string): string
return input .. " " .. input
end
it('returns the initial value after the first re-render', function()
local renderResult = renderHook(useCustomHook, 'text')
expect(renderResult.result.current).toBe("text text")
renderResult.rerender('bye')
expect(renderResult.result.current).toBe('bye bye')
end)
Note about multiple returned values: if the hook being rendered returns more than one value, those will be packed with table.pack
. This means that the renderResult.result.current
will contain an array.
If needed, you can pass custom renderOptions
to the createRenderHook
.
Returns a new renderHook
function.
This project is available under the MIT license. See LICENSE.txt for details.