@@ -524,6 +524,7 @@ describe('ReactDOMFizzForm', () => {
524
524
525
525
// @gate enableFormActions
526
526
it ( 'can provide a custom action on buttons the server for actions' , async ( ) => {
527
+ const hiddenRef = React . createRef ( ) ;
527
528
const inputRef = React . createRef ( ) ;
528
529
const buttonRef = React . createRef ( ) ;
529
530
let foo ;
@@ -546,7 +547,7 @@ describe('ReactDOMFizzForm', () => {
546
547
function App ( ) {
547
548
return (
548
549
< form >
549
- < input type = "hidden" name = "foo" value = "bar" />
550
+ < input type = "hidden" name = "foo" value = "bar" ref = { hiddenRef } />
550
551
< input
551
552
type = "submit"
552
553
formAction = { action }
@@ -588,6 +589,8 @@ describe('ReactDOMFizzForm', () => {
588
589
ReactDOMClient . hydrateRoot ( container , < App /> ) ;
589
590
} ) ;
590
591
592
+ expect ( hiddenRef . current . name ) . toBe ( 'foo' ) ;
593
+
591
594
submit ( inputRef . current ) ;
592
595
593
596
expect ( foo ) . toBe ( 'bar' ) ;
@@ -598,4 +601,51 @@ describe('ReactDOMFizzForm', () => {
598
601
599
602
expect ( foo ) . toBe ( 'bar' ) ;
600
603
} ) ;
604
+
605
+ // @gate enableFormActions
606
+ it ( 'can hydrate hidden fields in the beginning of a form' , async ( ) => {
607
+ const hiddenRef = React . createRef ( ) ;
608
+
609
+ let invoked = false ;
610
+ function action ( formData ) {
611
+ invoked = true ;
612
+ }
613
+ action . $$FORM_ACTION = function ( identifierPrefix ) {
614
+ const extraFields = new FormData ( ) ;
615
+ extraFields . append ( identifierPrefix + 'hello' , 'world' ) ;
616
+ return {
617
+ action : '' ,
618
+ name : identifierPrefix ,
619
+ method : 'POST' ,
620
+ encType : 'multipart/form-data' ,
621
+ data : extraFields ,
622
+ } ;
623
+ } ;
624
+ function App ( ) {
625
+ return (
626
+ < form action = { action } >
627
+ < input type = "hidden" name = "bar" defaultValue = "baz" ref = { hiddenRef } />
628
+ < input type = "text" name = "foo" defaultValue = "bar" />
629
+ </ form >
630
+ ) ;
631
+ }
632
+
633
+ const stream = await ReactDOMServer . renderToReadableStream ( < App /> ) ;
634
+ await readIntoContainer ( stream ) ;
635
+
636
+ const barField = container . querySelector ( '[name=bar]' ) ;
637
+
638
+ await act ( async ( ) => {
639
+ ReactDOMClient . hydrateRoot ( container , < App /> ) ;
640
+ } ) ;
641
+
642
+ expect ( hiddenRef . current ) . toBe ( barField ) ;
643
+
644
+ expect ( hiddenRef . current . name ) . toBe ( 'bar' ) ;
645
+ expect ( hiddenRef . current . value ) . toBe ( 'baz' ) ;
646
+
647
+ expect ( container . querySelectorAll ( '[name=bar]' ) . length ) . toBe ( 1 ) ;
648
+
649
+ submit ( hiddenRef . current . form ) ;
650
+ } ) ;
601
651
} ) ;
0 commit comments