@@ -14,6 +14,7 @@ let act;
1414let use ;
1515let startTransition ;
1616let React ;
17+ let ReactServer ;
1718let ReactNoop ;
1819let ReactNoopFlightServer ;
1920let ReactNoopFlightClient ;
@@ -25,12 +26,18 @@ let assertLog;
2526describe ( 'ReactFlight' , ( ) => {
2627 beforeEach ( ( ) => {
2728 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 ) ;
2937 React = require ( 'react' ) ;
3038 startTransition = React . startTransition ;
3139 use = React . use ;
3240 ReactNoop = require ( 'react-noop-renderer' ) ;
33- ReactNoopFlightServer = require ( 'react-noop-renderer/flight-server' ) ;
3441 ReactNoopFlightClient = require ( 'react-noop-renderer/flight-client' ) ;
3542 act = require ( 'internal-test-utils' ) . act ;
3643 Scheduler = require ( 'scheduler' ) ;
@@ -111,6 +118,19 @@ describe('ReactFlight', () => {
111118 return ctx ;
112119 }
113120
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+
114134 function clientReference ( value ) {
115135 return Object . defineProperties (
116136 function ( ) {
@@ -970,7 +990,7 @@ describe('ReactFlight', () => {
970990 const Context = React . createContext ( ) ;
971991 const ClientContext = clientReference ( Context ) ;
972992 function ServerComponent ( ) {
973- return React . useContext ( ClientContext ) ;
993+ return ReactServer . useContext ( ClientContext ) ;
974994 }
975995 expect ( ( ) => {
976996 const transport = ReactNoopFlightServer . render ( < ServerComponent /> ) ;
@@ -982,7 +1002,7 @@ describe('ReactFlight', () => {
9821002
9831003 describe ( 'Hooks' , ( ) => {
9841004 function DivWithId ( { children} ) {
985- const id = React . useId ( ) ;
1005+ const id = ReactServer . useId ( ) ;
9861006 return < div prop = { id } > { children } </ div > ;
9871007 }
9881008
@@ -1039,7 +1059,7 @@ describe('ReactFlight', () => {
10391059 // 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
10401060 // we just accept that it is a nuance of useId in Flight
10411061 function App ( ) {
1042- const id = React . useId ( ) ;
1062+ const id = ReactServer . useId ( ) ;
10431063 const div = < div prop = { id } > { id } </ div > ;
10441064 return < ClientDoublerModuleRef el = { div } /> ;
10451065 }
@@ -1076,12 +1096,12 @@ describe('ReactFlight', () => {
10761096 describe ( 'ServerContext' , ( ) => {
10771097 // @gate enableServerContext
10781098 it ( 'supports basic createServerContext usage' , async ( ) => {
1079- const ServerContext = createServerContext (
1099+ const ServerContext = createServerServerContext (
10801100 'ServerContext' ,
10811101 'hello from server' ,
10821102 ) ;
10831103 function Foo ( ) {
1084- const context = React . useContext ( ServerContext ) ;
1104+ const context = ReactServer . useContext ( ServerContext ) ;
10851105 return < div > { context } </ div > ;
10861106 }
10871107
@@ -1097,7 +1117,10 @@ describe('ReactFlight', () => {
10971117
10981118 // @gate enableServerContext
10991119 it ( 'propagates ServerContext providers in flight' , async ( ) => {
1100- const ServerContext = createServerContext ( 'ServerContext' , 'default' ) ;
1120+ const ServerContext = createServerServerContext (
1121+ 'ServerContext' ,
1122+ 'default' ,
1123+ ) ;
11011124
11021125 function Foo ( ) {
11031126 return (
@@ -1109,7 +1132,7 @@ describe('ReactFlight', () => {
11091132 ) ;
11101133 }
11111134 function Bar ( ) {
1112- const context = React . useContext ( ServerContext ) ;
1135+ const context = ReactServer . useContext ( ServerContext ) ;
11131136 return context ;
11141137 }
11151138
@@ -1125,7 +1148,7 @@ describe('ReactFlight', () => {
11251148
11261149 // @gate enableServerContext
11271150 it ( 'errors if you try passing JSX through ServerContext value' , ( ) => {
1128- const ServerContext = createServerContext ( 'ServerContext' , {
1151+ const ServerContext = createServerServerContext ( 'ServerContext' , {
11291152 foo : {
11301153 bar : < span > hi this is default</ span > ,
11311154 } ,
@@ -1146,7 +1169,7 @@ describe('ReactFlight', () => {
11461169 ) ;
11471170 }
11481171 function Bar ( ) {
1149- const context = React . useContext ( ServerContext ) ;
1172+ const context = ReactServer . useContext ( ServerContext ) ;
11501173 return context . foo . bar ;
11511174 }
11521175
@@ -1159,7 +1182,10 @@ describe('ReactFlight', () => {
11591182
11601183 // @gate enableServerContext
11611184 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+ ) ;
11631189
11641190 function Foo ( ) {
11651191 return (
@@ -1181,7 +1207,7 @@ describe('ReactFlight', () => {
11811207 ) ;
11821208 }
11831209 function Bar ( ) {
1184- const context = React . useContext ( ServerContext ) ;
1210+ const context = ReactServer . useContext ( ServerContext ) ;
11851211 return < span > { context } </ span > ;
11861212 }
11871213
@@ -1203,7 +1229,10 @@ describe('ReactFlight', () => {
12031229
12041230 // @gate enableServerContext
12051231 it ( 'propagates ServerContext providers in flight after suspending' , async ( ) => {
1206- const ServerContext = createServerContext ( 'ServerContext' , 'default' ) ;
1232+ const ServerContext = createServerServerContext (
1233+ 'ServerContext' ,
1234+ 'default' ,
1235+ ) ;
12071236
12081237 function Foo ( ) {
12091238 return (
@@ -1231,7 +1260,7 @@ describe('ReactFlight', () => {
12311260 throw promise ;
12321261 }
12331262 Scheduler . log ( 'rendered' ) ;
1234- const context = React . useContext ( ServerContext ) ;
1263+ const context = ReactServer . useContext ( ServerContext ) ;
12351264 return context ;
12361265 }
12371266
@@ -1248,8 +1277,6 @@ describe('ReactFlight', () => {
12481277 assertLog ( [ 'rendered' ] ) ;
12491278
12501279 await act ( async ( ) => {
1251- ServerContext . _currentRenderer = null ;
1252- ServerContext . _currentRenderer2 = null ;
12531280 ReactNoop . render ( await ReactNoopFlightClient . read ( transport ) ) ;
12541281 } ) ;
12551282
@@ -1258,11 +1285,15 @@ describe('ReactFlight', () => {
12581285
12591286 // @gate enableServerContext
12601287 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' ) ;
12621293
12631294 function ClientBar ( ) {
12641295 Scheduler . log ( 'ClientBar' ) ;
1265- const context = React . useContext ( ServerContext ) ;
1296+ const context = React . useContext ( ClientContext ) ;
12661297 return < span > { context } </ span > ;
12671298 }
12681299
@@ -1285,8 +1316,6 @@ describe('ReactFlight', () => {
12851316 assertLog ( [ ] ) ;
12861317
12871318 await act ( async ( ) => {
1288- ServerContext . _currentRenderer = null ;
1289- ServerContext . _currentRenderer2 = null ;
12901319 const flightModel = await ReactNoopFlightClient . read ( transport ) ;
12911320 ReactNoop . render ( flightModel . foo ) ;
12921321 } ) ;
@@ -1301,9 +1330,12 @@ describe('ReactFlight', () => {
13011330
13021331 // @gate enableServerContext
13031332 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+ ) ;
13051337 function Bar ( ) {
1306- return < span > { React . useContext ( ServerContext ) } </ span > ;
1338+ return < span > { ReactServer . useContext ( ServerContext ) } </ span > ;
13071339 }
13081340 const transport = ReactNoopFlightServer . render ( < Bar /> , {
13091341 context : [ [ 'ServerContext' , 'Override' ] ] ,
@@ -1321,7 +1353,7 @@ describe('ReactFlight', () => {
13211353 let ServerContext ;
13221354 function inlineLazyServerContextInitialization ( ) {
13231355 if ( ! ServerContext ) {
1324- ServerContext = createServerContext ( 'ServerContext' , 'default' ) ;
1356+ ServerContext = createServerServerContext ( 'ServerContext' , 'default' ) ;
13251357 }
13261358 return ServerContext ;
13271359 }
@@ -1346,7 +1378,7 @@ describe('ReactFlight', () => {
13461378 return (
13471379 < article >
13481380 < div >
1349- { React . useContext ( inlineLazyServerContextInitialization ( ) ) }
1381+ { ReactServer . useContext ( inlineLazyServerContextInitialization ( ) ) }
13501382 </ div >
13511383 < Baz />
13521384 </ article >
@@ -1381,11 +1413,17 @@ describe('ReactFlight', () => {
13811413 // Reset all modules, except flight-modules which keeps the registry of Client Components
13821414 const flightModules = require ( 'react-noop-renderer/flight-modules' ) ;
13831415 jest . resetModules ( ) ;
1416+ jest . mock ( 'react' , ( ) => require ( 'react/react.shared-subset' ) ) ;
13841417 jest . mock ( 'react-noop-renderer/flight-modules' , ( ) => flightModules ) ;
13851418
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 ) ;
13861425 React = require ( 'react' ) ;
13871426 ReactNoop = require ( 'react-noop-renderer' ) ;
1388- ReactNoopFlightServer = require ( 'react-noop-renderer/flight-server' ) ;
13891427 ReactNoopFlightClient = require ( 'react-noop-renderer/flight-client' ) ;
13901428 act = require ( 'internal-test-utils' ) . act ;
13911429 Scheduler = require ( 'scheduler' ) ;
0 commit comments