UNPKG

1.43 kBTypeScriptView Raw
1import { queries, Queries, BoundFunction } from '@testing-library/dom'
2import { act as preactAct } from 'preact/test-utils'
3import { ComponentChild } from 'preact'
4
5export * from '@testing-library/dom'
6
7export type RenderResult<Q extends Queries = typeof queries> = {
8 container: Element
9 baseElement: Element
10 debug: (baseElement?: Element | DocumentFragment) => void
11 rerender: (ui: ComponentChild) => void
12 unmount: () => boolean
13 asFragment: () => DocumentFragment
14} & { [P in keyof Q]: BoundFunction<Q[P]> }
15
16export interface RenderOptions<Q extends Queries = typeof queries> {
17 container?: Element
18 baseElement?: Element
19 queries?: Q
20 wrapper?: ComponentChild
21}
22
23type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
24
25/**
26 * Render into a container which is appended to document.body. It should be used with cleanup.
27 */
28export function render(ui: ComponentChild, options?: Omit<RenderOptions, 'queries'>): RenderResult
29
30export function render<Q extends Queries>(
31 ui: ComponentChild,
32 options: RenderOptions<Q>,
33): RenderResult<Q>
34
35/**
36 * Unmounts Preact trees that were mounted with render.
37 */
38export function cleanup(): void
39
40/**
41 * Simply calls preact/test-utils.act(cb)
42 *
43 * If that's not available (older version of preact) then it
44 * simply calls the given callback immediately
45 */
46export const act: typeof preactAct extends undefined
47 ? (callback: () => void) => void
48 : typeof preactAct
49
\No newline at end of file