-
Notifications
You must be signed in to change notification settings - Fork 51k
Expand file tree
/
Copy pathprofilingCommitTreeBuilder-test.js
More file actions
263 lines (230 loc) · 7.6 KB
/
profilingCommitTreeBuilder-test.js
File metadata and controls
263 lines (230 loc) · 7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type Store from 'react-devtools-shared/src/devtools/store';
import {getVersionedRenderImplementation} from './utils';
describe('commit tree', () => {
let React = require('react');
let Scheduler;
let store: Store;
let utils;
beforeEach(() => {
utils = require('./utils');
utils.beforeEachProfiling();
store = global.store;
store.collapseNodesByDefault = false;
store.recordChangeDescriptions = true;
React = require('react');
Scheduler = require('scheduler');
});
const {render} = getVersionedRenderImplementation();
// @reactVersion >= 16.9
it('should be able to rebuild the store tree for each commit', () => {
const Parent = ({count}) => {
Scheduler.unstable_advanceTime(10);
return new Array(count)
.fill(true)
.map((_, index) => <Child key={index} />);
};
const Child = React.memo(function Child() {
Scheduler.unstable_advanceTime(2);
return null;
});
utils.act(() => store.profilerStore.startProfiling());
utils.act(() => render(<Parent count={1} />));
expect(store).toMatchInlineSnapshot(`
[root]
▾ <Parent>
<Child key="0"> [Memo]
`);
utils.act(() => render(<Parent count={3} />));
expect(store).toMatchInlineSnapshot(`
[root]
▾ <Parent>
<Child key="0"> [Memo]
<Child key="1"> [Memo]
<Child key="2"> [Memo]
`);
utils.act(() => render(<Parent count={2} />));
expect(store).toMatchInlineSnapshot(`
[root]
▾ <Parent>
<Child key="0"> [Memo]
<Child key="1"> [Memo]
`);
utils.act(() => render(<Parent count={0} />));
expect(store).toMatchInlineSnapshot(`
[root]
<Parent>
`);
utils.act(() => store.profilerStore.stopProfiling());
const rootID = store.roots[0];
const commitTrees = [];
for (let commitIndex = 0; commitIndex < 4; commitIndex++) {
commitTrees.push(
store.profilerStore.profilingCache.getCommitTree({
commitIndex,
rootID,
}),
);
}
expect(commitTrees[0].nodes.size).toBe(3); // <Root> + <Parent> + <Child>
expect(commitTrees[1].nodes.size).toBe(5); // <Root> + <Parent> + <Child> x 3
expect(commitTrees[2].nodes.size).toBe(4); // <Root> + <Parent> + <Child> x 2
expect(commitTrees[3].nodes.size).toBe(2); // <Root> + <Parent>
});
describe('Lazy', () => {
async function fakeImport(result) {
return {default: result};
}
const LazyInnerComponent = () => null;
const App = ({renderChildren}) => {
if (renderChildren) {
return (
<React.Suspense fallback="Loading...">
<LazyComponent />
</React.Suspense>
);
} else {
return null;
}
};
let LazyComponent;
beforeEach(() => {
LazyComponent = React.lazy(() => fakeImport(LazyInnerComponent));
});
// @reactVersion >= 16.9
it('should support Lazy components', async () => {
utils.act(() => store.profilerStore.startProfiling());
utils.act(() => render(<App renderChildren={true} />));
await Promise.resolve();
expect(store).toMatchInlineSnapshot(`
[root]
▾ <App>
<Suspense>
[suspense-root] rects={null}
<Suspense name="App" uniqueSuspenders={true} rects={null}>
`);
utils.act(() => render(<App renderChildren={true} />));
expect(store).toMatchInlineSnapshot(`
[root]
▾ <App>
▾ <Suspense>
<LazyInnerComponent>
[suspense-root] rects={null}
<Suspense name="App" uniqueSuspenders={true} rects={null}>
`);
utils.act(() => render(<App renderChildren={false} />));
expect(store).toMatchInlineSnapshot(`
[root]
<App>
`);
utils.act(() => store.profilerStore.stopProfiling());
const rootID = store.roots[0];
const commitTrees = [];
for (let commitIndex = 0; commitIndex < 3; commitIndex++) {
commitTrees.push(
store.profilerStore.profilingCache.getCommitTree({
commitIndex,
rootID,
}),
);
}
expect(commitTrees[0].nodes.size).toBe(3);
expect(commitTrees[1].nodes.size).toBe(4); // <Root> + <App> + <Suspense> + <LazyInnerComponent>
expect(commitTrees[2].nodes.size).toBe(2); // <Root> + <App>
});
// @reactVersion >= 16.9
it('should support Lazy components that are unmounted before resolving', async () => {
utils.act(() => store.profilerStore.startProfiling());
utils.act(() => render(<App renderChildren={true} />));
expect(store).toMatchInlineSnapshot(`
[root]
▾ <App>
<Suspense>
[suspense-root] rects={null}
<Suspense name="App" uniqueSuspenders={true} rects={null}>
`);
utils.act(() => render(<App renderChildren={false} />));
expect(store).toMatchInlineSnapshot(`
[root]
<App>
`);
utils.act(() => store.profilerStore.stopProfiling());
const rootID = store.roots[0];
const commitTrees = [];
for (let commitIndex = 0; commitIndex < 2; commitIndex++) {
commitTrees.push(
store.profilerStore.profilingCache.getCommitTree({
commitIndex,
rootID,
}),
);
}
expect(commitTrees[0].nodes.size).toBe(3);
expect(commitTrees[1].nodes.size).toBe(2); // <Root> + <App>
});
});
describe('Suspense', () => {
it('should handle transitioning from fallback back to content during profiling', async () => {
let resolvePromise;
let promise = null;
let childTreeBaseDuration = 10;
function Wrapper({children}) {
Scheduler.unstable_advanceTime(2);
return children;
}
function Child() {
Scheduler.unstable_advanceTime(childTreeBaseDuration);
if (promise !== null) {
React.use(promise);
}
return <GrandChild />;
}
function GrandChild() {
Scheduler.unstable_advanceTime(5);
return null;
}
function Fallback() {
Scheduler.unstable_advanceTime(2);
return null;
}
function App() {
Scheduler.unstable_advanceTime(1);
return (
<React.Suspense fallback={<Fallback />}>
<Wrapper>
<Child />
</Wrapper>
</React.Suspense>
);
}
utils.act(() => store.profilerStore.startProfiling());
// Commit 1: Mount with primary
utils.act(() => render(<App step={1} />));
// Commit 2: Suspend, show fallback
promise = new Promise(resolve => (resolvePromise = resolve));
await utils.actAsync(() => render(<App step={2} />));
// Commit 3: Resolve suspended promise, show primary content with a different duration.
childTreeBaseDuration = 20;
promise = null;
await utils.actAsync(resolvePromise);
utils.act(() => render(<App step={3} />));
utils.act(() => store.profilerStore.stopProfiling());
const rootID = store.roots[0];
const dataForRoot = store.profilerStore.getDataForRoot(rootID);
const numCommits = dataForRoot.commitData.length;
for (let commitIndex = 0; commitIndex < numCommits; commitIndex++) {
store.profilerStore.profilingCache.getCommitTree({
commitIndex,
rootID,
});
}
});
});
});