Skip to content

Added a DevTools store test for component names #17283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('][')}]`;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Serialize the store in a format that includes the HOC badges, more like the Badge component does in the real DevTools UI. This will make our tests more meaningful.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is pretty. Nice.


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