@@ -14,6 +14,7 @@ let act;
14
14
let use ;
15
15
let startTransition ;
16
16
let React ;
17
+ let ReactServer ;
17
18
let ReactNoop ;
18
19
let ReactNoopFlightServer ;
19
20
let ReactNoopFlightClient ;
@@ -25,12 +26,18 @@ let assertLog;
25
26
describe ( 'ReactFlight' , ( ) => {
26
27
beforeEach ( ( ) => {
27
28
jest . resetModules ( ) ;
28
-
29
+ jest . mock ( 'react' , ( ) => require ( 'react/react.shared-subset' ) ) ;
30
+ ReactServer = require ( 'react' ) ;
31
+ ReactNoopFlightServer = require ( 'react-noop-renderer/flight-server' ) ;
32
+ // This stores the state so we need to preserve it
33
+ const flightModules = require ( 'react-noop-renderer/flight-modules' ) ;
34
+ __unmockReact ( ) ;
35
+ jest . resetModules ( ) ;
36
+ jest . mock ( 'react-noop-renderer/flight-modules' , ( ) => flightModules ) ;
29
37
React = require ( 'react' ) ;
30
38
startTransition = React . startTransition ;
31
39
use = React . use ;
32
40
ReactNoop = require ( 'react-noop-renderer' ) ;
33
- ReactNoopFlightServer = require ( 'react-noop-renderer/flight-server' ) ;
34
41
ReactNoopFlightClient = require ( 'react-noop-renderer/flight-client' ) ;
35
42
act = require ( 'internal-test-utils' ) . act ;
36
43
Scheduler = require ( 'scheduler' ) ;
@@ -111,6 +118,19 @@ describe('ReactFlight', () => {
111
118
return ctx ;
112
119
}
113
120
121
+ function createServerServerContext ( globalName , defaultValue , withStack ) {
122
+ let ctx ;
123
+ expect ( ( ) => {
124
+ ctx = ReactServer . createServerContext ( globalName , defaultValue ) ;
125
+ } ) . toErrorDev (
126
+ 'Server Context is deprecated and will soon be removed. ' +
127
+ 'It was never documented and we have found it not to be useful ' +
128
+ 'enough to warrant the downside it imposes on all apps.' ,
129
+ { withoutStack : ! withStack } ,
130
+ ) ;
131
+ return ctx ;
132
+ }
133
+
114
134
function clientReference ( value ) {
115
135
return Object . defineProperties (
116
136
function ( ) {
@@ -970,7 +990,7 @@ describe('ReactFlight', () => {
970
990
const Context = React . createContext ( ) ;
971
991
const ClientContext = clientReference ( Context ) ;
972
992
function ServerComponent ( ) {
973
- return React . useContext ( ClientContext ) ;
993
+ return ReactServer . useContext ( ClientContext ) ;
974
994
}
975
995
expect ( ( ) => {
976
996
const transport = ReactNoopFlightServer . render ( < ServerComponent /> ) ;
@@ -982,7 +1002,7 @@ describe('ReactFlight', () => {
982
1002
983
1003
describe ( 'Hooks' , ( ) => {
984
1004
function DivWithId ( { children} ) {
985
- const id = React . useId ( ) ;
1005
+ const id = ReactServer . useId ( ) ;
986
1006
return < div prop = { id } > { children } </ div > ;
987
1007
}
988
1008
@@ -1039,7 +1059,7 @@ describe('ReactFlight', () => {
1039
1059
// so the output passed to the Client has no knowledge of the useId use. In the future we would like to add a DEV warning when this happens. For now
1040
1060
// we just accept that it is a nuance of useId in Flight
1041
1061
function App ( ) {
1042
- const id = React . useId ( ) ;
1062
+ const id = ReactServer . useId ( ) ;
1043
1063
const div = < div prop = { id } > { id } </ div > ;
1044
1064
return < ClientDoublerModuleRef el = { div } /> ;
1045
1065
}
@@ -1076,12 +1096,12 @@ describe('ReactFlight', () => {
1076
1096
describe ( 'ServerContext' , ( ) => {
1077
1097
// @gate enableServerContext
1078
1098
it ( 'supports basic createServerContext usage' , async ( ) => {
1079
- const ServerContext = createServerContext (
1099
+ const ServerContext = createServerServerContext (
1080
1100
'ServerContext' ,
1081
1101
'hello from server' ,
1082
1102
) ;
1083
1103
function Foo ( ) {
1084
- const context = React . useContext ( ServerContext ) ;
1104
+ const context = ReactServer . useContext ( ServerContext ) ;
1085
1105
return < div > { context } </ div > ;
1086
1106
}
1087
1107
@@ -1097,7 +1117,10 @@ describe('ReactFlight', () => {
1097
1117
1098
1118
// @gate enableServerContext
1099
1119
it ( 'propagates ServerContext providers in flight' , async ( ) => {
1100
- const ServerContext = createServerContext ( 'ServerContext' , 'default' ) ;
1120
+ const ServerContext = createServerServerContext (
1121
+ 'ServerContext' ,
1122
+ 'default' ,
1123
+ ) ;
1101
1124
1102
1125
function Foo ( ) {
1103
1126
return (
@@ -1109,7 +1132,7 @@ describe('ReactFlight', () => {
1109
1132
) ;
1110
1133
}
1111
1134
function Bar ( ) {
1112
- const context = React . useContext ( ServerContext ) ;
1135
+ const context = ReactServer . useContext ( ServerContext ) ;
1113
1136
return context ;
1114
1137
}
1115
1138
@@ -1125,7 +1148,7 @@ describe('ReactFlight', () => {
1125
1148
1126
1149
// @gate enableServerContext
1127
1150
it ( 'errors if you try passing JSX through ServerContext value' , ( ) => {
1128
- const ServerContext = createServerContext ( 'ServerContext' , {
1151
+ const ServerContext = createServerServerContext ( 'ServerContext' , {
1129
1152
foo : {
1130
1153
bar : < span > hi this is default</ span > ,
1131
1154
} ,
@@ -1146,7 +1169,7 @@ describe('ReactFlight', () => {
1146
1169
) ;
1147
1170
}
1148
1171
function Bar ( ) {
1149
- const context = React . useContext ( ServerContext ) ;
1172
+ const context = ReactServer . useContext ( ServerContext ) ;
1150
1173
return context . foo . bar ;
1151
1174
}
1152
1175
@@ -1159,7 +1182,10 @@ describe('ReactFlight', () => {
1159
1182
1160
1183
// @gate enableServerContext
1161
1184
it ( 'propagates ServerContext and cleans up the providers in flight' , async ( ) => {
1162
- const ServerContext = createServerContext ( 'ServerContext' , 'default' ) ;
1185
+ const ServerContext = createServerServerContext (
1186
+ 'ServerContext' ,
1187
+ 'default' ,
1188
+ ) ;
1163
1189
1164
1190
function Foo ( ) {
1165
1191
return (
@@ -1181,7 +1207,7 @@ describe('ReactFlight', () => {
1181
1207
) ;
1182
1208
}
1183
1209
function Bar ( ) {
1184
- const context = React . useContext ( ServerContext ) ;
1210
+ const context = ReactServer . useContext ( ServerContext ) ;
1185
1211
return < span > { context } </ span > ;
1186
1212
}
1187
1213
@@ -1203,7 +1229,10 @@ describe('ReactFlight', () => {
1203
1229
1204
1230
// @gate enableServerContext
1205
1231
it ( 'propagates ServerContext providers in flight after suspending' , async ( ) => {
1206
- const ServerContext = createServerContext ( 'ServerContext' , 'default' ) ;
1232
+ const ServerContext = createServerServerContext (
1233
+ 'ServerContext' ,
1234
+ 'default' ,
1235
+ ) ;
1207
1236
1208
1237
function Foo ( ) {
1209
1238
return (
@@ -1231,7 +1260,7 @@ describe('ReactFlight', () => {
1231
1260
throw promise ;
1232
1261
}
1233
1262
Scheduler . log ( 'rendered' ) ;
1234
- const context = React . useContext ( ServerContext ) ;
1263
+ const context = ReactServer . useContext ( ServerContext ) ;
1235
1264
return context ;
1236
1265
}
1237
1266
@@ -1248,8 +1277,6 @@ describe('ReactFlight', () => {
1248
1277
assertLog ( [ 'rendered' ] ) ;
1249
1278
1250
1279
await act ( async ( ) => {
1251
- ServerContext . _currentRenderer = null ;
1252
- ServerContext . _currentRenderer2 = null ;
1253
1280
ReactNoop . render ( await ReactNoopFlightClient . read ( transport ) ) ;
1254
1281
} ) ;
1255
1282
@@ -1258,11 +1285,15 @@ describe('ReactFlight', () => {
1258
1285
1259
1286
// @gate enableServerContext
1260
1287
it ( 'serializes ServerContext to client' , async ( ) => {
1261
- const ServerContext = createServerContext ( 'ServerContext' , 'default' ) ;
1288
+ const ServerContext = createServerServerContext (
1289
+ 'ServerContext' ,
1290
+ 'default' ,
1291
+ ) ;
1292
+ const ClientContext = createServerContext ( 'ServerContext' , 'default' ) ;
1262
1293
1263
1294
function ClientBar ( ) {
1264
1295
Scheduler . log ( 'ClientBar' ) ;
1265
- const context = React . useContext ( ServerContext ) ;
1296
+ const context = React . useContext ( ClientContext ) ;
1266
1297
return < span > { context } </ span > ;
1267
1298
}
1268
1299
@@ -1285,8 +1316,6 @@ describe('ReactFlight', () => {
1285
1316
assertLog ( [ ] ) ;
1286
1317
1287
1318
await act ( async ( ) => {
1288
- ServerContext . _currentRenderer = null ;
1289
- ServerContext . _currentRenderer2 = null ;
1290
1319
const flightModel = await ReactNoopFlightClient . read ( transport ) ;
1291
1320
ReactNoop . render ( flightModel . foo ) ;
1292
1321
} ) ;
@@ -1301,9 +1330,12 @@ describe('ReactFlight', () => {
1301
1330
1302
1331
// @gate enableServerContext
1303
1332
it ( 'takes ServerContext from the client for refetching use cases' , async ( ) => {
1304
- const ServerContext = createServerContext ( 'ServerContext' , 'default' ) ;
1333
+ const ServerContext = createServerServerContext (
1334
+ 'ServerContext' ,
1335
+ 'default' ,
1336
+ ) ;
1305
1337
function Bar ( ) {
1306
- return < span > { React . useContext ( ServerContext ) } </ span > ;
1338
+ return < span > { ReactServer . useContext ( ServerContext ) } </ span > ;
1307
1339
}
1308
1340
const transport = ReactNoopFlightServer . render ( < Bar /> , {
1309
1341
context : [ [ 'ServerContext' , 'Override' ] ] ,
@@ -1321,7 +1353,7 @@ describe('ReactFlight', () => {
1321
1353
let ServerContext ;
1322
1354
function inlineLazyServerContextInitialization ( ) {
1323
1355
if ( ! ServerContext ) {
1324
- ServerContext = createServerContext ( 'ServerContext' , 'default' ) ;
1356
+ ServerContext = createServerServerContext ( 'ServerContext' , 'default' ) ;
1325
1357
}
1326
1358
return ServerContext ;
1327
1359
}
@@ -1346,7 +1378,7 @@ describe('ReactFlight', () => {
1346
1378
return (
1347
1379
< article >
1348
1380
< div >
1349
- { React . useContext ( inlineLazyServerContextInitialization ( ) ) }
1381
+ { ReactServer . useContext ( inlineLazyServerContextInitialization ( ) ) }
1350
1382
</ div >
1351
1383
< Baz />
1352
1384
</ article >
@@ -1381,11 +1413,17 @@ describe('ReactFlight', () => {
1381
1413
// Reset all modules, except flight-modules which keeps the registry of Client Components
1382
1414
const flightModules = require ( 'react-noop-renderer/flight-modules' ) ;
1383
1415
jest . resetModules ( ) ;
1416
+ jest . mock ( 'react' , ( ) => require ( 'react/react.shared-subset' ) ) ;
1384
1417
jest . mock ( 'react-noop-renderer/flight-modules' , ( ) => flightModules ) ;
1385
1418
1419
+ ReactServer = require ( 'react' ) ;
1420
+ ReactNoopFlightServer = require ( 'react-noop-renderer/flight-server' ) ;
1421
+
1422
+ __unmockReact ( ) ;
1423
+ jest . resetModules ( ) ;
1424
+ jest . mock ( 'react-noop-renderer/flight-modules' , ( ) => flightModules ) ;
1386
1425
React = require ( 'react' ) ;
1387
1426
ReactNoop = require ( 'react-noop-renderer' ) ;
1388
- ReactNoopFlightServer = require ( 'react-noop-renderer/flight-server' ) ;
1389
1427
ReactNoopFlightClient = require ( 'react-noop-renderer/flight-client' ) ;
1390
1428
act = require ( 'internal-test-utils' ) . act ;
1391
1429
Scheduler = require ( 'scheduler' ) ;
0 commit comments