|
1 | 1 | import { queries, Queries, BoundFunction } from '@testing-library/dom' |
2 | 2 | import { act as preactAct } from 'preact/test-utils' |
3 | | -import { ComponentChild } from 'preact' |
| 3 | +import { ComponentChild, ComponentType, Element } from 'preact' |
4 | 4 |
|
5 | 5 | export * from '@testing-library/dom' |
6 | 6 |
|
@@ -46,3 +46,49 @@ export function cleanup(): void |
46 | 46 | export const act: typeof preactAct extends undefined |
47 | 47 | ? (callback: () => void) => void |
48 | 48 | : typeof preactAct |
| 49 | + |
| 50 | +export interface RenderHookResult<Result, Props> { |
| 51 | + /** |
| 52 | + * Triggers a re-render. The props will be passed to your renderHook callback. |
| 53 | + */ |
| 54 | + rerender: (props?: Props) => void |
| 55 | + /** |
| 56 | + * This is a stable reference to the latest value returned by your renderHook |
| 57 | + * callback |
| 58 | + */ |
| 59 | + result: { |
| 60 | + /** |
| 61 | + * The value returned by your renderHook callback |
| 62 | + */ |
| 63 | + current: Result |
| 64 | + } |
| 65 | + /** |
| 66 | + * Unmounts the test component. This is useful for when you need to test |
| 67 | + * any cleanup your useEffects have. |
| 68 | + */ |
| 69 | + unmount: () => void |
| 70 | +} |
| 71 | + |
| 72 | +export interface RenderHookOptions<Props> { |
| 73 | + /** |
| 74 | + * The argument passed to the renderHook callback. Can be useful if you plan |
| 75 | + * to use the rerender utility to change the values passed to your hook. |
| 76 | + */ |
| 77 | + initialProps?: Props |
| 78 | + /** |
| 79 | + * Pass a React Component as the wrapper option to have it rendered around the inner element. This is most useful for creating |
| 80 | + * reusable custom render functions for common data providers. See setup for examples. |
| 81 | + * |
| 82 | + * @see https://testing-library.com/docs/react-testing-library/api/#wrapper |
| 83 | + */ |
| 84 | + wrapper?: ComponentType<{ children: Element }> |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Allows you to render a hook within a test React component without having to |
| 89 | + * create that component yourself. |
| 90 | + */ |
| 91 | +export function renderHook<Result, Props>( |
| 92 | + render: (initialProps: Props) => Result, |
| 93 | + options?: RenderHookOptions<Props>, |
| 94 | +): RenderHookResult<Result, Props> |
0 commit comments