9
9
10
10
import type Store from 'react-devtools-shared/src/devtools/store' ;
11
11
12
+ import {
13
+ getLegacyRenderImplementation ,
14
+ getModernRenderImplementation ,
15
+ } from './utils' ;
16
+
12
17
describe ( 'commit tree' , ( ) => {
13
18
let React ;
14
- let ReactDOMClient ;
15
19
let Scheduler ;
16
- let legacyRender ;
17
20
let store : Store ;
18
21
let utils ;
19
22
20
23
beforeEach ( ( ) => {
21
24
utils = require ( './utils' ) ;
22
25
utils . beforeEachProfiling ( ) ;
23
26
24
- legacyRender = utils . legacyRender ;
25
-
26
27
store = global . store ;
27
28
store . collapseNodesByDefault = false ;
28
29
store . recordChangeDescriptions = true ;
29
30
30
31
React = require ( 'react' ) ;
31
- ReactDOMClient = require ( 'react-dom/client' ) ;
32
32
Scheduler = require ( 'scheduler' ) ;
33
33
} ) ;
34
34
35
+ const { render : legacyRender } = getLegacyRenderImplementation ( ) ;
36
+ const { render : modernRender } = getModernRenderImplementation ( ) ;
37
+
35
38
// @reactVersion >= 16.9
36
- it ( 'should be able to rebuild the store tree for each commit' , ( ) => {
39
+ // @reactVersion <= 18.2
40
+ it ( 'should be able to rebuild the store tree for each commit (legacy render)' , ( ) => {
37
41
const Parent = ( { count} ) => {
38
42
Scheduler . unstable_advanceTime ( 10 ) ;
39
43
return new Array ( count )
@@ -45,31 +49,88 @@ describe('commit tree', () => {
45
49
return null ;
46
50
} ) ;
47
51
48
- const container = document . createElement ( 'div' ) ;
52
+ utils . act ( ( ) => store . profilerStore . startProfiling ( ) ) ;
53
+ utils . act ( ( ) => legacyRender ( < Parent count = { 1 } /> ) ) ;
54
+ expect ( store ) . toMatchInlineSnapshot ( `
55
+ [root]
56
+ ▾ <Parent>
57
+ <Child key="0"> [Memo]
58
+ ` ) ;
59
+ utils . act ( ( ) => legacyRender ( < Parent count = { 3 } /> ) ) ;
60
+ expect ( store ) . toMatchInlineSnapshot ( `
61
+ [root]
62
+ ▾ <Parent>
63
+ <Child key="0"> [Memo]
64
+ <Child key="1"> [Memo]
65
+ <Child key="2"> [Memo]
66
+ ` ) ;
67
+ utils . act ( ( ) => legacyRender ( < Parent count = { 2 } /> ) ) ;
68
+ expect ( store ) . toMatchInlineSnapshot ( `
69
+ [root]
70
+ ▾ <Parent>
71
+ <Child key="0"> [Memo]
72
+ <Child key="1"> [Memo]
73
+ ` ) ;
74
+ utils . act ( ( ) => legacyRender ( < Parent count = { 0 } /> ) ) ;
75
+ expect ( store ) . toMatchInlineSnapshot ( `
76
+ [root]
77
+ <Parent>
78
+ ` ) ;
79
+ utils . act ( ( ) => store . profilerStore . stopProfiling ( ) ) ;
80
+
81
+ const rootID = store . roots [ 0 ] ;
82
+ const commitTrees = [ ] ;
83
+ for ( let commitIndex = 0 ; commitIndex < 4 ; commitIndex ++ ) {
84
+ commitTrees . push (
85
+ store . profilerStore . profilingCache . getCommitTree ( {
86
+ commitIndex,
87
+ rootID,
88
+ } ) ,
89
+ ) ;
90
+ }
91
+
92
+ expect ( commitTrees [ 0 ] . nodes . size ) . toBe ( 3 ) ; // <Root> + <Parent> + <Child>
93
+ expect ( commitTrees [ 1 ] . nodes . size ) . toBe ( 5 ) ; // <Root> + <Parent> + <Child> x 3
94
+ expect ( commitTrees [ 2 ] . nodes . size ) . toBe ( 4 ) ; // <Root> + <Parent> + <Child> x 2
95
+ expect ( commitTrees [ 3 ] . nodes . size ) . toBe ( 2 ) ; // <Root> + <Parent>
96
+ } ) ;
97
+
98
+ // @reactVersion >= 18
99
+ it ( 'should be able to rebuild the store tree for each commit (createRoot)' , ( ) => {
100
+ const Parent = ( { count} ) => {
101
+ Scheduler . unstable_advanceTime ( 10 ) ;
102
+ return new Array ( count )
103
+ . fill ( true )
104
+ . map ( ( _ , index ) => < Child key = { index } /> ) ;
105
+ } ;
106
+ const Child = React . memo ( function Child ( ) {
107
+ Scheduler . unstable_advanceTime ( 2 ) ;
108
+ return null ;
109
+ } ) ;
49
110
50
111
utils . act ( ( ) => store . profilerStore . startProfiling ( ) ) ;
51
- utils . act ( ( ) => legacyRender ( < Parent count = { 1 } /> , container ) ) ;
112
+ utils . act ( ( ) => modernRender ( < Parent count = { 1 } /> ) ) ;
52
113
expect ( store ) . toMatchInlineSnapshot ( `
53
114
[root]
54
115
▾ <Parent>
55
116
<Child key="0"> [Memo]
56
117
` ) ;
57
- utils . act ( ( ) => legacyRender ( < Parent count = { 3 } /> , container ) ) ;
118
+ utils . act ( ( ) => modernRender ( < Parent count = { 3 } /> ) ) ;
58
119
expect ( store ) . toMatchInlineSnapshot ( `
59
120
[root]
60
121
▾ <Parent>
61
122
<Child key="0"> [Memo]
62
123
<Child key="1"> [Memo]
63
124
<Child key="2"> [Memo]
64
125
` ) ;
65
- utils . act ( ( ) => legacyRender ( < Parent count = { 2 } /> , container ) ) ;
126
+ utils . act ( ( ) => modernRender ( < Parent count = { 2 } /> ) ) ;
66
127
expect ( store ) . toMatchInlineSnapshot ( `
67
128
[root]
68
129
▾ <Parent>
69
130
<Child key="0"> [Memo]
70
131
<Child key="1"> [Memo]
71
132
` ) ;
72
- utils . act ( ( ) => legacyRender ( < Parent count = { 0 } /> , container ) ) ;
133
+ utils . act ( ( ) => modernRender ( < Parent count = { 0 } /> ) ) ;
73
134
expect ( store ) . toMatchInlineSnapshot ( `
74
135
[root]
75
136
<Parent>
@@ -118,25 +179,24 @@ describe('commit tree', () => {
118
179
} ) ;
119
180
120
181
// @reactVersion >= 16.9
182
+ // @reactVersion <= 18.2
121
183
it ( 'should support Lazy components (legacy render)' , async ( ) => {
122
- const container = document . createElement ( 'div' ) ;
123
-
124
184
utils . act ( ( ) => store . profilerStore . startProfiling ( ) ) ;
125
- utils . act ( ( ) => legacyRender ( < App renderChildren = { true } /> , container ) ) ;
185
+ utils . act ( ( ) => legacyRender ( < App renderChildren = { true } /> ) ) ;
126
186
await Promise . resolve ( ) ;
127
187
expect ( store ) . toMatchInlineSnapshot ( `
128
188
[root]
129
189
▾ <App>
130
190
<Suspense>
131
191
` ) ;
132
- utils . act ( ( ) => legacyRender ( < App renderChildren = { true } /> , container ) ) ;
192
+ utils . act ( ( ) => legacyRender ( < App renderChildren = { true } /> ) ) ;
133
193
expect ( store ) . toMatchInlineSnapshot ( `
134
194
[root]
135
195
▾ <App>
136
196
▾ <Suspense>
137
197
<LazyInnerComponent>
138
198
` ) ;
139
- utils . act ( ( ) => legacyRender ( < App renderChildren = { false } /> , container ) ) ;
199
+ utils . act ( ( ) => legacyRender ( < App renderChildren = { false } /> ) ) ;
140
200
expect ( store ) . toMatchInlineSnapshot ( `
141
201
[root]
142
202
<App>
@@ -161,25 +221,22 @@ describe('commit tree', () => {
161
221
162
222
// @reactVersion >= 18.0
163
223
it ( 'should support Lazy components (createRoot)' , async ( ) => {
164
- const container = document . createElement ( 'div' ) ;
165
- const root = ReactDOMClient . createRoot ( container ) ;
166
-
167
224
utils . act ( ( ) => store . profilerStore . startProfiling ( ) ) ;
168
- utils . act ( ( ) => root . render ( < App renderChildren = { true } /> ) ) ;
225
+ utils . act ( ( ) => modernRender ( < App renderChildren = { true } /> ) ) ;
169
226
await Promise . resolve ( ) ;
170
227
expect ( store ) . toMatchInlineSnapshot ( `
171
228
[root]
172
229
▾ <App>
173
230
<Suspense>
174
231
` ) ;
175
- utils . act ( ( ) => root . render ( < App renderChildren = { true } /> ) ) ;
232
+ utils . act ( ( ) => modernRender ( < App renderChildren = { true } /> ) ) ;
176
233
expect ( store ) . toMatchInlineSnapshot ( `
177
234
[root]
178
235
▾ <App>
179
236
▾ <Suspense>
180
237
<LazyInnerComponent>
181
238
` ) ;
182
- utils . act ( ( ) => root . render ( < App renderChildren = { false } /> ) ) ;
239
+ utils . act ( ( ) => modernRender ( < App renderChildren = { false } /> ) ) ;
183
240
expect ( store ) . toMatchInlineSnapshot ( `
184
241
[root]
185
242
<App>
@@ -203,17 +260,16 @@ describe('commit tree', () => {
203
260
} ) ;
204
261
205
262
// @reactVersion >= 16.9
263
+ // @reactVersion <= 18.2
206
264
it ( 'should support Lazy components that are unmounted before resolving (legacy render)' , async ( ) => {
207
- const container = document . createElement ( 'div' ) ;
208
-
209
265
utils . act ( ( ) => store . profilerStore . startProfiling ( ) ) ;
210
- utils . act ( ( ) => legacyRender ( < App renderChildren = { true } /> , container ) ) ;
266
+ utils . act ( ( ) => legacyRender ( < App renderChildren = { true } /> ) ) ;
211
267
expect ( store ) . toMatchInlineSnapshot ( `
212
268
[root]
213
269
▾ <App>
214
270
<Suspense>
215
271
` ) ;
216
- utils . act ( ( ) => legacyRender ( < App renderChildren = { false } /> , container ) ) ;
272
+ utils . act ( ( ) => legacyRender ( < App renderChildren = { false } /> ) ) ;
217
273
expect ( store ) . toMatchInlineSnapshot ( `
218
274
[root]
219
275
<App>
@@ -237,17 +293,14 @@ describe('commit tree', () => {
237
293
238
294
// @reactVersion >= 18.0
239
295
it ( 'should support Lazy components that are unmounted before resolving (createRoot)' , async ( ) => {
240
- const container = document . createElement ( 'div' ) ;
241
- const root = ReactDOMClient . createRoot ( container ) ;
242
-
243
296
utils . act ( ( ) => store . profilerStore . startProfiling ( ) ) ;
244
- utils . act ( ( ) => root . render ( < App renderChildren = { true } /> ) ) ;
297
+ utils . act ( ( ) => modernRender ( < App renderChildren = { true } /> ) ) ;
245
298
expect ( store ) . toMatchInlineSnapshot ( `
246
299
[root]
247
300
▾ <App>
248
301
<Suspense>
249
302
` ) ;
250
- utils . act ( ( ) => root . render ( < App renderChildren = { false } /> ) ) ;
303
+ utils . act ( ( ) => modernRender ( < App renderChildren = { false } /> ) ) ;
251
304
expect ( store ) . toMatchInlineSnapshot ( `
252
305
[root]
253
306
<App>
0 commit comments