-
Notifications
You must be signed in to change notification settings - Fork 51k
Expand file tree
/
Copy pathReactFlightDOMRelay-test.internal.js
More file actions
234 lines (211 loc) · 5.5 KB
/
ReactFlightDOMRelay-test.internal.js
File metadata and controls
234 lines (211 loc) · 5.5 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
let act;
let React;
let ReactDOM;
let ReactDOMFlightRelayServer;
let ReactDOMFlightRelayServerRuntime;
let ReactDOMFlightRelayClient;
describe('ReactFlightDOMRelay', () => {
beforeEach(() => {
jest.resetModules();
act = require('react-dom/test-utils').act;
React = require('react');
ReactDOM = require('react-dom');
ReactDOMFlightRelayServer = require('react-transport-dom-relay/server');
ReactDOMFlightRelayServerRuntime = require('react-transport-dom-relay/server-runtime');
ReactDOMFlightRelayClient = require('react-transport-dom-relay');
});
function readThrough(data) {
const response = ReactDOMFlightRelayClient.createResponse();
for (let i = 0; i < data.length; i++) {
const chunk = data[i];
if (chunk.type === 'json') {
ReactDOMFlightRelayClient.resolveModel(response, chunk.id, chunk.json);
} else {
ReactDOMFlightRelayClient.resolveError(
response,
chunk.id,
chunk.json.message,
chunk.json.stack,
);
}
}
ReactDOMFlightRelayClient.close(response);
const model = response.readRoot();
return model;
}
function block(render, load) {
if (load === undefined) {
return ReactDOMFlightRelayServerRuntime.serverBlock(render);
}
return function(...args) {
const curriedLoad = () => {
return load(...args);
};
return ReactDOMFlightRelayServerRuntime.serverBlock(render, curriedLoad);
};
}
it('can render a server component', () => {
function Bar({text}) {
return text.toUpperCase();
}
function Foo() {
return {
bar: (
<div>
<Bar text="a" />, <Bar text="b" />
</div>
),
};
}
const transport = [];
ReactDOMFlightRelayServer.render(
{
foo: <Foo />,
},
transport,
);
const model = readThrough(transport);
expect(model).toEqual({
foo: {
bar: (
<div>
{'A'}
{', '}
{'B'}
</div>
),
},
});
});
// @gate experimental
it('can transfer a Block to the client and render there', () => {
function load(firstName, lastName) {
return {name: firstName + ' ' + lastName};
}
function User(props, data) {
return (
<span>
{props.greeting}, {data.name}
</span>
);
}
const loadUser = block(User, load);
const model = {
User: loadUser('Seb', 'Smith'),
};
const transport = [];
ReactDOMFlightRelayServer.render(model, transport);
const modelClient = readThrough(transport);
const container = document.createElement('div');
const root = ReactDOM.createRoot(container);
act(() => {
const UserClient = modelClient.User;
root.render(<UserClient greeting="Hello" />);
});
expect(container.innerHTML).toEqual('<span>Hello, Seb Smith</span>');
});
// @gate experimental
it('can reasonably handle different element types', () => {
const {
forwardRef,
memo,
Fragment,
StrictMode,
Profiler,
Suspense,
SuspenseList,
} = React;
const Inner = memo(
forwardRef((props, ref) => {
return <div ref={ref}>{'Hello ' + props.name}</div>;
}),
);
function Foo() {
return {
bar: (
<div>
<Fragment>Fragment child</Fragment>
<Profiler>Profiler child</Profiler>
<StrictMode>StrictMode child</StrictMode>
<Suspense fallback="Loading...">Suspense child</Suspense>
<SuspenseList fallback="Loading...">
{'SuspenseList row 1'}
{'SuspenseList row 2'}
</SuspenseList>
<Inner name="world" />
</div>
),
};
}
const transport = [];
ReactDOMFlightRelayServer.render(
{
foo: <Foo />,
},
transport,
);
const model = readThrough(transport);
expect(model).toEqual({
foo: {
bar: (
<div>
{'Fragment child'}
{'Profiler child'}
{'StrictMode child'}
{'Suspense child'}
{['SuspenseList row 1', 'SuspenseList row 2']}
<div>Hello world</div>
</div>
),
},
});
});
it('can handle a subset of Hooks', () => {
const {useMemo, useCallback} = React;
function Inner({x}) {
const foo = useMemo(() => x + x, [x]);
const bar = useCallback(() => 10 + foo, [foo]);
return bar();
}
function Foo() {
return {
bar: <Inner x={2} />,
};
}
const transport = [];
ReactDOMFlightRelayServer.render(
{
foo: <Foo />,
},
transport,
);
const model = readThrough(transport);
expect(model).toEqual({
foo: {
bar: 14,
},
});
});
it('can handle a subset of Hooks, with element as root', () => {
const {useMemo, useCallback} = React;
function Inner({x}) {
const foo = useMemo(() => x + x, [x]);
const bar = useCallback(() => 10 + foo, [foo]);
return bar();
}
function Foo() {
return <Inner x={2} />;
}
const transport = [];
ReactDOMFlightRelayServer.render(<Foo />, transport);
const model = readThrough(transport);
expect(model).toEqual(14);
});
});