-
Notifications
You must be signed in to change notification settings - Fork 47.3k
/
ReactDOMFizzForm-test.js
601 lines (525 loc) · 18.7 KB
/
ReactDOMFizzForm-test.js
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/**
* 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
*/
'use strict';
import {insertNodesAndExecuteScripts} from '../test-utils/FizzTestUtils';
// Polyfills for test environment
global.ReadableStream =
require('web-streams-polyfill/ponyfill/es6').ReadableStream;
global.TextEncoder = require('util').TextEncoder;
let act;
let container;
let React;
let ReactDOMServer;
let ReactDOMClient;
let useFormStatus;
let useOptimistic;
describe('ReactDOMFizzForm', () => {
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactDOMServer = require('react-dom/server.browser');
ReactDOMClient = require('react-dom/client');
useFormStatus = require('react-dom').experimental_useFormStatus;
useOptimistic = require('react').experimental_useOptimistic;
act = require('internal-test-utils').act;
container = document.createElement('div');
document.body.appendChild(container);
});
afterEach(() => {
document.body.removeChild(container);
});
function submit(submitter) {
const form = submitter.form || submitter;
if (!submitter.form) {
submitter = undefined;
}
const submitEvent = new Event('submit', {bubbles: true, cancelable: true});
submitEvent.submitter = submitter;
const returnValue = form.dispatchEvent(submitEvent);
if (!returnValue) {
return;
}
const action =
(submitter && submitter.getAttribute('formaction')) || form.action;
if (!/\s*javascript:/i.test(action)) {
throw new Error('Navigate to: ' + action);
}
}
async function readIntoContainer(stream) {
const reader = stream.getReader();
let result = '';
while (true) {
const {done, value} = await reader.read();
if (done) {
break;
}
result += Buffer.from(value).toString('utf8');
}
const temp = document.createElement('div');
temp.innerHTML = result;
insertNodesAndExecuteScripts(temp, container, null);
}
// @gate enableFormActions
it('should allow passing a function to form action during SSR', async () => {
const ref = React.createRef();
let foo;
function action(formData) {
foo = formData.get('foo');
}
function App() {
return (
<form action={action} ref={ref}>
<input type="text" name="foo" defaultValue="bar" />
</form>
);
}
const stream = await ReactDOMServer.renderToReadableStream(<App />);
await readIntoContainer(stream);
await act(async () => {
ReactDOMClient.hydrateRoot(container, <App />);
});
submit(ref.current);
expect(foo).toBe('bar');
});
// @gate enableFormActions
it('should allow passing a function to an input/button formAction', async () => {
const inputRef = React.createRef();
const buttonRef = React.createRef();
let rootActionCalled = false;
let savedTitle = null;
let deletedTitle = null;
function action(formData) {
rootActionCalled = true;
}
function saveItem(formData) {
savedTitle = formData.get('title');
}
function deleteItem(formData) {
deletedTitle = formData.get('title');
}
function App() {
return (
<form action={action}>
<input type="text" name="title" defaultValue="Hello" />
<input
type="submit"
formAction={saveItem}
value="Save"
ref={inputRef}
/>
<button formAction={deleteItem} ref={buttonRef}>
Delete
</button>
</form>
);
}
const stream = await ReactDOMServer.renderToReadableStream(<App />);
await readIntoContainer(stream);
await act(async () => {
ReactDOMClient.hydrateRoot(container, <App />);
});
expect(savedTitle).toBe(null);
expect(deletedTitle).toBe(null);
submit(inputRef.current);
expect(savedTitle).toBe('Hello');
expect(deletedTitle).toBe(null);
savedTitle = null;
submit(buttonRef.current);
expect(savedTitle).toBe(null);
expect(deletedTitle).toBe('Hello');
deletedTitle = null;
expect(rootActionCalled).toBe(false);
});
// @gate enableFormActions || !__DEV__
it('should warn when passing a function action during SSR and string during hydration', async () => {
function action(formData) {}
function App({isClient}) {
return (
<form action={isClient ? 'action' : action}>
<input type="text" name="foo" defaultValue="bar" />
</form>
);
}
const stream = await ReactDOMServer.renderToReadableStream(<App />);
await readIntoContainer(stream);
await expect(async () => {
await act(async () => {
ReactDOMClient.hydrateRoot(container, <App isClient={true} />);
});
}).toErrorDev(
'Prop `action` did not match. Server: "function" Client: "action"',
);
});
// @gate enableFormActions || !__DEV__
it('should ideally warn when passing a string during SSR and function during hydration', async () => {
function action(formData) {}
function App({isClient}) {
return (
<form action={isClient ? action : 'action'}>
<input type="text" name="foo" defaultValue="bar" />
</form>
);
}
const stream = await ReactDOMServer.renderToReadableStream(<App />);
await readIntoContainer(stream);
// This should ideally warn because only the client provides a function that doesn't line up.
await act(async () => {
ReactDOMClient.hydrateRoot(container, <App isClient={true} />);
});
});
// @gate enableFormActions || !__DEV__
it('should reset form fields after you update away from hydrated function', async () => {
const formRef = React.createRef();
const inputRef = React.createRef();
const buttonRef = React.createRef();
function action(formData) {}
function App({isUpdate}) {
return (
<form
action={isUpdate ? 'action' : action}
ref={formRef}
method={isUpdate ? 'POST' : null}>
<input
type="submit"
formAction={isUpdate ? 'action' : action}
ref={inputRef}
formTarget={isUpdate ? 'elsewhere' : null}
/>
<button
formAction={isUpdate ? 'action' : action}
ref={buttonRef}
formEncType={isUpdate ? 'multipart/form-data' : null}
/>
</form>
);
}
const stream = await ReactDOMServer.renderToReadableStream(<App />);
await readIntoContainer(stream);
let root;
await act(async () => {
root = ReactDOMClient.hydrateRoot(container, <App />);
});
await act(async () => {
root.render(<App isUpdate={true} />);
});
expect(formRef.current.getAttribute('action')).toBe('action');
expect(formRef.current.hasAttribute('encType')).toBe(false);
expect(formRef.current.getAttribute('method')).toBe('POST');
expect(formRef.current.hasAttribute('target')).toBe(false);
expect(inputRef.current.getAttribute('formAction')).toBe('action');
expect(inputRef.current.hasAttribute('name')).toBe(false);
expect(inputRef.current.hasAttribute('formEncType')).toBe(false);
expect(inputRef.current.hasAttribute('formMethod')).toBe(false);
expect(inputRef.current.getAttribute('formTarget')).toBe('elsewhere');
expect(buttonRef.current.getAttribute('formAction')).toBe('action');
expect(buttonRef.current.hasAttribute('name')).toBe(false);
expect(buttonRef.current.getAttribute('formEncType')).toBe(
'multipart/form-data',
);
expect(buttonRef.current.hasAttribute('formMethod')).toBe(false);
expect(buttonRef.current.hasAttribute('formTarget')).toBe(false);
});
// @gate enableFormActions || !__DEV__
it('should reset form fields after you remove a hydrated function', async () => {
const formRef = React.createRef();
const inputRef = React.createRef();
const buttonRef = React.createRef();
function action(formData) {}
function App({isUpdate}) {
return (
<form action={isUpdate ? undefined : action} ref={formRef}>
<input
type="submit"
formAction={isUpdate ? undefined : action}
ref={inputRef}
/>
<button formAction={isUpdate ? undefined : action} ref={buttonRef} />
</form>
);
}
const stream = await ReactDOMServer.renderToReadableStream(<App />);
await readIntoContainer(stream);
let root;
await act(async () => {
root = ReactDOMClient.hydrateRoot(container, <App />);
});
await act(async () => {
root.render(<App isUpdate={true} />);
});
expect(formRef.current.hasAttribute('action')).toBe(false);
expect(formRef.current.hasAttribute('encType')).toBe(false);
expect(formRef.current.hasAttribute('method')).toBe(false);
expect(formRef.current.hasAttribute('target')).toBe(false);
expect(inputRef.current.hasAttribute('formAction')).toBe(false);
expect(inputRef.current.hasAttribute('name')).toBe(false);
expect(inputRef.current.hasAttribute('formEncType')).toBe(false);
expect(inputRef.current.hasAttribute('formMethod')).toBe(false);
expect(inputRef.current.hasAttribute('formTarget')).toBe(false);
expect(buttonRef.current.hasAttribute('formAction')).toBe(false);
expect(buttonRef.current.hasAttribute('name')).toBe(false);
expect(buttonRef.current.hasAttribute('formEncType')).toBe(false);
expect(buttonRef.current.hasAttribute('formMethod')).toBe(false);
expect(buttonRef.current.hasAttribute('formTarget')).toBe(false);
});
// @gate enableFormActions || !__DEV__
it('should restore the form fields even if they were incorrectly set', async () => {
const formRef = React.createRef();
const inputRef = React.createRef();
const buttonRef = React.createRef();
function action(formData) {}
function App({isUpdate}) {
return (
<form
action={isUpdate ? 'action' : action}
ref={formRef}
method="DELETE">
<input
type="submit"
formAction={isUpdate ? 'action' : action}
ref={inputRef}
formTarget="elsewhere"
/>
<button
formAction={isUpdate ? 'action' : action}
ref={buttonRef}
formEncType="text/plain"
/>
</form>
);
}
// Specifying the extra form fields are a DEV error, but we expect it
// to eventually still be patched up after an update.
await expect(async () => {
const stream = await ReactDOMServer.renderToReadableStream(<App />);
await readIntoContainer(stream);
}).toErrorDev([
'Cannot specify a encType or method for a form that specifies a function as the action.',
'Cannot specify a formTarget for a button that specifies a function as a formAction.',
]);
let root;
await expect(async () => {
await act(async () => {
root = ReactDOMClient.hydrateRoot(container, <App />);
});
}).toErrorDev(['Prop `formTarget` did not match.']);
await act(async () => {
root.render(<App isUpdate={true} />);
});
expect(formRef.current.getAttribute('action')).toBe('action');
expect(formRef.current.hasAttribute('encType')).toBe(false);
expect(formRef.current.getAttribute('method')).toBe('DELETE');
expect(formRef.current.hasAttribute('target')).toBe(false);
expect(inputRef.current.getAttribute('formAction')).toBe('action');
expect(inputRef.current.hasAttribute('name')).toBe(false);
expect(inputRef.current.hasAttribute('formEncType')).toBe(false);
expect(inputRef.current.hasAttribute('formMethod')).toBe(false);
expect(inputRef.current.getAttribute('formTarget')).toBe('elsewhere');
expect(buttonRef.current.getAttribute('formAction')).toBe('action');
expect(buttonRef.current.hasAttribute('name')).toBe(false);
expect(buttonRef.current.getAttribute('formEncType')).toBe('text/plain');
expect(buttonRef.current.hasAttribute('formMethod')).toBe(false);
expect(buttonRef.current.hasAttribute('formTarget')).toBe(false);
});
// @gate enableFormActions
// @gate enableAsyncActions
it('useFormStatus is not pending during server render', async () => {
function App() {
const {pending} = useFormStatus();
return 'Pending: ' + pending;
}
const stream = await ReactDOMServer.renderToReadableStream(<App />);
await readIntoContainer(stream);
expect(container.textContent).toBe('Pending: false');
await act(() => ReactDOMClient.hydrateRoot(container, <App />));
expect(container.textContent).toBe('Pending: false');
});
// @gate enableFormActions
it('should replay a form action after hydration', async () => {
let foo;
function action(formData) {
foo = formData.get('foo');
}
function App() {
return (
<form action={action}>
<input type="text" name="foo" defaultValue="bar" />
</form>
);
}
const stream = await ReactDOMServer.renderToReadableStream(<App />);
await readIntoContainer(stream);
// Dispatch an event before hydration
submit(container.getElementsByTagName('form')[0]);
await act(async () => {
ReactDOMClient.hydrateRoot(container, <App />);
});
// It should've now been replayed
expect(foo).toBe('bar');
});
// @gate enableFormActions
it('should replay input/button formAction', async () => {
let rootActionCalled = false;
let savedTitle = null;
let deletedTitle = null;
function action(formData) {
rootActionCalled = true;
}
function saveItem(formData) {
savedTitle = formData.get('title');
}
function deleteItem(formData) {
deletedTitle = formData.get('title');
}
function App() {
return (
<form action={action}>
<input type="text" name="title" defaultValue="Hello" />
<input type="submit" formAction={saveItem} value="Save" />
<button formAction={deleteItem}>Delete</button>
</form>
);
}
const stream = await ReactDOMServer.renderToReadableStream(<App />);
await readIntoContainer(stream);
submit(container.getElementsByTagName('input')[1]);
submit(container.getElementsByTagName('button')[0]);
await act(async () => {
ReactDOMClient.hydrateRoot(container, <App />);
});
expect(savedTitle).toBe('Hello');
expect(deletedTitle).toBe('Hello');
expect(rootActionCalled).toBe(false);
});
// @gate enableAsyncActions
it('useOptimistic returns passthrough value', async () => {
function App() {
const [optimisticState] = useOptimistic('hi');
return optimisticState;
}
const stream = await ReactDOMServer.renderToReadableStream(<App />);
await readIntoContainer(stream);
expect(container.textContent).toBe('hi');
await act(async () => {
ReactDOMClient.hydrateRoot(container, <App />);
});
expect(container.textContent).toBe('hi');
});
// @gate enableFormActions
it('can provide a custom action on the server for actions', async () => {
const ref = React.createRef();
let foo;
function action(formData) {
foo = formData.get('foo');
}
action.$$FORM_ACTION = function (identifierPrefix) {
const extraFields = new FormData();
extraFields.append(identifierPrefix + 'hello', 'world');
return {
action: this.name,
name: identifierPrefix,
method: 'POST',
encType: 'multipart/form-data',
target: 'self',
data: extraFields,
};
};
function App() {
return (
<form action={action} ref={ref} method={null}>
<input type="text" name="foo" defaultValue="bar" />
</form>
);
}
const stream = await ReactDOMServer.renderToReadableStream(<App />);
await readIntoContainer(stream);
const form = container.firstChild;
expect(form.getAttribute('action')).toBe('action');
expect(form.getAttribute('method')).toBe('POST');
expect(form.getAttribute('enctype')).toBe('multipart/form-data');
expect(form.getAttribute('target')).toBe('self');
const formActionName = form.firstChild.getAttribute('name');
expect(
container
.querySelector('input[name="' + formActionName + 'hello"]')
.getAttribute('value'),
).toBe('world');
await act(async () => {
ReactDOMClient.hydrateRoot(container, <App />);
});
submit(ref.current);
expect(foo).toBe('bar');
});
// @gate enableFormActions
it('can provide a custom action on buttons the server for actions', async () => {
const inputRef = React.createRef();
const buttonRef = React.createRef();
let foo;
function action(formData) {
foo = formData.get('foo');
}
action.$$FORM_ACTION = function (identifierPrefix) {
const extraFields = new FormData();
extraFields.append(identifierPrefix + 'hello', 'world');
return {
action: this.name,
name: identifierPrefix,
method: 'POST',
encType: 'multipart/form-data',
target: 'self',
data: extraFields,
};
};
function App() {
return (
<form>
<input type="hidden" name="foo" value="bar" />
<input
type="submit"
formAction={action}
method={null}
ref={inputRef}
/>
<button formAction={action} ref={buttonRef} target={null} />
</form>
);
}
const stream = await ReactDOMServer.renderToReadableStream(<App />);
await readIntoContainer(stream);
const input = container.getElementsByTagName('input')[1];
const button = container.getElementsByTagName('button')[0];
expect(input.getAttribute('formaction')).toBe('action');
expect(input.getAttribute('formmethod')).toBe('POST');
expect(input.getAttribute('formenctype')).toBe('multipart/form-data');
expect(input.getAttribute('formtarget')).toBe('self');
expect(button.getAttribute('formaction')).toBe('action');
expect(button.getAttribute('formmethod')).toBe('POST');
expect(button.getAttribute('formenctype')).toBe('multipart/form-data');
expect(button.getAttribute('formtarget')).toBe('self');
const inputName = input.getAttribute('name');
const buttonName = button.getAttribute('name');
expect(
container
.querySelector('input[name="' + inputName + 'hello"]')
.getAttribute('value'),
).toBe('world');
expect(
container
.querySelector('input[name="' + buttonName + 'hello"]')
.getAttribute('value'),
).toBe('world');
await act(async () => {
ReactDOMClient.hydrateRoot(container, <App />);
});
submit(inputRef.current);
expect(foo).toBe('bar');
foo = null;
submit(buttonRef.current);
expect(foo).toBe('bar');
});
});