-
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 4 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 |
---|---|---|
|
@@ -90,6 +90,25 @@ export function render( | |
options?: Omit<RenderOptions, 'queries'>, | ||
): RenderResult | ||
|
||
// TODO JSDOC | ||
interface RenderHookResult<Result, Props> { | ||
rerender: (props?: Props) => void | ||
result: {current: Result} | ||
unmount: () => void | ||
} | ||
|
||
// TODO JSDOC | ||
interface RenderHookOptions<Props> { | ||
initialProps?: Props | ||
wrapper?: React.ComponentType | ||
kentcdodds marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// TODO JSDOC | ||
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).