forked from facebook/react
/
ReactProfilerDevToolsIntegration-test.internal.js
206 lines (173 loc) · 6.12 KB
/
ReactProfilerDevToolsIntegration-test.internal.js
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
* @jest-environment node
*/
'use strict';
describe('ReactProfiler DevTools integration', () => {
let React;
let ReactFeatureFlags;
let ReactTestRenderer;
let Scheduler;
let SchedulerTracing;
let AdvanceTime;
let hook;
beforeEach(() => {
global.__REACT_DEVTOOLS_GLOBAL_HOOK__ = hook = {
inject: () => {},
onCommitFiberRoot: jest.fn((rendererId, root) => {}),
onCommitFiberUnmount: () => {},
supportsFiber: true,
};
jest.resetModules();
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.enableProfilerTimer = true;
ReactFeatureFlags.enableSchedulerTracing = true;
Scheduler = require('scheduler');
SchedulerTracing = require('scheduler/tracing');
React = require('react');
ReactTestRenderer = require('react-test-renderer');
AdvanceTime = class extends React.Component {
static defaultProps = {
byAmount: 10,
shouldComponentUpdate: true,
};
shouldComponentUpdate(nextProps) {
return nextProps.shouldComponentUpdate;
}
render() {
// Simulate time passing when this component is rendered
Scheduler.unstable_advanceTime(this.props.byAmount);
return this.props.children || null;
}
};
});
it('should auto-Profile all fibers if the DevTools hook is detected', () => {
const App = ({multiplier}) => {
Scheduler.unstable_advanceTime(2);
return (
<React.Profiler id="Profiler" onRender={onRender}>
<AdvanceTime byAmount={3 * multiplier} shouldComponentUpdate={true} />
<AdvanceTime
byAmount={7 * multiplier}
shouldComponentUpdate={false}
/>
</React.Profiler>
);
};
const onRender = jest.fn(() => {});
const rendered = ReactTestRenderer.create(<App multiplier={1} />);
expect(hook.onCommitFiberRoot).toHaveBeenCalledTimes(1);
// Measure observable timing using the Profiler component.
// The time spent in App (above the Profiler) won't be included in the durations,
// But needs to be accounted for in the offset times.
expect(onRender).toHaveBeenCalledTimes(1);
expect(onRender).toHaveBeenCalledWith(
'Profiler',
'mount',
10,
10,
2,
12,
new Set(),
);
onRender.mockClear();
// Measure unobservable timing required by the DevTools profiler.
// At this point, the base time should include both:
// The time 2ms in the App component itself, and
// The 10ms spend in the Profiler sub-tree beneath.
expect(rendered.root.findByType(App)._currentFiber().treeBaseDuration).toBe(
12,
);
rendered.update(<App multiplier={2} />);
// Measure observable timing using the Profiler component.
// The time spent in App (above the Profiler) won't be included in the durations,
// But needs to be accounted for in the offset times.
expect(onRender).toHaveBeenCalledTimes(1);
expect(onRender).toHaveBeenCalledWith(
'Profiler',
'update',
6,
13,
14,
20,
new Set(),
);
// Measure unobservable timing required by the DevTools profiler.
// At this point, the base time should include both:
// The initial 9ms for the components that do not re-render, and
// The updated 6ms for the component that does.
expect(rendered.root.findByType(App)._currentFiber().treeBaseDuration).toBe(
15,
);
});
it('should reset the fiber stack correctly after an error when profiling host roots', () => {
Scheduler.unstable_advanceTime(20);
const rendered = ReactTestRenderer.create(
<div>
<AdvanceTime byAmount={2} />
</div>,
);
Scheduler.unstable_advanceTime(20);
expect(() => {
rendered.update(
<div ref="this-will-cause-an-error">
<AdvanceTime byAmount={3} />
</div>,
);
}).toThrow();
Scheduler.unstable_advanceTime(20);
// But this should render correctly, if the profiler's fiber stack has been reset.
rendered.update(
<div>
<AdvanceTime byAmount={7} />
</div>,
);
// Measure unobservable timing required by the DevTools profiler.
// At this point, the base time should include only the most recent (not failed) render.
// It should not include time spent on the initial render,
// Or time that elapsed between any of the above renders.
expect(
rendered.root.findByType('div')._currentFiber().treeBaseDuration,
).toBe(7);
});
it('should store traced interactions on the HostNode so DevTools can access them', () => {
// Render without an interaction
const rendered = ReactTestRenderer.create(<div />);
const root = rendered.root._currentFiber().return;
expect(root.stateNode.memoizedInteractions).toContainNoInteractions();
Scheduler.unstable_advanceTime(10);
const eventTime = Scheduler.unstable_now();
// Render with an interaction
SchedulerTracing.unstable_trace('some event', eventTime, () => {
rendered.update(<div />);
});
expect(root.stateNode.memoizedInteractions).toMatchInteractions([
{name: 'some event', timestamp: eventTime},
]);
});
it('regression test: #17159', () => {
function Text({text}) {
Scheduler.unstable_yieldValue(text);
return text;
}
const root = ReactTestRenderer.create(null, {unstable_isConcurrent: true});
// Commit something
root.update(<Text text="A" />);
expect(Scheduler).toFlushAndYield(__DEV__ ? ['A', 'A'] : ['A']);
expect(root).toMatchRenderedOutput('A');
// Advance time by many seconds, larger than the default expiration time
// for updates.
Scheduler.unstable_advanceTime(10000);
// Schedule an update.
root.update(<Text text="B" />);
// Update B should not instantly expire.
expect(Scheduler).toFlushExpired([]);
expect(Scheduler).toFlushAndYield(__DEV__ ? ['B', 'B'] : ['B']);
expect(root).toMatchRenderedOutput('B');
});
});