@@ -86,14 +86,68 @@ describe('ReactDOMFizzServer', () => {
86
86
) ;
87
87
} ) ;
88
88
89
+ // @gate experimental
90
+ it ( 'emits all HTML as one unit if we wait until the end to start' , async ( ) => {
91
+ let hasLoaded = false ;
92
+ let resolve ;
93
+ const promise = new Promise ( r => ( resolve = r ) ) ;
94
+ function Wait ( ) {
95
+ if ( ! hasLoaded ) {
96
+ throw promise ;
97
+ }
98
+ return 'Done' ;
99
+ }
100
+ let isComplete = false ;
101
+ const { writable, output} = getTestWritable ( ) ;
102
+ const { startWriting} = ReactDOMFizzServer . pipeToNodeWritable (
103
+ < div >
104
+ < Suspense fallback = "Loading" >
105
+ < Wait />
106
+ </ Suspense >
107
+ </ div > ,
108
+ writable ,
109
+ {
110
+ onCompleteAll ( ) {
111
+ isComplete = true ;
112
+ } ,
113
+ } ,
114
+ ) ;
115
+ await jest . runAllTimers ( ) ;
116
+ expect ( output . result ) . toBe ( '' ) ;
117
+ expect ( isComplete ) . toBe ( false ) ;
118
+ // Resolve the loading.
119
+ hasLoaded = true ;
120
+ await resolve ( ) ;
121
+
122
+ await jest . runAllTimers ( ) ;
123
+
124
+ expect ( output . result ) . toBe ( '' ) ;
125
+ expect ( isComplete ) . toBe ( true ) ;
126
+
127
+ // First we write our header.
128
+ output . result +=
129
+ '<!doctype html><html><head><title>test</title><head><body>' ;
130
+ // Then React starts writing.
131
+ startWriting ( ) ;
132
+ expect ( output . result ) . toBe (
133
+ '<!doctype html><html><head><title>test</title><head><body><div><!--$-->Done<!--/$--></div>' ,
134
+ ) ;
135
+ } ) ;
136
+
89
137
// @gate experimental
90
138
it ( 'should error the stream when an error is thrown at the root' , async ( ) => {
139
+ const reportedErrors = [ ] ;
91
140
const { writable, output, completed} = getTestWritable ( ) ;
92
141
ReactDOMFizzServer . pipeToNodeWritable (
93
142
< div >
94
143
< Throw />
95
144
</ div > ,
96
145
writable ,
146
+ {
147
+ onError ( x ) {
148
+ reportedErrors . push ( x ) ;
149
+ } ,
150
+ } ,
97
151
) ;
98
152
99
153
// The stream is errored even if we haven't started writing.
@@ -102,10 +156,13 @@ describe('ReactDOMFizzServer', () => {
102
156
103
157
expect ( output . error ) . toBe ( theError ) ;
104
158
expect ( output . result ) . toBe ( '' ) ;
159
+ // This type of error is reported to the error callback too.
160
+ expect ( reportedErrors ) . toEqual ( [ theError ] ) ;
105
161
} ) ;
106
162
107
163
// @gate experimental
108
164
it ( 'should error the stream when an error is thrown inside a fallback' , async ( ) => {
165
+ const reportedErrors = [ ] ;
109
166
const { writable, output, completed} = getTestWritable ( ) ;
110
167
const { startWriting} = ReactDOMFizzServer . pipeToNodeWritable (
111
168
< div >
@@ -114,17 +171,24 @@ describe('ReactDOMFizzServer', () => {
114
171
</ Suspense >
115
172
</ div > ,
116
173
writable ,
174
+ {
175
+ onError ( x ) {
176
+ reportedErrors . push ( x ) ;
177
+ } ,
178
+ } ,
117
179
) ;
118
180
startWriting ( ) ;
119
181
120
182
await completed ;
121
183
122
184
expect ( output . error ) . toBe ( theError ) ;
123
185
expect ( output . result ) . toBe ( '' ) ;
186
+ expect ( reportedErrors ) . toEqual ( [ theError ] ) ;
124
187
} ) ;
125
188
126
189
// @gate experimental
127
190
it ( 'should not error the stream when an error is thrown inside suspense boundary' , async ( ) => {
191
+ const reportedErrors = [ ] ;
128
192
const { writable, output, completed} = getTestWritable ( ) ;
129
193
const { startWriting} = ReactDOMFizzServer . pipeToNodeWritable (
130
194
< div >
@@ -133,13 +197,20 @@ describe('ReactDOMFizzServer', () => {
133
197
</ Suspense >
134
198
</ div > ,
135
199
writable ,
200
+ {
201
+ onError ( x ) {
202
+ reportedErrors . push ( x ) ;
203
+ } ,
204
+ } ,
136
205
) ;
137
206
startWriting ( ) ;
138
207
139
208
await completed ;
140
209
141
210
expect ( output . error ) . toBe ( undefined ) ;
142
211
expect ( output . result ) . toContain ( 'Loading' ) ;
212
+ // While no error is reported to the stream, the error is reported to the callback.
213
+ expect ( reportedErrors ) . toEqual ( [ theError ] ) ;
143
214
} ) ;
144
215
145
216
// @gate experimental
0 commit comments