forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReactDOMFizzViewTransition-test.js
More file actions
335 lines (304 loc) · 8.42 KB
/
ReactDOMFizzViewTransition-test.js
File metadata and controls
335 lines (304 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
* @jest-environment ./scripts/jest/ReactDOMServerIntegrationEnvironment
*/
'use strict';
import {
insertNodesAndExecuteScripts,
getVisibleChildren,
} from '../test-utils/FizzTestUtils';
let JSDOM;
let React;
let Suspense;
let ViewTransition;
let ReactDOMClient;
let clientAct;
let ReactDOMFizzServer;
let Stream;
let document;
let writable;
let container;
let buffer = '';
let hasErrored = false;
let fatalError = undefined;
describe('ReactDOMFizzViewTransition', () => {
beforeEach(() => {
jest.resetModules();
JSDOM = require('jsdom').JSDOM;
React = require('react');
ReactDOMClient = require('react-dom/client');
clientAct = require('internal-test-utils').act;
ReactDOMFizzServer = require('react-dom/server');
Stream = require('stream');
Suspense = React.Suspense;
ViewTransition = React.ViewTransition;
// Test Environment
const jsdom = new JSDOM(
'<!DOCTYPE html><html><head></head><body><div id="container">',
{
runScripts: 'dangerously',
},
);
document = jsdom.window.document;
container = document.getElementById('container');
global.window = jsdom.window;
// The Fizz runtime assumes requestAnimationFrame exists so we need to polyfill it.
global.requestAnimationFrame = global.window.requestAnimationFrame = cb =>
setTimeout(cb);
buffer = '';
hasErrored = false;
writable = new Stream.PassThrough();
writable.setEncoding('utf8');
writable.on('data', chunk => {
buffer += chunk;
});
writable.on('error', error => {
hasErrored = true;
fatalError = error;
});
});
afterEach(() => {
jest.restoreAllMocks();
});
async function serverAct(callback) {
await callback();
// Await one turn around the event loop.
// This assumes that we'll flush everything we have so far.
await new Promise(resolve => {
setImmediate(resolve);
});
if (hasErrored) {
throw fatalError;
}
// JSDOM doesn't support stream HTML parser so we need to give it a proper fragment.
// We also want to execute any scripts that are embedded.
// We assume that we have now received a proper fragment of HTML.
const bufferedContent = buffer;
buffer = '';
const temp = document.createElement('body');
temp.innerHTML = bufferedContent;
await insertNodesAndExecuteScripts(temp, container, null);
jest.runAllTimers();
}
// @gate enableViewTransition
it('emits annotations for view transitions', async () => {
function App() {
return (
<div>
<ViewTransition>
<div />
</ViewTransition>
<ViewTransition name="foo" update="bar">
<div />
</ViewTransition>
<ViewTransition update={{something: 'a', default: 'baz'}}>
<div />
</ViewTransition>
<ViewTransition name="outer" update="bar" share="pair">
<ViewTransition>
<div />
</ViewTransition>
</ViewTransition>
</div>
);
}
await serverAct(async () => {
const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<App />);
pipe(writable);
});
expect(getVisibleChildren(container)).toEqual(
<div>
<div vt-update="auto" />
<div vt-name="foo" vt-update="bar" vt-share="auto" />
<div vt-update="baz" />
<div vt-name="outer" vt-update="auto" vt-share="pair" />
</div>,
);
// Hydration should not yield any errors.
await clientAct(async () => {
ReactDOMClient.hydrateRoot(container, <App />);
});
});
// @gate enableViewTransition
it('emits enter/exit annotations for view transitions inside Suspense', async () => {
let resolve;
const promise = new Promise(r => (resolve = r));
function Suspend() {
return React.use(promise);
}
function App() {
const fallback = (
<ViewTransition>
<div>
<ViewTransition>
<span>Loading</span>
</ViewTransition>
</div>
</ViewTransition>
);
return (
<div>
<Suspense fallback={fallback}>
<ViewTransition>
<Suspend />
</ViewTransition>
</Suspense>
</div>
);
}
await serverAct(async () => {
const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<App />);
pipe(writable);
});
expect(getVisibleChildren(container)).toEqual(
<div>
<div vt-update="auto" vt-exit="auto">
<span vt-update="auto">Loading</span>
</div>
</div>,
);
await serverAct(async () => {
await resolve(
<div>
<ViewTransition>
<span>Content</span>
</ViewTransition>
</div>,
);
});
expect(getVisibleChildren(container)).toEqual(
<div>
<div vt-update="auto" vt-enter="auto">
<span vt-update="auto">Content</span>
</div>
</div>,
);
// Hydration should not yield any errors.
await clientAct(async () => {
ReactDOMClient.hydrateRoot(container, <App />);
});
});
// @gate enableViewTransition
it('can emit both enter and exit on the same node', async () => {
let resolve;
const promise = new Promise(r => (resolve = r));
function Suspend() {
return React.use(promise);
}
function App() {
const fallback = (
<Suspense fallback={null}>
<ViewTransition enter="hello" exit="goodbye">
<div>
<ViewTransition>
<span>Loading</span>
</ViewTransition>
</div>
</ViewTransition>
</Suspense>
);
return (
<div>
<Suspense fallback={fallback}>
<ViewTransition enter="hi">
<Suspend />
</ViewTransition>
</Suspense>
</div>
);
}
await serverAct(async () => {
const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<App />);
pipe(writable);
});
expect(getVisibleChildren(container)).toEqual(
<div>
<div vt-update="auto" vt-enter="hello" vt-exit="goodbye">
<span vt-update="auto">Loading</span>
</div>
</div>,
);
await serverAct(async () => {
await resolve(
<div>
<ViewTransition>
<span>Content</span>
</ViewTransition>
</div>,
);
});
expect(getVisibleChildren(container)).toEqual(
<div>
<div vt-update="auto" vt-enter="hi">
<span vt-update="auto">Content</span>
</div>
</div>,
);
// Hydration should not yield any errors.
await clientAct(async () => {
ReactDOMClient.hydrateRoot(container, <App />);
});
});
// @gate enableViewTransition
it('emits annotations for view transitions outside Suspense', async () => {
let resolve;
const promise = new Promise(r => (resolve = r));
function Suspend() {
return React.use(promise);
}
function App() {
const fallback = (
<div>
<ViewTransition>
<span>Loading</span>
</ViewTransition>
</div>
);
return (
<div>
<ViewTransition>
<Suspense fallback={fallback}>
<Suspend />
</Suspense>
</ViewTransition>
</div>
);
}
await serverAct(async () => {
const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<App />);
pipe(writable);
});
expect(getVisibleChildren(container)).toEqual(
<div>
<div vt-name="_R_0_" vt-update="auto" vt-share="auto">
<span vt-update="auto">Loading</span>
</div>
</div>,
);
await serverAct(async () => {
await resolve(
<div>
<ViewTransition>
<span>Content</span>
</ViewTransition>
</div>,
);
});
expect(getVisibleChildren(container)).toEqual(
<div>
<div vt-name="_R_0_" vt-update="auto" vt-share="auto">
<span vt-update="auto">Content</span>
</div>
</div>,
);
// Hydration should not yield any errors.
await clientAct(async () => {
ReactDOMClient.hydrateRoot(container, <App />);
});
});
});