Skip to content

Commit

Permalink
DevTools cleanup (#17283)
Browse files Browse the repository at this point in the history
1. Add a Store test for memo, lazy, and forwardRef components
2. Remove dead code for React.lazy
3. Update DT tests to include HOC badge names in the serialized store
  • Loading branch information
Brian Vaughn authored Nov 6, 2019
1 parent cd1bdcd commit 3452706
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -612,3 +612,15 @@ exports[`Store should properly serialize non-string key values: 1: mount 1`] = `
[root]
<Child key="123">
`;

exports[`Store should show the right display names for special component types 1`] = `
[root]
▾ <App>
<MyComponent>
<MyComponent> [ForwardRef]
<MyComponent> [Memo]
▾ <MyComponent> [Memo]
<MyComponent> [ForwardRef]
▾ <Suspense>
<MyComponent>
`;
38 changes: 38 additions & 0 deletions packages/react-devtools-shared/src/__tests__/store-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -848,4 +848,42 @@ describe('Store', () => {
act(() => ReactDOM.render([fauxElement], document.createElement('div')));
expect(store).toMatchSnapshot('1: mount');
});

it('should show the right display names for special component types', async done => {
async function fakeImport(result) {
return {default: result};
}

const MyComponent = (props, ref) => null;
const FowardRefComponent = React.forwardRef(MyComponent);
const MemoComponent = React.memo(MyComponent);
const MemoForwardRefComponent = React.memo(FowardRefComponent);
const LazyComponent = React.lazy(() => fakeImport(MyComponent));

const App = () => (
<React.Fragment>
<MyComponent />
<FowardRefComponent />
<MemoComponent />
<MemoForwardRefComponent />
<React.Suspense fallback="Loading...">
<LazyComponent />
</React.Suspense>
</React.Fragment>
);

const container = document.createElement('div');

// Render once to start fetching the lazy component
act(() => ReactDOM.render(<App />, container));

await Promise.resolve();

// Render again after it resolves
act(() => ReactDOM.render(<App />, container));

expect(store).toMatchSnapshot();

done();
});
});
5 changes: 0 additions & 5 deletions packages/react-devtools-shared/src/backend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,6 @@ export function getInternalReactConstants(
} = ReactSymbols;

function resolveFiberType(type: any) {
// This is to support lazy components with a Promise as the type.
// see https://github.com/facebook/react/pull/13397
if (typeof type.then === 'function') {
return type._reactResult;
}
const typeSymbol = getTypeSymbol(type);
switch (typeSymbol) {
case MEMO_NUMBER:
Expand Down
24 changes: 22 additions & 2 deletions packages/react-devtools-shared/src/devtools/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
* @flow
*/

import {
ElementTypeForwardRef,
ElementTypeMemo,
} from 'react-devtools-shared/src/types';

import type {Element} from './views/Components/types';
import type Store from './store';

Expand All @@ -21,10 +26,25 @@ export function printElement(element: Element, includeWeight: boolean = false) {
key = ` key="${element.key}"`;
}

let hocs = '';
let hocDisplayNames = null;
if (element.hocDisplayNames !== null) {
hocs = ` [${element.hocDisplayNames.join('][')}]`;
hocDisplayNames = [...element.hocDisplayNames];
}
if (element.type === ElementTypeMemo) {
if (hocDisplayNames === null) {
hocDisplayNames = ['Memo'];
} else {
hocDisplayNames.push('Memo');
}
} else if (element.type === ElementTypeForwardRef) {
if (hocDisplayNames === null) {
hocDisplayNames = ['ForwardRef'];
} else {
hocDisplayNames.push('ForwardRef');
}
}

let hocs = hocDisplayNames === null ? '' : ` [${hocDisplayNames.join('][')}]`;

let suffix = '';
if (includeWeight) {
Expand Down

0 comments on commit 3452706

Please sign in to comment.