@@ -11,7 +11,6 @@ let useMemo;
11
11
let useEffect ;
12
12
let Suspense ;
13
13
let startTransition ;
14
- let cache ;
15
14
let pendingTextRequests ;
16
15
let waitFor ;
17
16
let waitForPaint ;
@@ -34,7 +33,6 @@ describe('ReactUse', () => {
34
33
useEffect = React . useEffect ;
35
34
Suspense = React . Suspense ;
36
35
startTransition = React . startTransition ;
37
- cache = React . cache ;
38
36
39
37
const InternalTestUtils = require ( 'internal-test-utils' ) ;
40
38
waitForAll = InternalTestUtils . waitForAll ;
@@ -643,10 +641,10 @@ describe('ReactUse', () => {
643
641
} ) ;
644
642
645
643
test ( 'when waiting for data to resolve, an update on a different root does not cause work to be dropped' , async ( ) => {
646
- const getCachedAsyncText = cache ( getAsyncText ) ;
644
+ const promise = getAsyncText ( 'Hi' ) ;
647
645
648
646
function App ( ) {
649
- return < Text text = { use ( getCachedAsyncText ( 'Hi' ) ) } /> ;
647
+ return < Text text = { use ( promise ) } /> ;
650
648
}
651
649
652
650
const root1 = ReactNoop . createRoot ( ) ;
@@ -998,39 +996,46 @@ describe('ReactUse', () => {
998
996
) ;
999
997
1000
998
test ( 'load multiple nested Suspense boundaries' , async ( ) => {
1001
- const getCachedAsyncText = cache ( getAsyncText ) ;
999
+ const promiseA = getAsyncText ( 'A' ) ;
1000
+ const promiseB = getAsyncText ( 'B' ) ;
1001
+ const promiseC = getAsyncText ( 'C' ) ;
1002
+ assertLog ( [
1003
+ 'Async text requested [A]' ,
1004
+ 'Async text requested [B]' ,
1005
+ 'Async text requested [C]' ,
1006
+ ] ) ;
1002
1007
1003
- function AsyncText ( { text } ) {
1004
- return < Text text = { use ( getCachedAsyncText ( text ) ) } /> ;
1008
+ function AsyncText ( { promise } ) {
1009
+ return < Text text = { use ( promise ) } /> ;
1005
1010
}
1006
1011
1007
1012
const root = ReactNoop . createRoot ( ) ;
1008
1013
await act ( ( ) => {
1009
1014
root . render (
1010
1015
< Suspense fallback = { < Text text = "(Loading A...)" /> } >
1011
- < AsyncText text = "A" />
1016
+ < AsyncText promise = { promiseA } />
1012
1017
< Suspense fallback = { < Text text = "(Loading B...)" /> } >
1013
- < AsyncText text = "B" />
1018
+ < AsyncText promise = { promiseB } />
1014
1019
< Suspense fallback = { < Text text = "(Loading C...)" /> } >
1015
- < AsyncText text = "C" />
1020
+ < AsyncText promise = { promiseC } />
1016
1021
</ Suspense >
1017
1022
</ Suspense >
1018
1023
</ Suspense > ,
1019
1024
) ;
1020
1025
} ) ;
1021
- assertLog ( [ 'Async text requested [A]' , ' (Loading A...)'] ) ;
1026
+ assertLog ( [ '(Loading A...)' ] ) ;
1022
1027
expect ( root ) . toMatchRenderedOutput ( '(Loading A...)' ) ;
1023
1028
1024
1029
await act ( ( ) => {
1025
1030
resolveTextRequests ( 'A' ) ;
1026
1031
} ) ;
1027
- assertLog ( [ 'A' , 'Async text requested [B]' , ' (Loading B...)'] ) ;
1032
+ assertLog ( [ 'A' , '(Loading B...)' ] ) ;
1028
1033
expect ( root ) . toMatchRenderedOutput ( 'A(Loading B...)' ) ;
1029
1034
1030
1035
await act ( ( ) => {
1031
1036
resolveTextRequests ( 'B' ) ;
1032
1037
} ) ;
1033
- assertLog ( [ 'B' , 'Async text requested [C]' , ' (Loading C...)'] ) ;
1038
+ assertLog ( [ 'B' , '(Loading C...)' ] ) ;
1034
1039
expect ( root ) . toMatchRenderedOutput ( 'AB(Loading C...)' ) ;
1035
1040
1036
1041
await act ( ( ) => {
@@ -1584,34 +1589,38 @@ describe('ReactUse', () => {
1584
1589
} ) ;
1585
1590
1586
1591
test ( 'regression test: updates while component is suspended should not be mistaken for render phase updates' , async ( ) => {
1587
- const getCachedAsyncText = cache ( getAsyncText ) ;
1592
+ const promiseA = getAsyncText ( 'A' ) ;
1593
+ const promiseB = getAsyncText ( 'B' ) ;
1594
+ const promiseC = getAsyncText ( 'C' ) ;
1595
+ assertLog ( [
1596
+ 'Async text requested [A]' ,
1597
+ 'Async text requested [B]' ,
1598
+ 'Async text requested [C]' ,
1599
+ ] ) ;
1588
1600
1589
1601
let setState ;
1590
1602
function App ( ) {
1591
- const [ state , _setState ] = useState ( 'A' ) ;
1603
+ const [ state , _setState ] = useState ( promiseA ) ;
1592
1604
setState = _setState ;
1593
- return < Text text = { use ( getCachedAsyncText ( state ) ) } /> ;
1605
+ return < Text text = { use ( state ) } /> ;
1594
1606
}
1595
1607
1596
1608
// Initial render
1597
1609
const root = ReactNoop . createRoot ( ) ;
1598
1610
await act ( ( ) => root . render ( < App /> ) ) ;
1599
- assertLog ( [ 'Async text requested [A]' ] ) ;
1600
1611
expect ( root ) . toMatchRenderedOutput ( null ) ;
1601
1612
await act ( ( ) => resolveTextRequests ( 'A' ) ) ;
1602
1613
assertLog ( [ 'A' ] ) ;
1603
1614
expect ( root ) . toMatchRenderedOutput ( 'A' ) ;
1604
1615
1605
1616
// Update to B. This will suspend.
1606
- await act ( ( ) => startTransition ( ( ) => setState ( 'B' ) ) ) ;
1607
- assertLog ( [ 'Async text requested [B]' ] ) ;
1617
+ await act ( ( ) => startTransition ( ( ) => setState ( promiseB ) ) ) ;
1608
1618
expect ( root ) . toMatchRenderedOutput ( 'A' ) ;
1609
1619
1610
1620
// While B is suspended, update to C. This should immediately interrupt
1611
1621
// the render for B. In the regression, this update was mistakenly treated
1612
1622
// as a render phase update.
1613
- ReactNoop . flushSync ( ( ) => setState ( 'C' ) ) ;
1614
- assertLog ( [ 'Async text requested [C]' ] ) ;
1623
+ ReactNoop . flushSync ( ( ) => setState ( promiseC ) ) ;
1615
1624
1616
1625
// Finish rendering.
1617
1626
await act ( ( ) => resolveTextRequests ( 'C' ) ) ;
0 commit comments