|
| 1 | +/** |
| 2 | + * Copyright (c) 2013-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @emails react-core |
| 8 | + */ |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +describe('ReactProfiler DevTools integration', () => { |
| 13 | + let React; |
| 14 | + let ReactFeatureFlags; |
| 15 | + let ReactTestRenderer; |
| 16 | + let AdvanceTime; |
| 17 | + let advanceTimeBy; |
| 18 | + let hook; |
| 19 | + let mockNow; |
| 20 | + |
| 21 | + const mockNowForTests = () => { |
| 22 | + let currentTime = 0; |
| 23 | + |
| 24 | + mockNow = jest.fn().mockImplementation(() => currentTime); |
| 25 | + |
| 26 | + ReactTestRenderer.unstable_setNowImplementation(mockNow); |
| 27 | + advanceTimeBy = amount => { |
| 28 | + currentTime += amount; |
| 29 | + }; |
| 30 | + }; |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + global.__REACT_DEVTOOLS_GLOBAL_HOOK__ = hook = { |
| 34 | + inject: () => {}, |
| 35 | + onCommitFiberRoot: jest.fn((rendererId, root) => {}), |
| 36 | + onCommitFiberUnmount: () => {}, |
| 37 | + supportsFiber: true, |
| 38 | + }; |
| 39 | + |
| 40 | + jest.resetModules(); |
| 41 | + |
| 42 | + ReactFeatureFlags = require('shared/ReactFeatureFlags'); |
| 43 | + ReactFeatureFlags.enableProfilerTimer = true; |
| 44 | + React = require('react'); |
| 45 | + ReactTestRenderer = require('react-test-renderer'); |
| 46 | + |
| 47 | + mockNowForTests(); |
| 48 | + |
| 49 | + AdvanceTime = class extends React.Component { |
| 50 | + static defaultProps = { |
| 51 | + byAmount: 10, |
| 52 | + shouldComponentUpdate: true, |
| 53 | + }; |
| 54 | + shouldComponentUpdate(nextProps) { |
| 55 | + return nextProps.shouldComponentUpdate; |
| 56 | + } |
| 57 | + render() { |
| 58 | + // Simulate time passing when this component is rendered |
| 59 | + advanceTimeBy(this.props.byAmount); |
| 60 | + return this.props.children || null; |
| 61 | + } |
| 62 | + }; |
| 63 | + }); |
| 64 | + |
| 65 | + it('should auto-Profile all fibers if the DevTools hook is detected', () => { |
| 66 | + const App = ({multiplier}) => { |
| 67 | + advanceTimeBy(2); |
| 68 | + return ( |
| 69 | + <React.unstable_Profiler id="Profiler" onRender={onRender}> |
| 70 | + <AdvanceTime byAmount={3 * multiplier} shouldComponentUpdate={true} /> |
| 71 | + <AdvanceTime |
| 72 | + byAmount={7 * multiplier} |
| 73 | + shouldComponentUpdate={false} |
| 74 | + /> |
| 75 | + </React.unstable_Profiler> |
| 76 | + ); |
| 77 | + }; |
| 78 | + |
| 79 | + const onRender = jest.fn(() => {}); |
| 80 | + const rendered = ReactTestRenderer.create(<App multiplier={1} />); |
| 81 | + |
| 82 | + expect(hook.onCommitFiberRoot).toHaveBeenCalledTimes(1); |
| 83 | + |
| 84 | + // Measure observable timing using the Profiler component. |
| 85 | + // The time spent in App (above the Profiler) won't be included in the durations, |
| 86 | + // But needs to be accounted for in the offset times. |
| 87 | + expect(onRender).toHaveBeenCalledTimes(1); |
| 88 | + expect(onRender).toHaveBeenCalledWith('Profiler', 'mount', 10, 10, 2, 12); |
| 89 | + onRender.mockClear(); |
| 90 | + |
| 91 | + // Measure unobservable timing required by the DevTools profiler. |
| 92 | + // At this point, the base time should include both: |
| 93 | + // The time 2ms in the App component itself, and |
| 94 | + // The 10ms spend in the Profiler sub-tree beneath. |
| 95 | + expect(rendered.root.findByType(App)._currentFiber().treeBaseTime).toBe(12); |
| 96 | + |
| 97 | + rendered.update(<App multiplier={2} />); |
| 98 | + |
| 99 | + // Measure observable timing using the Profiler component. |
| 100 | + // The time spent in App (above the Profiler) won't be included in the durations, |
| 101 | + // But needs to be accounted for in the offset times. |
| 102 | + expect(onRender).toHaveBeenCalledTimes(1); |
| 103 | + expect(onRender).toHaveBeenCalledWith('Profiler', 'update', 6, 13, 14, 20); |
| 104 | + |
| 105 | + // Measure unobservable timing required by the DevTools profiler. |
| 106 | + // At this point, the base time should include both: |
| 107 | + // The initial 9ms for the components that do not re-render, and |
| 108 | + // The updated 6ms for the component that does. |
| 109 | + expect(rendered.root.findByType(App)._currentFiber().treeBaseTime).toBe(15); |
| 110 | + }); |
| 111 | +}); |
0 commit comments