|
| 1 | +/** |
| 2 | + * @jest-environment node |
| 3 | + */ |
| 4 | +/// <reference types="react/experimental" /> |
| 5 | + |
| 6 | +import * as React from 'react'; |
| 7 | +import { Suspense, PropsWithChildren } from 'react'; |
| 8 | + |
| 9 | +import * as path from 'path'; |
| 10 | +import * as mock from 'mock-fs'; |
| 11 | + |
| 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'; |
| 16 | + |
| 17 | +const manifestFileDirectory = path.resolve(__dirname, '../src'); |
| 18 | +const clientManifestPath = path.join(manifestFileDirectory, 'react-client-manifest.json'); |
| 19 | + |
| 20 | +beforeEach(() => { |
| 21 | + mock({ |
| 22 | + [clientManifestPath]: JSON.stringify({ |
| 23 | + filePathToModuleMetadata: {}, |
| 24 | + moduleLoading: { prefix: '', crossOrigin: null }, |
| 25 | + }), |
| 26 | + }); |
| 27 | +}); |
| 28 | + |
| 29 | +afterEach(() => mock.restore()); |
| 30 | + |
| 31 | +const AsyncQueueItem = async ({ |
| 32 | + asyncQueue, |
| 33 | + children, |
| 34 | +}: PropsWithChildren<{ asyncQueue: AsyncQueue<string> }>) => { |
| 35 | + const value = await asyncQueue.dequeue(); |
| 36 | + |
| 37 | + return ( |
| 38 | + <> |
| 39 | + <p>Data: {value}</p> |
| 40 | + {children} |
| 41 | + </> |
| 42 | + ); |
| 43 | +}; |
| 44 | + |
| 45 | +const AsyncQueueContainer = ({ asyncQueue }: { asyncQueue: AsyncQueue<string> }) => { |
| 46 | + return ( |
| 47 | + <div> |
| 48 | + <h1>Async Queue</h1> |
| 49 | + <Suspense fallback={<p>Loading Item1</p>}> |
| 50 | + <AsyncQueueItem asyncQueue={asyncQueue}> |
| 51 | + <Suspense fallback={<p>Loading Item2</p>}> |
| 52 | + <AsyncQueueItem asyncQueue={asyncQueue}> |
| 53 | + <Suspense fallback={<p>Loading Item3</p>}> |
| 54 | + <AsyncQueueItem asyncQueue={asyncQueue} /> |
| 55 | + </Suspense> |
| 56 | + </AsyncQueueItem> |
| 57 | + </Suspense> |
| 58 | + </AsyncQueueItem> |
| 59 | + </Suspense> |
| 60 | + </div> |
| 61 | + ); |
| 62 | +}; |
| 63 | + |
| 64 | +ReactOnRails.register({ AsyncQueueContainer }); |
| 65 | + |
| 66 | +const renderComponent = (props: Record<string, unknown>) => { |
| 67 | + return ReactOnRails.serverRenderRSCReactComponent({ |
| 68 | + railsContext: { |
| 69 | + reactClientManifestFileName: 'react-client-manifest.json', |
| 70 | + reactServerClientManifestFileName: 'react-server-client-manifest.json', |
| 71 | + } as unknown as RailsContextWithServerStreamingCapabilities, |
| 72 | + name: 'AsyncQueueContainer', |
| 73 | + renderingReturnsPromises: true, |
| 74 | + throwJsErrors: true, |
| 75 | + domNodeId: 'dom-id', |
| 76 | + props, |
| 77 | + }); |
| 78 | +}; |
| 79 | + |
| 80 | +const createParallelRenders = (size: number) => { |
| 81 | + const asyncQueues = new Array(size).fill(null).map(() => new AsyncQueue<string>()); |
| 82 | + const streams = asyncQueues.map((asyncQueue) => { |
| 83 | + return renderComponent({ asyncQueue }); |
| 84 | + }); |
| 85 | + const readers = streams.map((stream) => new StreamReader(stream)); |
| 86 | + |
| 87 | + const enqueue = (value: string) => asyncQueues.forEach((asyncQueue) => asyncQueue.enqueue(value)); |
| 88 | + |
| 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(/Queue Ended/))); |
| 99 | + |
| 100 | + return { enqueue, expectNextChunk, expectEndOfStream }; |
| 101 | +}; |
| 102 | + |
| 103 | +test('Renders concurrent rsc streams as single rsc stream', async () => { |
| 104 | + expect.assertions(258); |
| 105 | + const asyncQueue = new AsyncQueue<string>(); |
| 106 | + const stream = renderComponent({ asyncQueue }); |
| 107 | + const reader = new StreamReader(stream); |
| 108 | + |
| 109 | + const chunks: string[] = []; |
| 110 | + let chunk = await reader.nextChunk(); |
| 111 | + chunks.push(chunk); |
| 112 | + expect(chunk).toContain('Async Queue'); |
| 113 | + expect(chunk).toContain('Loading Item2'); |
| 114 | + expect(chunk).not.toContain('Random Value'); |
| 115 | + |
| 116 | + asyncQueue.enqueue('Random Value1'); |
| 117 | + chunk = await reader.nextChunk(); |
| 118 | + chunks.push(chunk); |
| 119 | + expect(chunk).toContain('Random Value1'); |
| 120 | + |
| 121 | + asyncQueue.enqueue('Random Value2'); |
| 122 | + chunk = await reader.nextChunk(); |
| 123 | + chunks.push(chunk); |
| 124 | + expect(chunk).toContain('Random Value2'); |
| 125 | + |
| 126 | + asyncQueue.enqueue('Random Value3'); |
| 127 | + chunk = await reader.nextChunk(); |
| 128 | + chunks.push(chunk); |
| 129 | + expect(chunk).toContain('Random Value3'); |
| 130 | + |
| 131 | + await expect(reader.nextChunk()).rejects.toThrow(/Queue Ended/); |
| 132 | + |
| 133 | + const { enqueue, expectNextChunk, expectEndOfStream } = createParallelRenders(50); |
| 134 | + |
| 135 | + expect(chunks).toHaveLength(4); |
| 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]); |
| 143 | + await expectEndOfStream(); |
| 144 | +}); |
0 commit comments