react/packages/react-devtools-shared/src/__tests__/profilingCache-test.js

848 lines
24 KiB
JavaScript
Raw Normal View History

2019-08-28 01:54:01 +08:00
/**
* 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.
*
* @flow
*/
import typeof ReactTestRenderer from 'react-test-renderer';
2019-08-14 08:58:03 +08:00
import type {FrontendBridge} from 'react-devtools-shared/src/bridge';
2019-08-14 06:59:43 +08:00
import type Store from 'react-devtools-shared/src/devtools/store';
describe('ProfilingCache', () => {
let PropTypes;
let React;
let ReactDOM;
Move createRoot/hydrateRoot to react-dom/client (#23385) * Move createRoot/hydrateRoot to /client We want these APIs ideally to be imported separately from things you might use in arbitrary components (like flushSync). Those other methods are "isomorphic" to how the ReactDOM tree is rendered. Similar to hooks. E.g. importing flushSync into a component that only uses it on the client should ideally not also pull in the entry client implementation on the server. This also creates a nicer parity with /server where the roots are in a separate entry point. Unfortunately, I can't quite do this yet because we have some legacy APIs that we plan on removing (like findDOMNode) and we also haven't implemented flushSync using a flag like startTransition does yet. Another problem is that we currently encourage these APIs to be aliased by /profiling (or unstable_testing). In the future you don't have to alias them because you can just change your roots to just import those APIs and they'll still work with the isomorphic forms. Although we might also just use export conditions for them. For that all to work, I went with a different strategy for now where the real API is in / but it comes with a warning if you use it. If you instead import /client it disables the warning in a wrapper. That means that if you alias / then import /client that will inturn import the alias and it'll just work. In a future breaking changes (likely when we switch to ESM) we can just remove createRoot/hydrateRoot from / and move away from the aliasing strategy. * Update tests to import from react-dom/client * Fix fixtures * Update warnings * Add test for the warning * Update devtools * Change order of react-dom, react-dom/client alias I think the order matters here. The first one takes precedence. * Require react-dom through client so it can be aliased Co-authored-by: Andrew Clark <git@andrewclark.io>
2022-03-01 13:13:28 +08:00
let ReactDOMClient;
let Scheduler;
let TestRenderer: ReactTestRenderer;
let bridge: FrontendBridge;
let legacyRender;
let store: Store;
let utils;
beforeEach(() => {
utils = require('./utils');
utils.beforeEachProfiling();
legacyRender = utils.legacyRender;
bridge = global.bridge;
store = global.store;
store.collapseNodesByDefault = false;
store.recordChangeDescriptions = true;
PropTypes = require('prop-types');
React = require('react');
ReactDOM = require('react-dom');
Move createRoot/hydrateRoot to react-dom/client (#23385) * Move createRoot/hydrateRoot to /client We want these APIs ideally to be imported separately from things you might use in arbitrary components (like flushSync). Those other methods are "isomorphic" to how the ReactDOM tree is rendered. Similar to hooks. E.g. importing flushSync into a component that only uses it on the client should ideally not also pull in the entry client implementation on the server. This also creates a nicer parity with /server where the roots are in a separate entry point. Unfortunately, I can't quite do this yet because we have some legacy APIs that we plan on removing (like findDOMNode) and we also haven't implemented flushSync using a flag like startTransition does yet. Another problem is that we currently encourage these APIs to be aliased by /profiling (or unstable_testing). In the future you don't have to alias them because you can just change your roots to just import those APIs and they'll still work with the isomorphic forms. Although we might also just use export conditions for them. For that all to work, I went with a different strategy for now where the real API is in / but it comes with a warning if you use it. If you instead import /client it disables the warning in a wrapper. That means that if you alias / then import /client that will inturn import the alias and it'll just work. In a future breaking changes (likely when we switch to ESM) we can just remove createRoot/hydrateRoot from / and move away from the aliasing strategy. * Update tests to import from react-dom/client * Fix fixtures * Update warnings * Add test for the warning * Update devtools * Change order of react-dom, react-dom/client alias I think the order matters here. The first one takes precedence. * Require react-dom through client so it can be aliased Co-authored-by: Andrew Clark <git@andrewclark.io>
2022-03-01 13:13:28 +08:00
ReactDOMClient = require('react-dom/client');
Scheduler = require('scheduler');
TestRenderer = utils.requireTestRenderer();
});
2019-05-22 23:05:34 +08:00
it('should collect data for each root (including ones added or mounted after profiling started)', () => {
2019-08-14 08:58:03 +08:00
const Parent = ({count}) => {
Scheduler.unstable_advanceTime(10);
const children = new Array(count)
.fill(true)
.map((_, index) => <Child key={index} duration={index} />);
return (
<React.Fragment>
{children}
<MemoizedChild duration={1} />
</React.Fragment>
);
};
2019-08-14 08:58:03 +08:00
const Child = ({duration}) => {
Scheduler.unstable_advanceTime(duration);
return null;
};
const MemoizedChild = React.memo(Child);
2019-05-22 23:05:34 +08:00
const containerA = document.createElement('div');
const containerB = document.createElement('div');
const containerC = document.createElement('div');
utils.act(() => legacyRender(<Parent count={2} />, containerA));
utils.act(() => legacyRender(<Parent count={1} />, containerB));
utils.act(() => store.profilerStore.startProfiling());
utils.act(() => legacyRender(<Parent count={3} />, containerA));
utils.act(() => legacyRender(<Parent count={1} />, containerC));
utils.act(() => legacyRender(<Parent count={1} />, containerA));
2019-05-22 23:05:34 +08:00
utils.act(() => ReactDOM.unmountComponentAtNode(containerB));
utils.act(() => legacyRender(<Parent count={0} />, containerA));
utils.act(() => store.profilerStore.stopProfiling());
const allProfilingDataForRoots = [];
2019-08-14 08:58:03 +08:00
function Validator({previousProfilingDataForRoot, rootID}) {
2019-05-22 23:05:34 +08:00
const profilingDataForRoot = store.profilerStore.getDataForRoot(rootID);
if (previousProfilingDataForRoot != null) {
expect(profilingDataForRoot).toEqual(previousProfilingDataForRoot);
} else {
2019-05-22 23:05:34 +08:00
expect(profilingDataForRoot).toMatchSnapshot(
2019-08-14 08:58:03 +08:00
`Data for root ${profilingDataForRoot.displayName}`,
2019-05-22 23:05:34 +08:00
);
}
2019-05-22 23:05:34 +08:00
allProfilingDataForRoots.push(profilingDataForRoot);
return null;
}
2019-05-22 23:05:34 +08:00
const dataForRoots =
store.profilerStore.profilingData !== null
? store.profilerStore.profilingData.dataForRoots
: null;
2019-05-22 23:05:34 +08:00
expect(dataForRoots).not.toBeNull();
2019-05-22 23:05:34 +08:00
if (dataForRoots !== null) {
dataForRoots.forEach(dataForRoot => {
utils.act(() =>
TestRenderer.create(
<Validator
previousProfilingDataForRoot={null}
rootID={dataForRoot.rootID}
2019-08-14 08:58:03 +08:00
/>,
),
2019-05-22 23:05:34 +08:00
);
});
}
expect(allProfilingDataForRoots).toHaveLength(3);
2019-05-22 23:05:34 +08:00
utils.exportImportHelper(bridge, store);
allProfilingDataForRoots.forEach(profilingDataForRoot => {
utils.act(() =>
TestRenderer.create(
<Validator
previousProfilingDataForRoot={profilingDataForRoot}
2019-05-22 23:05:34 +08:00
rootID={profilingDataForRoot.rootID}
2019-08-14 08:58:03 +08:00
/>,
),
2019-05-22 23:05:34 +08:00
);
});
});
2019-05-22 23:05:34 +08:00
it('should collect data for each commit', () => {
2019-08-14 08:58:03 +08:00
const Parent = ({count}) => {
Scheduler.unstable_advanceTime(10);
const children = new Array(count)
.fill(true)
.map((_, index) => <Child key={index} duration={index} />);
return (
<React.Fragment>
{children}
<MemoizedChild duration={1} />
</React.Fragment>
);
};
2019-08-14 08:58:03 +08:00
const Child = ({duration}) => {
Scheduler.unstable_advanceTime(duration);
return null;
};
const MemoizedChild = React.memo(Child);
const container = document.createElement('div');
utils.act(() => store.profilerStore.startProfiling());
utils.act(() => legacyRender(<Parent count={2} />, container));
utils.act(() => legacyRender(<Parent count={3} />, container));
utils.act(() => legacyRender(<Parent count={1} />, container));
utils.act(() => legacyRender(<Parent count={0} />, container));
utils.act(() => store.profilerStore.stopProfiling());
const allCommitData = [];
2019-08-14 08:58:03 +08:00
function Validator({commitIndex, previousCommitDetails, rootID}) {
const commitData = store.profilerStore.getCommitData(rootID, commitIndex);
if (previousCommitDetails != null) {
expect(commitData).toEqual(previousCommitDetails);
} else {
allCommitData.push(commitData);
expect(commitData).toMatchSnapshot(
2019-08-14 08:58:03 +08:00
`CommitDetails commitIndex: ${commitIndex}`,
);
}
return null;
}
const rootID = store.roots[0];
for (let commitIndex = 0; commitIndex < 4; commitIndex++) {
2019-05-22 23:05:34 +08:00
utils.act(() => {
TestRenderer.create(
2019-05-22 23:05:34 +08:00
<Validator
commitIndex={commitIndex}
previousCommitDetails={null}
rootID={rootID}
2019-08-14 08:58:03 +08:00
/>,
);
});
}
expect(allCommitData).toHaveLength(4);
2019-05-22 23:05:34 +08:00
utils.exportImportHelper(bridge, store);
for (let commitIndex = 0; commitIndex < 4; commitIndex++) {
2019-05-22 23:05:34 +08:00
utils.act(() => {
TestRenderer.create(
2019-05-22 23:05:34 +08:00
<Validator
commitIndex={commitIndex}
previousCommitDetails={allCommitData[commitIndex]}
rootID={rootID}
2019-08-14 08:58:03 +08:00
/>,
);
});
}
});
2019-06-09 02:41:39 +08:00
it('should record changed props/state/context/hooks', () => {
let instance = null;
const ModernContext = React.createContext(0);
class LegacyContextProvider extends React.Component<
any,
2019-08-14 08:58:03 +08:00
{|count: number|},
> {
static childContextTypes = {
count: PropTypes.number,
};
2019-08-14 08:58:03 +08:00
state = {count: 0};
getChildContext() {
return this.state;
}
render() {
instance = this;
return (
<ModernContext.Provider value={this.state.count}>
<React.Fragment>
<ModernContextConsumer />
<LegacyContextConsumer />
</React.Fragment>
</ModernContext.Provider>
);
}
}
2019-08-14 08:58:03 +08:00
const FunctionComponentWithHooks = ({count}) => {
React.useMemo(() => count, [count]);
return null;
};
class ModernContextConsumer extends React.Component<any> {
static contextType = ModernContext;
render() {
return <FunctionComponentWithHooks count={this.context} />;
}
}
class LegacyContextConsumer extends React.Component<any> {
static contextTypes = {
count: PropTypes.number,
};
render() {
return <FunctionComponentWithHooks count={this.context.count} />;
}
}
const container = document.createElement('div');
utils.act(() => store.profilerStore.startProfiling());
utils.act(() => legacyRender(<LegacyContextProvider />, container));
expect(instance).not.toBeNull();
2019-08-14 08:58:03 +08:00
utils.act(() => (instance: any).setState({count: 1}));
2019-06-09 02:41:39 +08:00
utils.act(() =>
legacyRender(<LegacyContextProvider foo={123} />, container),
2019-06-09 02:41:39 +08:00
);
utils.act(() =>
legacyRender(<LegacyContextProvider bar="abc" />, container),
2019-06-09 02:41:39 +08:00
);
utils.act(() => legacyRender(<LegacyContextProvider />, container));
utils.act(() => store.profilerStore.stopProfiling());
const allCommitData = [];
2019-08-14 08:58:03 +08:00
function Validator({commitIndex, previousCommitDetails, rootID}) {
const commitData = store.profilerStore.getCommitData(rootID, commitIndex);
if (previousCommitDetails != null) {
expect(commitData).toEqual(previousCommitDetails);
} else {
allCommitData.push(commitData);
expect(commitData).toMatchSnapshot(
2019-08-14 08:58:03 +08:00
`CommitDetails commitIndex: ${commitIndex}`,
);
}
return null;
}
const rootID = store.roots[0];
2019-06-09 02:41:39 +08:00
for (let commitIndex = 0; commitIndex < 5; commitIndex++) {
utils.act(() => {
TestRenderer.create(
<Validator
commitIndex={commitIndex}
previousCommitDetails={null}
rootID={rootID}
2019-08-14 08:58:03 +08:00
/>,
);
});
}
2019-06-09 02:41:39 +08:00
expect(allCommitData).toHaveLength(5);
utils.exportImportHelper(bridge, store);
2019-06-09 02:41:39 +08:00
for (let commitIndex = 0; commitIndex < 5; commitIndex++) {
utils.act(() => {
TestRenderer.create(
<Validator
commitIndex={commitIndex}
previousCommitDetails={allCommitData[commitIndex]}
rootID={rootID}
2019-08-14 08:58:03 +08:00
/>,
);
});
}
});
it('should properly detect changed hooks', () => {
const Context = React.createContext(0);
const Context2 = React.createContext(0);
function reducer(state, action) {
switch (action.type) {
case 'invert':
return {value: !state.value};
default:
throw new Error();
}
}
let dispatch = null;
let setState = null;
const Component = ({count, string}) => {
// These hooks may change and initiate re-renders.
setState = React.useState('abc')[1];
dispatch = React.useReducer(reducer, {value: true})[1];
// This hook's return value may change between renders,
// but the hook itself isn't stateful.
React.useContext(Context);
React.useContext(Context2);
// These hooks and their dependencies may not change between renders.
// We're using them to ensure that they don't trigger false positives.
React.useCallback(() => () => {}, [string]);
React.useMemo(() => string, [string]);
// These hooks never "change".
React.useEffect(() => {}, [string]);
React.useLayoutEffect(() => {}, [string]);
return null;
};
const container = document.createElement('div');
utils.act(() => store.profilerStore.startProfiling());
utils.act(() =>
legacyRender(
<Context.Provider value={true}>
<Context2.Provider value={true}>
<Component count={1} />
</Context2.Provider>
</Context.Provider>,
container,
),
);
// Second render has no changed hooks, only changed props.
utils.act(() =>
legacyRender(
<Context.Provider value={true}>
<Context2.Provider value={true}>
<Component count={2} />
</Context2.Provider>
</Context.Provider>,
container,
),
);
// Third render has a changed reducer hook
utils.act(() => dispatch({type: 'invert'}));
// Fourth render has a changed state hook
utils.act(() => setState('def'));
// Fifth render has a changed context value for context 1, but no changed hook.
utils.act(() =>
legacyRender(
<Context.Provider value={false}>
<Context2.Provider value={true}>
<Component count={2} />
</Context2.Provider>
</Context.Provider>,
container,
),
);
// Sixth render has another changed context value for context 2, but no changed hook.
utils.act(() =>
legacyRender(
<Context.Provider value={false}>
<Context2.Provider value={false}>
<Component count={2} />
</Context2.Provider>
</Context.Provider>,
container,
),
);
utils.act(() => store.profilerStore.stopProfiling());
const allCommitData = [];
function Validator({commitIndex, previousCommitDetails, rootID}) {
const commitData = store.profilerStore.getCommitData(rootID, commitIndex);
if (previousCommitDetails != null) {
expect(commitData).toEqual(previousCommitDetails);
} else {
allCommitData.push(commitData);
expect(commitData).toMatchSnapshot(
`CommitDetails commitIndex: ${commitIndex}`,
);
}
return null;
}
const rootID = store.roots[0];
for (let commitIndex = 0; commitIndex < 6; commitIndex++) {
utils.act(() => {
TestRenderer.create(
<Validator
commitIndex={commitIndex}
previousCommitDetails={null}
rootID={rootID}
/>,
);
});
}
expect(allCommitData).toHaveLength(6);
// Export and re-import profile data and make sure it is retained.
utils.exportImportHelper(bridge, store);
for (let commitIndex = 0; commitIndex < 6; commitIndex++) {
utils.act(() => {
TestRenderer.create(
<Validator
commitIndex={commitIndex}
previousCommitDetails={allCommitData[commitIndex]}
rootID={rootID}
/>,
);
});
}
});
2019-05-22 23:05:34 +08:00
it('should calculate a self duration based on actual children (not filtered children)', () => {
store.componentFilters = [utils.createDisplayNameFilter('^Parent$')];
const Grandparent = () => {
Scheduler.unstable_advanceTime(10);
return (
<React.Fragment>
<Parent key="one" />
<Parent key="two" />
</React.Fragment>
);
};
const Parent = () => {
Scheduler.unstable_advanceTime(2);
return <Child />;
};
const Child = () => {
Scheduler.unstable_advanceTime(1);
return null;
};
utils.act(() => store.profilerStore.startProfiling());
utils.act(() =>
legacyRender(<Grandparent />, document.createElement('div')),
);
utils.act(() => store.profilerStore.stopProfiling());
let commitData = null;
2019-08-14 08:58:03 +08:00
function Validator({commitIndex, rootID}) {
commitData = store.profilerStore.getCommitData(rootID, commitIndex);
expect(commitData).toMatchSnapshot(
2019-08-14 08:58:03 +08:00
`CommitDetails with filtered self durations`,
);
return null;
}
const rootID = store.roots[0];
2019-05-22 23:05:34 +08:00
utils.act(() => {
TestRenderer.create(<Validator commitIndex={0} rootID={rootID} />);
});
expect(commitData).not.toBeNull();
});
it('should calculate self duration correctly for suspended views', async () => {
let data;
const getData = () => {
if (data) {
return data;
} else {
throw new Promise(resolve => {
data = 'abc';
resolve(data);
});
}
};
const Parent = () => {
Scheduler.unstable_advanceTime(10);
return (
<React.Suspense fallback={<Fallback />}>
<Async />
</React.Suspense>
);
};
const Fallback = () => {
Scheduler.unstable_advanceTime(2);
return 'Fallback...';
};
const Async = () => {
Scheduler.unstable_advanceTime(3);
2019-08-14 12:59:07 +08:00
return getData();
};
utils.act(() => store.profilerStore.startProfiling());
await utils.actAsync(() =>
legacyRender(<Parent />, document.createElement('div')),
);
utils.act(() => store.profilerStore.stopProfiling());
const allCommitData = [];
2019-08-14 08:58:03 +08:00
function Validator({commitIndex, rootID}) {
const commitData = store.profilerStore.getCommitData(rootID, commitIndex);
allCommitData.push(commitData);
expect(commitData).toMatchSnapshot(
2019-08-14 08:58:03 +08:00
`CommitDetails with filtered self durations`,
);
return null;
}
const rootID = store.roots[0];
for (let commitIndex = 0; commitIndex < 2; commitIndex++) {
2019-05-22 23:05:34 +08:00
utils.act(() => {
TestRenderer.create(
2019-08-14 08:58:03 +08:00
<Validator commitIndex={commitIndex} rootID={rootID} />,
);
});
}
expect(allCommitData).toHaveLength(2);
});
2019-05-22 23:05:34 +08:00
it('should collect data for each rendered fiber', () => {
2019-08-14 08:58:03 +08:00
const Parent = ({count}) => {
Scheduler.unstable_advanceTime(10);
const children = new Array(count)
.fill(true)
.map((_, index) => <Child key={index} duration={index} />);
return (
<React.Fragment>
{children}
<MemoizedChild duration={1} />
</React.Fragment>
);
};
2019-08-14 08:58:03 +08:00
const Child = ({duration}) => {
Scheduler.unstable_advanceTime(duration);
return null;
};
const MemoizedChild = React.memo(Child);
const container = document.createElement('div');
utils.act(() => store.profilerStore.startProfiling());
utils.act(() => legacyRender(<Parent count={1} />, container));
utils.act(() => legacyRender(<Parent count={2} />, container));
utils.act(() => legacyRender(<Parent count={3} />, container));
utils.act(() => store.profilerStore.stopProfiling());
const allFiberCommits = [];
2019-08-14 08:58:03 +08:00
function Validator({fiberID, previousFiberCommits, rootID}) {
const fiberCommits = store.profilerStore.profilingCache.getFiberCommits({
fiberID,
rootID,
});
if (previousFiberCommits != null) {
expect(fiberCommits).toEqual(previousFiberCommits);
} else {
allFiberCommits.push(fiberCommits);
expect(fiberCommits).toMatchSnapshot(
2019-08-14 08:58:03 +08:00
`FiberCommits: element ${fiberID}`,
);
}
return null;
}
const rootID = store.roots[0];
for (let index = 0; index < store.numElements; index++) {
2019-05-22 23:05:34 +08:00
utils.act(() => {
const fiberID = store.getElementIDAtIndex(index);
if (fiberID == null) {
throw Error(`Unexpected null ID for element at index ${index}`);
}
TestRenderer.create(
2019-05-22 23:05:34 +08:00
<Validator
fiberID={fiberID}
previousFiberCommits={null}
rootID={rootID}
2019-08-14 08:58:03 +08:00
/>,
);
});
}
expect(allFiberCommits).toHaveLength(store.numElements);
2019-05-22 23:05:34 +08:00
utils.exportImportHelper(bridge, store);
for (let index = 0; index < store.numElements; index++) {
2019-05-22 23:05:34 +08:00
utils.act(() => {
const fiberID = store.getElementIDAtIndex(index);
if (fiberID == null) {
throw Error(`Unexpected null ID for element at index ${index}`);
}
TestRenderer.create(
2019-05-22 23:05:34 +08:00
<Validator
fiberID={fiberID}
previousFiberCommits={allFiberCommits[index]}
rootID={rootID}
2019-08-14 08:58:03 +08:00
/>,
);
});
}
});
it('should handle unexpectedly shallow suspense trees', () => {
const container = document.createElement('div');
utils.act(() => store.profilerStore.startProfiling());
utils.act(() => legacyRender(<React.Suspense />, container));
utils.act(() => store.profilerStore.stopProfiling());
function Validator({commitIndex, rootID}) {
const profilingDataForRoot = store.profilerStore.getDataForRoot(rootID);
expect(profilingDataForRoot).toMatchSnapshot('Empty Suspense node');
return null;
}
const rootID = store.roots[0];
utils.act(() => {
TestRenderer.create(<Validator commitIndex={0} rootID={rootID} />);
});
});
// See https://github.com/facebook/react/issues/18831
it('should not crash during route transitions with Suspense', () => {
const RouterContext = React.createContext();
function App() {
return (
<Router>
<Switch>
<Route path="/">
<Home />
</Route>
<Route path="/about">
<About />
</Route>
</Switch>
</Router>
);
}
const Home = () => {
return (
<React.Suspense>
<Link path="/about">Home</Link>
</React.Suspense>
);
};
const About = () => <div>About</div>;
// Mimics https://github.com/ReactTraining/react-router/blob/master/packages/react-router/modules/Router.js
function Router({children}) {
const [path, setPath] = React.useState('/');
return (
<RouterContext.Provider value={{path, setPath}}>
{children}
</RouterContext.Provider>
);
}
// Mimics https://github.com/ReactTraining/react-router/blob/master/packages/react-router/modules/Switch.js
function Switch({children}) {
return (
<RouterContext.Consumer>
{context => {
let element = null;
React.Children.forEach(children, child => {
if (context.path === child.props.path) {
element = child.props.children;
}
});
return element ? React.cloneElement(element) : null;
}}
</RouterContext.Consumer>
);
}
// Mimics https://github.com/ReactTraining/react-router/blob/master/packages/react-router/modules/Route.js
function Route({children, path}) {
return null;
}
const linkRef = React.createRef();
// Mimics https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/modules/Link.js
function Link({children, path}) {
return (
<RouterContext.Consumer>
{context => {
return (
<button ref={linkRef} onClick={() => context.setPath(path)}>
{children}
</button>
);
}}
</RouterContext.Consumer>
);
}
const {Simulate} = require('react-dom/test-utils');
const container = document.createElement('div');
utils.act(() => legacyRender(<App />, container));
expect(container.textContent).toBe('Home');
utils.act(() => store.profilerStore.startProfiling());
utils.act(() => Simulate.click(linkRef.current));
utils.act(() => store.profilerStore.stopProfiling());
expect(container.textContent).toBe('About');
});
it('components that were deleted and added to updaters during the layout phase should not crash', () => {
let setChildUnmounted;
function Child() {
const [, setState] = React.useState(false);
React.useLayoutEffect(() => {
return () => setState(true);
});
return null;
}
function App() {
const [childUnmounted, _setChildUnmounted] = React.useState(false);
setChildUnmounted = _setChildUnmounted;
return <>{!childUnmounted && <Child />}</>;
}
Move createRoot/hydrateRoot to react-dom/client (#23385) * Move createRoot/hydrateRoot to /client We want these APIs ideally to be imported separately from things you might use in arbitrary components (like flushSync). Those other methods are "isomorphic" to how the ReactDOM tree is rendered. Similar to hooks. E.g. importing flushSync into a component that only uses it on the client should ideally not also pull in the entry client implementation on the server. This also creates a nicer parity with /server where the roots are in a separate entry point. Unfortunately, I can't quite do this yet because we have some legacy APIs that we plan on removing (like findDOMNode) and we also haven't implemented flushSync using a flag like startTransition does yet. Another problem is that we currently encourage these APIs to be aliased by /profiling (or unstable_testing). In the future you don't have to alias them because you can just change your roots to just import those APIs and they'll still work with the isomorphic forms. Although we might also just use export conditions for them. For that all to work, I went with a different strategy for now where the real API is in / but it comes with a warning if you use it. If you instead import /client it disables the warning in a wrapper. That means that if you alias / then import /client that will inturn import the alias and it'll just work. In a future breaking changes (likely when we switch to ESM) we can just remove createRoot/hydrateRoot from / and move away from the aliasing strategy. * Update tests to import from react-dom/client * Fix fixtures * Update warnings * Add test for the warning * Update devtools * Change order of react-dom, react-dom/client alias I think the order matters here. The first one takes precedence. * Require react-dom through client so it can be aliased Co-authored-by: Andrew Clark <git@andrewclark.io>
2022-03-01 13:13:28 +08:00
const root = ReactDOMClient.createRoot(document.createElement('div'));
utils.act(() => root.render(<App />));
utils.act(() => store.profilerStore.startProfiling());
utils.act(() => setChildUnmounted(true));
utils.act(() => store.profilerStore.stopProfiling());
const updaters = store.profilerStore.getCommitData(store.roots[0], 0)
.updaters;
expect(updaters.length).toEqual(1);
expect(updaters[0].displayName).toEqual('App');
});
it('components in a deleted subtree and added to updaters during the layout phase should not crash', () => {
let setChildUnmounted;
function Child() {
return <GrandChild />;
}
function GrandChild() {
const [, setState] = React.useState(false);
React.useLayoutEffect(() => {
return () => setState(true);
});
return null;
}
function App() {
const [childUnmounted, _setChildUnmounted] = React.useState(false);
setChildUnmounted = _setChildUnmounted;
return <>{!childUnmounted && <Child />}</>;
}
Move createRoot/hydrateRoot to react-dom/client (#23385) * Move createRoot/hydrateRoot to /client We want these APIs ideally to be imported separately from things you might use in arbitrary components (like flushSync). Those other methods are "isomorphic" to how the ReactDOM tree is rendered. Similar to hooks. E.g. importing flushSync into a component that only uses it on the client should ideally not also pull in the entry client implementation on the server. This also creates a nicer parity with /server where the roots are in a separate entry point. Unfortunately, I can't quite do this yet because we have some legacy APIs that we plan on removing (like findDOMNode) and we also haven't implemented flushSync using a flag like startTransition does yet. Another problem is that we currently encourage these APIs to be aliased by /profiling (or unstable_testing). In the future you don't have to alias them because you can just change your roots to just import those APIs and they'll still work with the isomorphic forms. Although we might also just use export conditions for them. For that all to work, I went with a different strategy for now where the real API is in / but it comes with a warning if you use it. If you instead import /client it disables the warning in a wrapper. That means that if you alias / then import /client that will inturn import the alias and it'll just work. In a future breaking changes (likely when we switch to ESM) we can just remove createRoot/hydrateRoot from / and move away from the aliasing strategy. * Update tests to import from react-dom/client * Fix fixtures * Update warnings * Add test for the warning * Update devtools * Change order of react-dom, react-dom/client alias I think the order matters here. The first one takes precedence. * Require react-dom through client so it can be aliased Co-authored-by: Andrew Clark <git@andrewclark.io>
2022-03-01 13:13:28 +08:00
const root = ReactDOMClient.createRoot(document.createElement('div'));
utils.act(() => root.render(<App />));
utils.act(() => store.profilerStore.startProfiling());
utils.act(() => setChildUnmounted(true));
utils.act(() => store.profilerStore.stopProfiling());
const updaters = store.profilerStore.getCommitData(store.roots[0], 0)
.updaters;
expect(updaters.length).toEqual(1);
expect(updaters[0].displayName).toEqual('App');
});
it('components that were deleted should not be added to updaters during the passive phase', () => {
let setChildUnmounted;
function Child() {
const [, setState] = React.useState(false);
React.useEffect(() => {
return () => setState(true);
});
return null;
}
function App() {
const [childUnmounted, _setChildUnmounted] = React.useState(false);
setChildUnmounted = _setChildUnmounted;
return <>{!childUnmounted && <Child />}</>;
}
Move createRoot/hydrateRoot to react-dom/client (#23385) * Move createRoot/hydrateRoot to /client We want these APIs ideally to be imported separately from things you might use in arbitrary components (like flushSync). Those other methods are "isomorphic" to how the ReactDOM tree is rendered. Similar to hooks. E.g. importing flushSync into a component that only uses it on the client should ideally not also pull in the entry client implementation on the server. This also creates a nicer parity with /server where the roots are in a separate entry point. Unfortunately, I can't quite do this yet because we have some legacy APIs that we plan on removing (like findDOMNode) and we also haven't implemented flushSync using a flag like startTransition does yet. Another problem is that we currently encourage these APIs to be aliased by /profiling (or unstable_testing). In the future you don't have to alias them because you can just change your roots to just import those APIs and they'll still work with the isomorphic forms. Although we might also just use export conditions for them. For that all to work, I went with a different strategy for now where the real API is in / but it comes with a warning if you use it. If you instead import /client it disables the warning in a wrapper. That means that if you alias / then import /client that will inturn import the alias and it'll just work. In a future breaking changes (likely when we switch to ESM) we can just remove createRoot/hydrateRoot from / and move away from the aliasing strategy. * Update tests to import from react-dom/client * Fix fixtures * Update warnings * Add test for the warning * Update devtools * Change order of react-dom, react-dom/client alias I think the order matters here. The first one takes precedence. * Require react-dom through client so it can be aliased Co-authored-by: Andrew Clark <git@andrewclark.io>
2022-03-01 13:13:28 +08:00
const root = ReactDOMClient.createRoot(document.createElement('div'));
utils.act(() => root.render(<App />));
utils.act(() => store.profilerStore.startProfiling());
utils.act(() => setChildUnmounted(true));
utils.act(() => store.profilerStore.stopProfiling());
const updaters = store.profilerStore.getCommitData(store.roots[0], 0)
.updaters;
expect(updaters.length).toEqual(1);
expect(updaters[0].displayName).toEqual('App');
});
});