-
-
Notifications
You must be signed in to change notification settings - Fork 629
Expand file tree
/
Copy pathserverRenderRSCReactComponent.test.js
More file actions
190 lines (160 loc) · 6.41 KB
/
Copy pathserverRenderRSCReactComponent.test.js
File metadata and controls
190 lines (160 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import path from 'path';
import fs from 'fs';
import { Readable } from 'stream';
import { buildVM, getVMContext, resetVM } from '../src/worker/vm';
import { getConfig } from '../src/shared/configBuilder';
const SimpleWorkingComponent = () => 'hello';
const ComponentWithSyncError = () => {
throw new Error('Sync error');
};
const ComponentWithAsyncError = async () => {
await new Promise((resolve) => {
setTimeout(resolve, 0);
});
throw new Error('Async error');
};
describe('serverRenderRSCReactComponent', () => {
let tempDir;
let tempRscBundlePath;
let tempManifestPath;
beforeAll(async () => {
// Create temporary directory
tempDir = path.join(process.cwd(), 'tmp/node-renderer-bundles-test/testing-bundle');
fs.mkdirSync(tempDir, { recursive: true });
// Copy rsc-bundle.js to temp directory
const originalRscBundlePath = path.join(
__dirname,
'../../../react_on_rails_pro/spec/dummy/ssr-generated/rsc-bundle.js',
);
tempRscBundlePath = path.join(tempDir, 'rsc-bundle.js');
fs.copyFileSync(originalRscBundlePath, tempRscBundlePath);
// Copy react-client-manifest.json to temp directory
const originalManifestPath = path.join(
__dirname,
'../../../react_on_rails_pro/spec/dummy/public/webpack/test/react-client-manifest.json',
);
tempManifestPath = path.join(tempDir, 'react-client-manifest.json');
fs.copyFileSync(originalManifestPath, tempManifestPath);
});
afterAll(async () => {
// Clean up temporary directory
if (tempDir && fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true, force: true });
}
});
beforeEach(async () => {
const config = getConfig();
config.supportModules = true;
config.maxVMPoolSize = 2; // Set a small pool size for testing
config.stubTimers = false;
});
afterEach(async () => {
resetVM();
});
// The serverRenderRSCReactComponent function should only be called when the bundle is compiled with the `react-server` condition.
// Therefore, we cannot call it directly in the test files. Instead, we run the RSC bundle through the VM and call the method from there.
const getReactOnRailsRSCObject = async () => {
// Use the copied rsc-bundle.js file from temp directory
await buildVM(tempRscBundlePath);
const vmContext = getVMContext(tempRscBundlePath);
const { ReactOnRails, React } = vmContext.context;
function SuspensedComponentWithAsyncError() {
return React.createElement('div', null, [
React.createElement('div', null, 'Hello'),
React.createElement(
React.Suspense,
{
fallback: React.createElement('div', null, 'Loading Async Component...'),
},
React.createElement(ComponentWithAsyncError),
),
]);
}
ReactOnRails.register({
SimpleWorkingComponent,
ComponentWithSyncError,
ComponentWithAsyncError,
SuspensedComponentWithAsyncError,
});
return ReactOnRails;
};
const renderComponent = async (componentName, throwJsErrors = false) => {
const ReactOnRails = await getReactOnRailsRSCObject();
return ReactOnRails.serverRenderRSCReactComponent({
name: componentName,
props: {},
throwJsErrors,
railsContext: {
serverSide: true,
reactClientManifestFileName: path.basename(tempManifestPath),
reactServerClientManifestFileName: 'react-server-client-manifest.json',
renderingReturnsPromises: true,
},
});
};
it('ReactOnRails should be defined and have serverRenderRSCReactComponent method', async () => {
const result = await getReactOnRailsRSCObject();
expect(result).toBeDefined();
expect(typeof result.serverRenderRSCReactComponent).toBe('function');
});
// Add these helper functions at the top of the describe block
const getStreamContent = async (stream) => {
let content = '';
stream.on('data', (chunk) => {
content += chunk.toString();
});
await new Promise((resolve) => {
stream.on('end', resolve);
});
return content;
};
const expectStreamContent = async (stream, expectedContents, options = {}) => {
const { throwJsErrors, expectedError } = options;
expect(stream).toBeDefined();
expect(stream).toBeInstanceOf(Readable);
const onError = throwJsErrors ? jest.fn() : null;
if (onError) {
stream.on('error', onError);
}
const content = await getStreamContent(stream);
if (expectedError) {
expect(onError).toHaveBeenCalled();
const [emittedError] = onError.mock.calls[0];
expect(Object.prototype.toString.call(emittedError)).toBe('[object Error]');
expect(emittedError.message).toBe(expectedError);
}
expectedContents.forEach((text) => {
expect(content).toContain(text);
});
};
it('should returns stream with content when the component renders successfully', async () => {
const result = await renderComponent('SimpleWorkingComponent');
await expectStreamContent(result, ['hello']);
});
it('should returns stream with error when the component throws a sync error', async () => {
const result = await renderComponent('ComponentWithSyncError');
await expectStreamContent(result, ['Sync error']);
});
it('should emit an error when the component throws a sync error and throwJsErrors is true', async () => {
const result = await renderComponent('ComponentWithSyncError', true);
await expectStreamContent(result, ['Sync error'], {
throwJsErrors: true,
expectedError: 'Sync error',
});
});
it('should emit an error when the component throws an async error and throwJsErrors is true', async () => {
const result = await renderComponent('ComponentWithAsyncError', true);
await expectStreamContent(result, ['Async error'], { throwJsErrors: true, expectedError: 'Async error' });
});
it('should render a suspense component with an async error', async () => {
const result = await renderComponent('SuspensedComponentWithAsyncError');
await expectStreamContent(result, ['Loading Async Component...', 'Hello', 'Async error']);
});
it('emits an error when the suspense component throws an async error and throwJsErrors is true', async () => {
const result = await renderComponent('SuspensedComponentWithAsyncError', true);
await expectStreamContent(result, ['Loading Async Component...', 'Hello', 'Async error'], {
throwJsErrors: true,
expectedError: 'Async error',
});
});
});