44/// <reference types="react/experimental" />
55
66import * as React from 'react' ;
7- import { PassThrough , Readable , Transform } from 'node:stream' ;
8- import { text } from 'node:stream/consumers' ;
97import { Suspense , PropsWithChildren } from 'react' ;
108
119import * as path from 'path' ;
1210import * as mock from 'mock-fs' ;
1311
14- import ReactOnRails , { RailsContextWithServerStreamingCapabilities } from '../src/ReactOnRailsRSC' ;
15- import AsyncQueue from './AsyncQueue' ;
16- import StreamReader from './StreamReader' ;
17- import removeRSCChunkStack from './utils/removeRSCChunkStack' ;
12+ import ReactOnRails , { RailsContextWithServerStreamingCapabilities } from '../src/ReactOnRailsRSC.ts ' ;
13+ import AsyncQueue from './AsyncQueue.ts ' ;
14+ import StreamReader from './StreamReader.ts ' ;
15+ import removeRSCChunkStack from './utils/removeRSCChunkStack.ts ' ;
1816
19- const manifestFileDirectory = path . resolve ( __dirname , '../src' )
17+ const manifestFileDirectory = path . resolve ( __dirname , '../src' ) ;
2018const clientManifestPath = path . join ( manifestFileDirectory , 'react-client-manifest.json' ) ;
2119
22- mock ( {
23- [ clientManifestPath ] : JSON . stringify ( {
24- filePathToModuleMetadata : { } ,
25- moduleLoading : { prefix : '' , crossOrigin : null } ,
26- } ) ,
20+ beforeEach ( ( ) => {
21+ mock ( {
22+ [ clientManifestPath ] : JSON . stringify ( {
23+ filePathToModuleMetadata : { } ,
24+ moduleLoading : { prefix : '' , crossOrigin : null } ,
25+ } ) ,
26+ } ) ;
2727} ) ;
2828
29- afterAll ( ( ) => mock . restore ( ) ) ;
29+ afterEach ( ( ) => mock . restore ( ) ) ;
3030
31- const AsyncQueueItem = async ( { asyncQueue, children } : PropsWithChildren < { asyncQueue : AsyncQueue < string > } > ) => {
31+ const AsyncQueueItem = async ( {
32+ asyncQueue,
33+ children,
34+ } : PropsWithChildren < { asyncQueue : AsyncQueue < string > } > ) => {
3235 const value = await asyncQueue . dequeue ( ) ;
3336
3437 return (
3538 < >
3639 < p > Data: { value } </ p >
3740 { children }
3841 </ >
39- )
40- }
42+ ) ;
43+ } ;
4144
4245const AsyncQueueContainer = ( { asyncQueue } : { asyncQueue : AsyncQueue < string > } ) => {
4346 return (
@@ -55,8 +58,8 @@ const AsyncQueueContainer = ({ asyncQueue }: { asyncQueue: AsyncQueue<string> })
5558 </ AsyncQueueItem >
5659 </ Suspense >
5760 </ div >
58- )
59- }
61+ ) ;
62+ } ;
6063
6164ReactOnRails . register ( { AsyncQueueContainer } ) ;
6265
@@ -72,30 +75,30 @@ const renderComponent = (props: Record<string, unknown>) => {
7275 domNodeId : 'dom-id' ,
7376 props,
7477 } ) ;
75- }
78+ } ;
7679
7780const createParallelRenders = ( size : number ) => {
7881 const asyncQueues = new Array ( size ) . fill ( null ) . map ( ( ) => new AsyncQueue < string > ( ) ) ;
7982 const streams = asyncQueues . map ( ( asyncQueue ) => {
8083 return renderComponent ( { asyncQueue } ) ;
8184 } ) ;
82- const readers = streams . map ( stream => new StreamReader ( stream ) ) ;
85+ const readers = streams . map ( ( stream ) => new StreamReader ( stream ) ) ;
8386
84- const enqueue = ( value : string ) => asyncQueues . forEach ( asyncQueues => asyncQueues . enqueue ( value ) ) ;
87+ const enqueue = ( value : string ) => asyncQueues . forEach ( ( asyncQueue ) => asyncQueue . enqueue ( value ) ) ;
8588
86- const expectNextChunk = ( nextChunk : string ) => Promise . all (
87- readers . map ( async ( reader ) => {
88- const chunk = await reader . nextChunk ( ) ;
89- expect ( removeRSCChunkStack ( chunk ) ) . toEqual ( removeRSCChunkStack ( nextChunk ) ) ;
90- } )
91- ) ;
92-
93- const expectEndOfStream = ( ) => Promise . all (
94- readers . map ( reader => expect ( reader . nextChunk ( ) ) . rejects . toThrow ( / Q u e u e E n d e d / ) )
95- ) ;
89+ const expectNextChunk = ( nextChunk : string ) =>
90+ Promise . all (
91+ readers . map ( async ( reader ) => {
92+ const chunk = await reader . nextChunk ( ) ;
93+ expect ( removeRSCChunkStack ( chunk ) ) . toEqual ( removeRSCChunkStack ( nextChunk ) ) ;
94+ } ) ,
95+ ) ;
96+
97+ const expectEndOfStream = ( ) =>
98+ Promise . all ( readers . map ( ( reader ) => expect ( reader . nextChunk ( ) ) . rejects . toThrow ( / Q u e u e E n d e d / ) ) ) ;
9699
97100 return { enqueue, expectNextChunk, expectEndOfStream } ;
98- }
101+ } ;
99102
100103test ( 'Renders concurrent rsc streams as single rsc stream' , async ( ) => {
101104 expect . assertions ( 258 ) ;
@@ -104,38 +107,38 @@ test('Renders concurrent rsc streams as single rsc stream', async () => {
104107 const reader = new StreamReader ( stream ) ;
105108
106109 const chunks : string [ ] = [ ] ;
107- let chunk = await reader . nextChunk ( )
110+ let chunk = await reader . nextChunk ( ) ;
108111 chunks . push ( chunk ) ;
109- expect ( chunk ) . toContain ( " Async Queue" ) ;
110- expect ( chunk ) . toContain ( " Loading Item2" ) ;
111- expect ( chunk ) . not . toContain ( " Random Value" ) ;
112+ expect ( chunk ) . toContain ( ' Async Queue' ) ;
113+ expect ( chunk ) . toContain ( ' Loading Item2' ) ;
114+ expect ( chunk ) . not . toContain ( ' Random Value' ) ;
112115
113- asyncQueue . enqueue ( " Random Value1" ) ;
116+ asyncQueue . enqueue ( ' Random Value1' ) ;
114117 chunk = await reader . nextChunk ( ) ;
115118 chunks . push ( chunk ) ;
116- expect ( chunk ) . toContain ( " Random Value1" ) ;
119+ expect ( chunk ) . toContain ( ' Random Value1' ) ;
117120
118- asyncQueue . enqueue ( " Random Value2" ) ;
121+ asyncQueue . enqueue ( ' Random Value2' ) ;
119122 chunk = await reader . nextChunk ( ) ;
120123 chunks . push ( chunk ) ;
121- expect ( chunk ) . toContain ( " Random Value2" ) ;
124+ expect ( chunk ) . toContain ( ' Random Value2' ) ;
122125
123- asyncQueue . enqueue ( " Random Value3" ) ;
126+ asyncQueue . enqueue ( ' Random Value3' ) ;
124127 chunk = await reader . nextChunk ( ) ;
125128 chunks . push ( chunk ) ;
126- expect ( chunk ) . toContain ( " Random Value3" ) ;
129+ expect ( chunk ) . toContain ( ' Random Value3' ) ;
127130
128131 await expect ( reader . nextChunk ( ) ) . rejects . toThrow ( / Q u e u e E n d e d / ) ;
129132
130133 const { enqueue, expectNextChunk, expectEndOfStream } = createParallelRenders ( 50 ) ;
131134
132135 expect ( chunks ) . toHaveLength ( 4 ) ;
133- await expectNextChunk ( chunks [ 0 ] ! ) ;
134- enqueue ( " Random Value1" ) ;
135- await expectNextChunk ( chunks [ 1 ] ! ) ;
136- enqueue ( " Random Value2" ) ;
137- await expectNextChunk ( chunks [ 2 ] ! ) ;
138- enqueue ( " Random Value3" ) ;
139- await expectNextChunk ( chunks [ 3 ] ! ) ;
136+ await expectNextChunk ( chunks [ 0 ] ) ;
137+ enqueue ( ' Random Value1' ) ;
138+ await expectNextChunk ( chunks [ 1 ] ) ;
139+ enqueue ( ' Random Value2' ) ;
140+ await expectNextChunk ( chunks [ 2 ] ) ;
141+ enqueue ( ' Random Value3' ) ;
142+ await expectNextChunk ( chunks [ 3 ] ) ;
140143 await expectEndOfStream ( ) ;
141144} ) ;
0 commit comments