@@ -13,6 +13,8 @@ const ReactDOM = require('react-dom');
13
13
14
14
// Isolate test renderer.
15
15
jest . resetModules ( ) ;
16
+ const React = require ( 'react' ) ;
17
+ const ReactCache = require ( 'react-cache' ) ;
16
18
const ReactTestRenderer = require ( 'react-test-renderer' ) ;
17
19
18
20
describe ( 'ReactTestRenderer' , ( ) => {
@@ -26,4 +28,76 @@ describe('ReactTestRenderer', () => {
26
28
withoutStack : true ,
27
29
} ) ;
28
30
} ) ;
31
+
32
+ describe ( 'timed out Suspense hidden subtrees should not be observable via toJSON' , ( ) => {
33
+ let AsyncText ;
34
+ let PendingResources ;
35
+ let TextResource ;
36
+
37
+ beforeEach ( ( ) => {
38
+ PendingResources = { } ;
39
+ TextResource = ReactCache . unstable_createResource (
40
+ text =>
41
+ new Promise ( resolve => {
42
+ PendingResources [ text ] = resolve ;
43
+ } ) ,
44
+ text => text ,
45
+ ) ;
46
+
47
+ AsyncText = ( { text} ) => {
48
+ const value = TextResource . read ( text ) ;
49
+ return value ;
50
+ } ;
51
+ } ) ;
52
+
53
+ it ( 'for root Suspense components' , async done => {
54
+ const App = ( { text} ) => {
55
+ return (
56
+ < React . Suspense fallback = "fallback" >
57
+ < AsyncText text = { text } />
58
+ </ React . Suspense >
59
+ ) ;
60
+ } ;
61
+
62
+ const root = ReactTestRenderer . create ( < App text = "initial" /> ) ;
63
+ PendingResources . initial ( 'initial' ) ;
64
+ await Promise . resolve ( ) ;
65
+ expect ( root . toJSON ( ) ) . toEqual ( 'initial' ) ;
66
+
67
+ root . update ( < App text = "dynamic" /> ) ;
68
+ expect ( root . toJSON ( ) ) . toEqual ( 'fallback' ) ;
69
+
70
+ PendingResources . dynamic ( 'dynamic' ) ;
71
+ await Promise . resolve ( ) ;
72
+ expect ( root . toJSON ( ) ) . toEqual ( 'dynamic' ) ;
73
+
74
+ done ( ) ;
75
+ } ) ;
76
+
77
+ it ( 'for nested Suspense components' , async done => {
78
+ const App = ( { text} ) => {
79
+ return (
80
+ < div >
81
+ < React . Suspense fallback = "fallback" >
82
+ < AsyncText text = { text } />
83
+ </ React . Suspense >
84
+ </ div >
85
+ ) ;
86
+ } ;
87
+
88
+ const root = ReactTestRenderer . create ( < App text = "initial" /> ) ;
89
+ PendingResources . initial ( 'initial' ) ;
90
+ await Promise . resolve ( ) ;
91
+ expect ( root . toJSON ( ) . children ) . toEqual ( [ 'initial' ] ) ;
92
+
93
+ root . update ( < App text = "dynamic" /> ) ;
94
+ expect ( root . toJSON ( ) . children ) . toEqual ( [ 'fallback' ] ) ;
95
+
96
+ PendingResources . dynamic ( 'dynamic' ) ;
97
+ await Promise . resolve ( ) ;
98
+ expect ( root . toJSON ( ) . children ) . toEqual ( [ 'dynamic' ] ) ;
99
+
100
+ done ( ) ;
101
+ } ) ;
102
+ } ) ;
29
103
} ) ;
0 commit comments