Skip to content

Latest commit

 

History

History
76 lines (49 loc) · 2.21 KB

File metadata and controls

76 lines (49 loc) · 2.21 KB

checks version GitHub top language license npm

ko-fi

react-render-hook

A small utility function to quickly test React hooks.

Installation

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

Content

renderHook

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.

createRenderHook

If needed, you can pass custom renderOptions to the createRenderHook.

Returns a new renderHook function.

License

This project is available under the MIT license. See LICENSE.txt for details.