UNPKG

9.23 kBTypeScriptView Raw
1// TypeScript Version: 3.8
2import * as ReactDOMClient from 'react-dom/client'
3import {
4 queries,
5 Queries,
6 BoundFunction,
7 prettyFormat,
8 Config as ConfigDTL,
9} from '@testing-library/dom'
10import {act as reactDeprecatedAct} from 'react-dom/test-utils'
11//@ts-ignore
12import {act as reactAct} from 'react'
13
14export * from '@testing-library/dom'
15
16export interface Config extends ConfigDTL {
17 reactStrictMode: boolean
18}
19
20export interface ConfigFn {
21 (existingConfig: Config): Partial<Config>
22}
23
24export function configure(configDelta: ConfigFn | Partial<Config>): void
25
26export function getConfig(): Config
27
28export type RenderResult<
29 Q extends Queries = typeof queries,
30 Container extends RendererableContainer | HydrateableContainer = HTMLElement,
31 BaseElement extends RendererableContainer | HydrateableContainer = Container,
32> = {
33 container: Container
34 baseElement: BaseElement
35 debug: (
36 baseElement?:
37 | RendererableContainer
38 | HydrateableContainer
39 | Array<RendererableContainer | HydrateableContainer>
40 | undefined,
41 maxLength?: number | undefined,
42 options?: prettyFormat.OptionsReceived | undefined,
43 ) => void
44 rerender: (ui: React.ReactNode) => void
45 unmount: () => void
46 asFragment: () => DocumentFragment
47} & {[P in keyof Q]: BoundFunction<Q[P]>}
48
49/** @deprecated */
50export type BaseRenderOptions<
51 Q extends Queries,
52 Container extends RendererableContainer | HydrateableContainer,
53 BaseElement extends RendererableContainer | HydrateableContainer,
54> = RenderOptions<Q, Container, BaseElement>
55
56type RendererableContainer = ReactDOMClient.Container
57type HydrateableContainer = Parameters<typeof ReactDOMClient['hydrateRoot']>[0]
58/** @deprecated */
59export interface ClientRenderOptions<
60 Q extends Queries,
61 Container extends RendererableContainer,
62 BaseElement extends RendererableContainer = Container,
63> extends BaseRenderOptions<Q, Container, BaseElement> {
64 /**
65 * If `hydrate` is set to `true`, then it will render with `ReactDOM.hydrate`. This may be useful if you are using server-side
66 * rendering and use ReactDOM.hydrate to mount your components.
67 *
68 * @see https://testing-library.com/docs/react-testing-library/api/#hydrate)
69 */
70 hydrate?: false | undefined
71}
72/** @deprecated */
73export interface HydrateOptions<
74 Q extends Queries,
75 Container extends HydrateableContainer,
76 BaseElement extends HydrateableContainer = Container,
77> extends BaseRenderOptions<Q, Container, BaseElement> {
78 /**
79 * If `hydrate` is set to `true`, then it will render with `ReactDOM.hydrate`. This may be useful if you are using server-side
80 * rendering and use ReactDOM.hydrate to mount your components.
81 *
82 * @see https://testing-library.com/docs/react-testing-library/api/#hydrate)
83 */
84 hydrate: true
85}
86
87export interface RenderOptions<
88 Q extends Queries = typeof queries,
89 Container extends RendererableContainer | HydrateableContainer = HTMLElement,
90 BaseElement extends RendererableContainer | HydrateableContainer = Container,
91> {
92 /**
93 * By default, React Testing Library will create a div and append that div to the document.body. Your React component will be rendered in the created div. If you provide your own HTMLElement container via this option,
94 * it will not be appended to the document.body automatically.
95 *
96 * For example: If you are unit testing a `<tbody>` element, it cannot be a child of a div. In this case, you can
97 * specify a table as the render container.
98 *
99 * @see https://testing-library.com/docs/react-testing-library/api/#container
100 */
101 container?: Container | undefined
102 /**
103 * Defaults to the container if the container is specified. Otherwise `document.body` is used for the default. This is used as
104 * the base element for the queries as well as what is printed when you use `debug()`.
105 *
106 * @see https://testing-library.com/docs/react-testing-library/api/#baseelement
107 */
108 baseElement?: BaseElement | undefined
109 /**
110 * If `hydrate` is set to `true`, then it will render with `ReactDOM.hydrate`. This may be useful if you are using server-side
111 * rendering and use ReactDOM.hydrate to mount your components.
112 *
113 * @see https://testing-library.com/docs/react-testing-library/api/#hydrate)
114 */
115 hydrate?: boolean | undefined
116 /**
117 * Only works if used with React 18.
118 * Set to `true` if you want to force synchronous `ReactDOM.render`.
119 * Otherwise `render` will default to concurrent React if available.
120 */
121 legacyRoot?: boolean | undefined
122 /**
123 * Queries to bind. Overrides the default set from DOM Testing Library unless merged.
124 *
125 * @see https://testing-library.com/docs/react-testing-library/api/#queries
126 */
127 queries?: Q | undefined
128 /**
129 * Pass a React Component as the wrapper option to have it rendered around the inner element. This is most useful for creating
130 * reusable custom render functions for common data providers. See setup for examples.
131 *
132 * @see https://testing-library.com/docs/react-testing-library/api/#wrapper
133 */
134 wrapper?: React.JSXElementConstructor<{children: React.ReactNode}> | undefined
135}
136
137type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
138
139/**
140 * Render into a container which is appended to document.body. It should be used with cleanup.
141 */
142export function render<
143 Q extends Queries = typeof queries,
144 Container extends RendererableContainer | HydrateableContainer = HTMLElement,
145 BaseElement extends RendererableContainer | HydrateableContainer = Container,
146>(
147 ui: React.ReactNode,
148 options: RenderOptions<Q, Container, BaseElement>,
149): RenderResult<Q, Container, BaseElement>
150export function render(
151 ui: React.ReactNode,
152 options?: Omit<RenderOptions, 'queries'> | undefined,
153): RenderResult
154
155export interface RenderHookResult<Result, Props> {
156 /**
157 * Triggers a re-render. The props will be passed to your renderHook callback.
158 */
159 rerender: (props?: Props) => void
160 /**
161 * This is a stable reference to the latest value returned by your renderHook
162 * callback
163 */
164 result: {
165 /**
166 * The value returned by your renderHook callback
167 */
168 current: Result
169 }
170 /**
171 * Unmounts the test component. This is useful for when you need to test
172 * any cleanup your useEffects have.
173 */
174 unmount: () => void
175}
176
177/** @deprecated */
178export type BaseRenderHookOptions<
179 Props,
180 Q extends Queries,
181 Container extends RendererableContainer | HydrateableContainer,
182 BaseElement extends Element | DocumentFragment,
183> = RenderHookOptions<Props, Q, Container, BaseElement>
184
185/** @deprecated */
186export interface ClientRenderHookOptions<
187 Props,
188 Q extends Queries,
189 Container extends Element | DocumentFragment,
190 BaseElement extends Element | DocumentFragment = Container,
191> extends BaseRenderHookOptions<Props, Q, Container, BaseElement> {
192 /**
193 * If `hydrate` is set to `true`, then it will render with `ReactDOM.hydrate`. This may be useful if you are using server-side
194 * rendering and use ReactDOM.hydrate to mount your components.
195 *
196 * @see https://testing-library.com/docs/react-testing-library/api/#hydrate)
197 */
198 hydrate?: false | undefined
199}
200
201/** @deprecated */
202export interface HydrateHookOptions<
203 Props,
204 Q extends Queries,
205 Container extends Element | DocumentFragment,
206 BaseElement extends Element | DocumentFragment = Container,
207> extends BaseRenderHookOptions<Props, Q, Container, BaseElement> {
208 /**
209 * If `hydrate` is set to `true`, then it will render with `ReactDOM.hydrate`. This may be useful if you are using server-side
210 * rendering and use ReactDOM.hydrate to mount your components.
211 *
212 * @see https://testing-library.com/docs/react-testing-library/api/#hydrate)
213 */
214 hydrate: true
215}
216
217export interface RenderHookOptions<
218 Props,
219 Q extends Queries = typeof queries,
220 Container extends RendererableContainer | HydrateableContainer = HTMLElement,
221 BaseElement extends RendererableContainer | HydrateableContainer = Container,
222> extends BaseRenderOptions<Q, Container, BaseElement> {
223 /**
224 * The argument passed to the renderHook callback. Can be useful if you plan
225 * to use the rerender utility to change the values passed to your hook.
226 */
227 initialProps?: Props | undefined
228}
229
230/**
231 * Allows you to render a hook within a test React component without having to
232 * create that component yourself.
233 */
234export function renderHook<
235 Result,
236 Props,
237 Q extends Queries = typeof queries,
238 Container extends RendererableContainer | HydrateableContainer = HTMLElement,
239 BaseElement extends RendererableContainer | HydrateableContainer = Container,
240>(
241 render: (initialProps: Props) => Result,
242 options?: RenderHookOptions<Props, Q, Container, BaseElement> | undefined,
243): RenderHookResult<Result, Props>
244
245/**
246 * Unmounts React trees that were mounted with render.
247 */
248export function cleanup(): void
249
250/**
251 * Simply calls React.act(cb)
252 * If that's not available (older version of react) then it
253 * simply calls the deprecated version which is ReactTestUtils.act(cb)
254 */
255// IfAny<typeof reactAct, reactDeprecatedAct, reactAct> from https://stackoverflow.com/a/61626123/3406963
256export const act: 0 extends 1 & typeof reactAct
257 ? typeof reactDeprecatedAct
258 : typeof reactAct
259
\No newline at end of file