Skip to content

Commit

Permalink
feat(renderHook): allow passing of all render options to renderHook (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
TkDodo authored Sep 4, 2022
1 parent 73ee9ba commit 27a9584
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
26 changes: 26 additions & 0 deletions src/__tests__/renderHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,29 @@ test('allows wrapper components', async () => {

expect(result.current).toEqual('provided')
})

test('legacyRoot uses legacy ReactDOM.render', () => {
jest.spyOn(console, 'error').mockImplementation(() => {})

const Context = React.createContext('default')
function Wrapper({children}) {
return <Context.Provider value="provided">{children}</Context.Provider>
}
const {result} = renderHook(
() => {
return React.useContext(Context)
},
{
wrapper: Wrapper,
legacyRoot: true,
},
)

expect(result.current).toEqual('provided')

expect(console.error).toHaveBeenCalledTimes(1)
expect(console.error).toHaveBeenNthCalledWith(
1,
"Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot",
)
})
4 changes: 2 additions & 2 deletions src/pure.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ function cleanup() {
}

function renderHook(renderCallback, options = {}) {
const {initialProps, wrapper} = options
const {initialProps, ...renderOptions} = options
const result = React.createRef()

function TestComponent({renderCallbackProps}) {
Expand All @@ -234,7 +234,7 @@ function renderHook(renderCallback, options = {}) {

const {rerender: baseRerender, unmount} = render(
<TestComponent renderCallbackProps={initialProps} />,
{wrapper},
renderOptions,
)

function rerender(rerenderCallbackProps) {
Expand Down
24 changes: 14 additions & 10 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,28 +120,32 @@ export interface RenderHookResult<Result, Props> {
unmount: () => void
}

export interface RenderHookOptions<Props> {
export interface RenderHookOptions<
Props,
Q extends Queries = typeof queries,
Container extends Element | DocumentFragment = HTMLElement,
BaseElement extends Element | DocumentFragment = Container,
> extends RenderOptions<Q, Container, BaseElement> {
/**
* The argument passed to the renderHook callback. Can be useful if you plan
* to use the rerender utility to change the values passed to your hook.
*/
initialProps?: Props
/**
* Pass a React Component as the wrapper option to have it rendered around the inner element. This is most useful for creating
* reusable custom render functions for common data providers. See setup for examples.
*
* @see https://testing-library.com/docs/react-testing-library/api/#wrapper
*/
wrapper?: React.JSXElementConstructor<{children: React.ReactElement}>
}

/**
* Allows you to render a hook within a test React component without having to
* create that component yourself.
*/
export function renderHook<Result, Props>(
export function renderHook<
Result,
Props,
Q extends Queries = typeof queries,
Container extends Element | DocumentFragment = HTMLElement,
BaseElement extends Element | DocumentFragment = Container,
>(
render: (initialProps: Props) => Result,
options?: RenderHookOptions<Props>,
options?: RenderHookOptions<Props, Q, Container, BaseElement>,
): RenderHookResult<Result, Props>

/**
Expand Down

0 comments on commit 27a9584

Please sign in to comment.