@@ -12,6 +12,7 @@ import { text } from 'stream/consumers';
1212import { buildServerRenderer } from 'react-on-rails-rsc/server.node' ;
1313import createReactOutput from 'react-on-rails/createReactOutput' ;
1414import ReactOnRails , { RailsContextWithServerStreamingCapabilities } from '../src/ReactOnRailsRSC.ts' ;
15+ import removeRSCStackFromAllChunks from './utils/removeRSCStackFromAllChunks.ts'
1516
1617const PromiseWrapper = async ( { promise, name } : { promise : Promise < string > , name : string } ) => {
1718 console . log ( `[${ name } ] Before awaitng` ) ;
5960
6061afterAll ( ( ) => {
6162 mock . restore ( ) ;
62- } )
63+ } ) ;
6364
6465test ( 'no logs leakage between concurrent rendering components' , async ( ) => {
6566 const readable1 = ReactOnRails . serverRenderRSCReactComponent ( {
@@ -89,10 +90,74 @@ test('no logs leakage between concurrent rendering components', async () => {
8990
9091 expect ( content1 ) . toContain ( "First Unique Name" ) ;
9192 expect ( content2 ) . toContain ( "Second Unique Name" ) ;
92- // expect(content1.match(/First Unique Name/g)).toHaveLength(55)
93- // expect(content2.match(/Second Unique Name/g)).toHaveLength(55)
9493 expect ( content1 ) . not . toContain ( "Second Unique Name" ) ;
9594 expect ( content2 ) . not . toContain ( "First Unique Name" ) ;
95+ } ) ;
96+
97+ test ( 'no logs lekage from outside the component' , async ( ) => {
98+ const readable1 = ReactOnRails . serverRenderRSCReactComponent ( {
99+ railsContext : {
100+ reactClientManifestFileName : 'react-client-manifest.json' ,
101+ reactServerClientManifestFileName : 'react-server-client-manifest.json' ,
102+ } as unknown as RailsContextWithServerStreamingCapabilities ,
103+ name : 'PromiseContainer' ,
104+ renderingReturnsPromises : true ,
105+ throwJsErrors : true ,
106+ domNodeId : 'dom-id' ,
107+ props : { name : "First Unique Name" }
108+ } ) ;
96109
97- // expect(content1.replace(new RegExp("First Unique Name", 'g'), "Second Unique Name")).toEqual(content2);
98- } )
110+ const promise = new Promise < void > ( ( resolve ) => {
111+ let i = 0 ;
112+ const intervalId = setInterval ( ( ) => {
113+ console . log ( `Interval ${ i } at [Outside The Component]` ) ;
114+ i += 1 ;
115+ if ( i === 50 ) {
116+ clearInterval ( intervalId ) ;
117+ resolve ( ) ;
118+ }
119+ } , 1 ) ;
120+ } ) ;
121+
122+ const [ content1 ] = await Promise . all ( [ text ( readable1 ) , promise ] ) ;
123+
124+ expect ( content1 ) . toContain ( "First Unique Name" ) ;
125+ expect ( content1 ) . not . toContain ( "Outside The Component" ) ;
126+ } ) ;
127+
128+ test ( '[bug] catches logs outside the component during reading the stream' , async ( ) => {
129+ const readable1 = ReactOnRails . serverRenderRSCReactComponent ( {
130+ railsContext : {
131+ reactClientManifestFileName : 'react-client-manifest.json' ,
132+ reactServerClientManifestFileName : 'react-server-client-manifest.json' ,
133+ } as unknown as RailsContextWithServerStreamingCapabilities ,
134+ name : 'PromiseContainer' ,
135+ renderingReturnsPromises : true ,
136+ throwJsErrors : true ,
137+ domNodeId : 'dom-id' ,
138+ props : { name : "First Unique Name" }
139+ } ) ;
140+
141+ let content1 = "" ;
142+ let i = 0 ;
143+ readable1 . on ( 'data' , ( chunk ) => {
144+ i += 1 ;
145+ // To avoid infinite loop
146+ if ( i < 5 ) {
147+ console . log ( "Outside The Component" ) ;
148+ }
149+ content1 += chunk . toString ( ) ;
150+ } ) ;
151+
152+ // However, any logs from outside the stream 'data' event callback is not catched
153+ const intervalId = setInterval ( ( ) => {
154+ console . log ( "From Interval" )
155+ } , 2 ) ;
156+ await finished ( readable1 ) ;
157+ clearInterval ( intervalId ) ;
158+
159+ expect ( content1 ) . toContain ( "First Unique Name" ) ;
160+ expect ( content1 ) . not . toContain ( "From Interval" ) ;
161+ // Here's the bug
162+ expect ( content1 ) . toContain ( "Outside The Component" ) ;
163+ } ) ;
0 commit comments