1 |
|
2 | import * as ReactDOMClient from 'react-dom/client'
|
3 | import {
|
4 | queries,
|
5 | Queries,
|
6 | BoundFunction,
|
7 | prettyFormat,
|
8 | Config as ConfigDTL,
|
9 | } from '@testing-library/dom'
|
10 | import {act as reactDeprecatedAct} from 'react-dom/test-utils'
|
11 |
|
12 | import {act as reactAct} from 'react'
|
13 |
|
14 | export * from '@testing-library/dom'
|
15 |
|
16 | export interface Config extends ConfigDTL {
|
17 | reactStrictMode: boolean
|
18 | }
|
19 |
|
20 | export interface ConfigFn {
|
21 | (existingConfig: Config): Partial<Config>
|
22 | }
|
23 |
|
24 | export function configure(configDelta: ConfigFn | Partial<Config>): void
|
25 |
|
26 | export function getConfig(): Config
|
27 |
|
28 | export 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 |
|
50 | export type BaseRenderOptions<
|
51 | Q extends Queries,
|
52 | Container extends RendererableContainer | HydrateableContainer,
|
53 | BaseElement extends RendererableContainer | HydrateableContainer,
|
54 | > = RenderOptions<Q, Container, BaseElement>
|
55 |
|
56 | type RendererableContainer = ReactDOMClient.Container
|
57 | type HydrateableContainer = Parameters<typeof ReactDOMClient['hydrateRoot']>[0]
|
58 |
|
59 | export interface ClientRenderOptions<
|
60 | Q extends Queries,
|
61 | Container extends RendererableContainer,
|
62 | BaseElement extends RendererableContainer = Container,
|
63 | > extends BaseRenderOptions<Q, Container, BaseElement> {
|
64 | |
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | hydrate?: false | undefined
|
71 | }
|
72 |
|
73 | export interface HydrateOptions<
|
74 | Q extends Queries,
|
75 | Container extends HydrateableContainer,
|
76 | BaseElement extends HydrateableContainer = Container,
|
77 | > extends BaseRenderOptions<Q, Container, BaseElement> {
|
78 | |
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | hydrate: true
|
85 | }
|
86 |
|
87 | export interface RenderOptions<
|
88 | Q extends Queries = typeof queries,
|
89 | Container extends RendererableContainer | HydrateableContainer = HTMLElement,
|
90 | BaseElement extends RendererableContainer | HydrateableContainer = Container,
|
91 | > {
|
92 | |
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | container?: Container | undefined
|
102 | |
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | baseElement?: BaseElement | undefined
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | hydrate?: boolean | undefined
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 | legacyRoot?: boolean | undefined
|
122 | |
123 |
|
124 |
|
125 |
|
126 |
|
127 | queries?: Q | undefined
|
128 | |
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | wrapper?: React.JSXElementConstructor<{children: React.ReactNode}> | undefined
|
135 | }
|
136 |
|
137 | type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | export 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>
|
150 | export function render(
|
151 | ui: React.ReactNode,
|
152 | options?: Omit<RenderOptions, 'queries'> | undefined,
|
153 | ): RenderResult
|
154 |
|
155 | export 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 |
|
167 |
|
168 | current: Result
|
169 | }
|
170 | |
171 |
|
172 |
|
173 |
|
174 | unmount: () => void
|
175 | }
|
176 |
|
177 |
|
178 | export 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 |
|
186 | export 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 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | hydrate?: false | undefined
|
199 | }
|
200 |
|
201 |
|
202 | export 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 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | hydrate: true
|
215 | }
|
216 |
|
217 | export 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 |
|
225 |
|
226 |
|
227 | initialProps?: Props | undefined
|
228 | }
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | export 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 | */
|
248 | export 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
|
256 | export const act: 0 extends 1 & typeof reactAct
|
257 | ? typeof reactDeprecatedAct
|
258 | : typeof reactAct
|
259 |
|
\ | No newline at end of file |