-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add renderHook
#991
feat: Add renderHook
#991
Changes from all commits
bc4f7e5
a842157
a3f6510
887d95b
88f44fe
43157d4
cc71c64
55811ff
144b485
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React from 'react' | ||
import {renderHook} from '../pure' | ||
|
||
test('gives comitted result', () => { | ||
const {result} = renderHook(() => { | ||
const [state, setState] = React.useState(1) | ||
|
||
React.useEffect(() => { | ||
setState(2) | ||
}, []) | ||
|
||
return [state, setState] | ||
}) | ||
|
||
expect(result.current).toEqual([2, expect.any(Function)]) | ||
}) | ||
|
||
test('allows rerendering', () => { | ||
const {result, rerender} = renderHook( | ||
({branch}) => { | ||
const [left, setLeft] = React.useState('left') | ||
const [right, setRight] = React.useState('right') | ||
|
||
// eslint-disable-next-line jest/no-if | ||
switch (branch) { | ||
case 'left': | ||
return [left, setLeft] | ||
case 'right': | ||
return [right, setRight] | ||
|
||
default: | ||
throw new Error( | ||
'No Props passed. This is a bug in the implementation', | ||
) | ||
} | ||
}, | ||
{initialProps: {branch: 'left'}}, | ||
) | ||
|
||
expect(result.current).toEqual(['left', expect.any(Function)]) | ||
|
||
rerender({branch: 'right'}) | ||
|
||
expect(result.current).toEqual(['right', expect.any(Function)]) | ||
}) | ||
|
||
test('allows wrapper components', async () => { | ||
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, | ||
}, | ||
) | ||
|
||
expect(result.current).toEqual('provided') | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,52 @@ export function render( | |
options?: Omit<RenderOptions, 'queries'>, | ||
): RenderResult | ||
|
||
interface RenderHookResult<Result, Props> { | ||
/** | ||
* Triggers a re-render. The props will be passed to your renderHook callback. | ||
*/ | ||
rerender: (props?: Props) => void | ||
/** | ||
* This is a stable reference to the latest value returned by your renderHook | ||
* callback | ||
*/ | ||
result: { | ||
/** | ||
* The value returned by your renderHook callback | ||
*/ | ||
current: Result | ||
} | ||
/** | ||
* Unmounts the test component. This is useful for when you need to test | ||
* any cleanup your useEffects have. | ||
*/ | ||
unmount: () => void | ||
} | ||
|
||
interface RenderHookOptions<Props> { | ||
/** | ||
* 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>( | ||
render: (initialProps: Props) => Result, | ||
options?: RenderHookOptions<Props>, | ||
): RenderHookResult<Result, Props> | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when interface RenderHookOptions<Props> {
initialProps: Props;
wrapper?: ComponentType;
strict?: boolean;
}
interface RenderHookResultWithoutProps<Result> {
rerender: () => void;
result: { current: Result };
unmount: () => void;
}
interface RenderHookOptionsWithoutProps {
wrapper?: ComponentType;
strict?: boolean;
}
export function renderHook<Result>(
renderCallback: () => Result,
options?: RenderHookOptionsWithoutProps,
): RenderHookResultWithoutProps<Result>;
export function renderHook<Result, Props>(
renderCallback: (props: Props) => Result,
options: RenderHookOptions<Props>,
): RenderHookResult<Result, Props>;
export function renderHook<Result, Props>(
renderCallback: (props?: Props) => Result,
options: Partial<RenderHookOptions<Props>>,
): RenderHookResult<Result, Props> | RenderHookResultWithoutProps<Result> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is fine and the types are correct. Is that not the case? |
||
/** | ||
* Unmounts React trees that were mounted with render. | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the spirit of creating a simple API, should we omit the
initialProps
functionality based on @mpeyper's comment here:I agree with @mpeyper's logic that this pattern is much more confusing than the alternative and would also enable this API to be even simpler in addition to being easier to use and understand (less footguns).