@@ -306,6 +306,23 @@ describe('ReactFlight', () => {
306306 ) ;
307307 } ) ;
308308
309+ it ( 'can transport Date' , async ( ) => {
310+ function ComponentClient ( { prop} ) {
311+ return `prop: ${ prop . toISOString ( ) } ` ;
312+ }
313+ const Component = clientReference ( ComponentClient ) ;
314+
315+ const model = < Component prop = { new Date ( 1234567890123 ) } /> ;
316+
317+ const transport = ReactNoopFlightServer . render ( model ) ;
318+
319+ await act ( async ( ) => {
320+ ReactNoop . render ( await ReactNoopFlightClient . read ( transport ) ) ;
321+ } ) ;
322+
323+ expect ( ReactNoop ) . toMatchRenderedOutput ( 'prop: 2009-02-13T23:31:30.123Z' ) ;
324+ } ) ;
325+
309326 it ( 'can render a lazy component as a shared component on the server' , async ( ) => {
310327 function SharedComponent ( { text} ) {
311328 return (
@@ -675,28 +692,39 @@ describe('ReactFlight', () => {
675692 } ) ;
676693
677694 it ( 'should warn in DEV if a toJSON instance is passed to a host component' , ( ) => {
695+ const obj = {
696+ toJSON ( ) {
697+ return 123 ;
698+ } ,
699+ } ;
678700 expect ( ( ) => {
679- const transport = ReactNoopFlightServer . render (
680- < input value = { new Date ( ) } /> ,
681- ) ;
701+ const transport = ReactNoopFlightServer . render ( < input value = { obj } /> ) ;
682702 ReactNoopFlightClient . read ( transport ) ;
683703 } ) . toErrorDev (
684704 'Only plain objects can be passed to Client Components from Server Components. ' +
685- 'Date objects are not supported.' ,
705+ 'Objects with toJSON methods are not supported. ' +
706+ 'Convert it manually to a simple value before passing it to props.\n' +
707+ ' <input value={{toJSON: function}}>\n' +
708+ ' ^^^^^^^^^^^^^^^^^^^^' ,
686709 { withoutStack : true } ,
687710 ) ;
688711 } ) ;
689712
690713 it ( 'should warn in DEV if a toJSON instance is passed to a host component child' , ( ) => {
714+ class MyError extends Error {
715+ toJSON ( ) {
716+ return 123 ;
717+ }
718+ }
691719 expect ( ( ) => {
692720 const transport = ReactNoopFlightServer . render (
693- < div > Current date : { new Date ( ) } </ div > ,
721+ < div > Womp womp : { new MyError ( 'spaghetti' ) } </ div > ,
694722 ) ;
695723 ReactNoopFlightClient . read ( transport ) ;
696724 } ) . toErrorDev (
697- 'Date objects cannot be rendered as text children. Try formatting it using toString().\n' +
698- ' <div>Current date : {Date }</div>\n' +
699- ' ^^^^^^' ,
725+ 'Error objects cannot be rendered as text children. Try formatting it using toString().\n' +
726+ ' <div>Womp womp : {Error }</div>\n' +
727+ ' ^ ^^^^^^' ,
700728 { withoutStack : true } ,
701729 ) ;
702730 } ) ;
@@ -728,37 +756,46 @@ describe('ReactFlight', () => {
728756 } ) ;
729757
730758 it ( 'should warn in DEV if a toJSON instance is passed to a Client Component' , ( ) => {
759+ const obj = {
760+ toJSON ( ) {
761+ return 123 ;
762+ } ,
763+ } ;
731764 function ClientImpl ( { value} ) {
732765 return < div > { value } </ div > ;
733766 }
734767 const Client = clientReference ( ClientImpl ) ;
735768 expect ( ( ) => {
736- const transport = ReactNoopFlightServer . render (
737- < Client value = { new Date ( ) } /> ,
738- ) ;
769+ const transport = ReactNoopFlightServer . render ( < Client value = { obj } /> ) ;
739770 ReactNoopFlightClient . read ( transport ) ;
740771 } ) . toErrorDev (
741772 'Only plain objects can be passed to Client Components from Server Components. ' +
742- 'Date objects are not supported.' ,
773+ 'Objects with toJSON methods are not supported.' ,
743774 { withoutStack : true } ,
744775 ) ;
745776 } ) ;
746777
747778 it ( 'should warn in DEV if a toJSON instance is passed to a Client Component child' , ( ) => {
779+ const obj = {
780+ toJSON ( ) {
781+ return 123 ;
782+ } ,
783+ } ;
748784 function ClientImpl ( { children} ) {
749785 return < div > { children } </ div > ;
750786 }
751787 const Client = clientReference ( ClientImpl ) ;
752788 expect ( ( ) => {
753789 const transport = ReactNoopFlightServer . render (
754- < Client > Current date: { new Date ( ) } </ Client > ,
790+ < Client > Current date: { obj } </ Client > ,
755791 ) ;
756792 ReactNoopFlightClient . read ( transport ) ;
757793 } ) . toErrorDev (
758794 'Only plain objects can be passed to Client Components from Server Components. ' +
759- 'Date objects are not supported.\n' +
760- ' <>Current date: {Date}</>\n' +
761- ' ^^^^^^' ,
795+ 'Objects with toJSON methods are not supported. ' +
796+ 'Convert it manually to a simple value before passing it to props.\n' +
797+ ' <>Current date: {{toJSON: function}}</>\n' +
798+ ' ^^^^^^^^^^^^^^^^^^^^' ,
762799 { withoutStack : true } ,
763800 ) ;
764801 } ) ;
0 commit comments