1
1
import * as ReactDOMClient from 'react-dom/client'
2
2
import * as ReactDOM from 'react-dom'
3
+ import { type RenderOptions } from '@testing-library/react/pure.js'
3
4
import {
4
- type RenderOptions ,
5
- type RenderResult ,
6
- } from '@testing-library/react/pure.js'
7
- import {
5
+ BoundFunction ,
8
6
getQueriesForElement ,
9
7
prettyDOM ,
8
+ prettyFormat ,
10
9
type Queries ,
11
10
} from '@testing-library/dom'
12
11
import React from 'react'
@@ -25,6 +24,26 @@ const mountedRootEntries: Array<{
25
24
root : ReturnType < typeof createConcurrentRoot >
26
25
} > = [ ]
27
26
27
+ export type AsyncRenderResult <
28
+ Q extends Queries = SyncQueries ,
29
+ Container extends ReactDOMClient . Container = HTMLElement ,
30
+ BaseElement extends ReactDOMClient . Container = Container ,
31
+ > = {
32
+ container : Container
33
+ baseElement : BaseElement
34
+ debug : (
35
+ baseElement ?:
36
+ | ReactDOMClient . Container
37
+ | Array < ReactDOMClient . Container >
38
+ | undefined ,
39
+ maxLength ?: number | undefined ,
40
+ options ?: prettyFormat . OptionsReceived | undefined ,
41
+ ) => void
42
+ rerender : ( rerenderUi : React . ReactNode ) => Promise < void >
43
+ unmount : ( ) => void
44
+ asFragment : ( ) => DocumentFragment
45
+ } & { [ P in keyof Q ] : BoundFunction < Q [ P ] > }
46
+
28
47
function renderRoot (
29
48
ui : React . ReactNode ,
30
49
{
@@ -38,7 +57,7 @@ function renderRoot(
38
57
container : ReactDOMClient . Container
39
58
root : ReturnType < typeof createConcurrentRoot >
40
59
} ,
41
- ) : RenderResult < Queries , any , any > {
60
+ ) : AsyncRenderResult < { } , any , any > {
42
61
root . render (
43
62
WrapperComponent ? React . createElement ( WrapperComponent , null , ui ) : ui ,
44
63
)
@@ -57,7 +76,7 @@ function renderRoot(
57
76
unmount : ( ) => {
58
77
root . unmount ( )
59
78
} ,
60
- rerender : rerenderUi => {
79
+ rerender : async rerenderUi => {
61
80
renderRoot ( rerenderUi , {
62
81
container,
63
82
baseElement,
@@ -80,7 +99,7 @@ function renderRoot(
80
99
}
81
100
} ,
82
101
...getQueriesForElement < Queries > ( baseElement as HTMLElement , queries ) ,
83
- } as RenderResult < Queries , any , any > // TODO clean up more
102
+ }
84
103
}
85
104
86
105
export type RenderWithoutActAsync = {
@@ -91,54 +110,41 @@ export type RenderWithoutActAsync = {
91
110
> (
92
111
this : any ,
93
112
ui : React . ReactNode ,
94
- options : //Omit<
95
- RenderOptions < Q , Container , BaseElement > ,
96
- //'hydrate' | 'legacyRoot' >,
97
- ) : Promise < RenderResult < Q , Container , BaseElement > >
113
+ options : Pick <
114
+ RenderOptions < Q , Container , BaseElement > ,
115
+ 'container' | 'baseElement' | 'queries' | 'wrapper'
116
+ > ,
117
+ ) : Promise < AsyncRenderResult < Q , Container , BaseElement > >
98
118
(
99
119
this : any ,
100
120
ui : React . ReactNode ,
101
121
options ?:
102
- | Omit < RenderOptions , 'hydrate ' | 'legacyRoot ' | 'queries ' >
122
+ | Pick < RenderOptions , 'container ' | 'baseElement ' | 'wrapper ' >
103
123
| undefined ,
104
124
) : Promise <
105
- RenderResult <
125
+ AsyncRenderResult <
106
126
SyncQueries ,
107
127
ReactDOMClient . Container ,
108
128
ReactDOMClient . Container
109
129
>
110
130
>
111
131
}
112
132
113
- export function renderWithoutAct <
114
- Q extends Queries = SyncQueries ,
115
- Container extends ReactDOMClient . Container = HTMLElement ,
116
- BaseElement extends ReactDOMClient . Container = Container ,
117
- > (
118
- ui : React . ReactNode ,
119
- options : //Omit<
120
- RenderOptions < Q , Container , BaseElement > ,
121
- //'hydrate' | 'legacyRoot' >,
122
- ) : RenderResult < Q , Container , BaseElement >
123
- export function renderWithoutAct (
124
- ui : React . ReactNode ,
125
- options ?:
126
- | Omit < RenderOptions , 'hydrate' | 'legacyRoot' | 'queries' >
127
- | undefined ,
128
- ) : RenderResult < Queries , ReactDOMClient . Container , ReactDOMClient . Container >
133
+ export const renderWithoutAct =
134
+ _renderWithoutAct as unknown as RenderWithoutActAsync
129
135
130
- export function renderWithoutAct (
136
+ async function _renderWithoutAct (
131
137
ui : React . ReactNode ,
132
138
{
133
139
container,
134
140
baseElement = container ,
135
141
queries,
136
142
wrapper,
137
- } : Omit <
138
- RenderOptions < Queries , ReactDOMClient . Container , ReactDOMClient . Container > ,
139
- 'hydrate ' | 'legacyRoot '
143
+ } : Pick <
144
+ RenderOptions < SyncQueries > ,
145
+ 'container ' | 'baseElement' | 'wrapper' | 'queries '
140
146
> = { } ,
141
- ) : RenderResult < any , ReactDOMClient . Container , ReactDOMClient . Container > {
147
+ ) : Promise < AsyncRenderResult < { } > > {
142
148
if ( ! baseElement ) {
143
149
// default to document.body instead of documentElement to avoid output of potentially-large
144
150
// head elements (such as JSS style blocks) in debug output
@@ -172,7 +178,13 @@ export function renderWithoutAct(
172
178
} )
173
179
}
174
180
175
- return renderRoot ( ui , { baseElement, container, queries, wrapper, root : root ! } )
181
+ return renderRoot ( ui , {
182
+ baseElement,
183
+ container,
184
+ queries,
185
+ wrapper,
186
+ root : root ! ,
187
+ } )
176
188
}
177
189
178
190
function createLegacyRoot ( container : ReactDOMClient . Container ) {
0 commit comments