Skip to content

Commit cc35c57

Browse files
author
Brian Vaughn
committed
Added a DevTools store test for component names
1 parent 4f02c93 commit cc35c57

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

packages/react-devtools-shared/src/__tests__/__snapshots__/store-test.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,15 @@ exports[`Store should properly serialize non-string key values: 1: mount 1`] = `
612612
[root]
613613
<Child key="123">
614614
`;
615+
616+
exports[`Store should show the right display names for special component types 1`] = `
617+
[root]
618+
▾ <App>
619+
<MyComponent>
620+
<MyComponent> [ForwardRef]
621+
<MyComponent> [Memo]
622+
▾ <MyComponent> [Memo]
623+
<MyComponent> [ForwardRef]
624+
▾ <Suspense>
625+
<MyComponent>
626+
`;

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,4 +848,52 @@ describe('Store', () => {
848848
act(() => ReactDOM.render([fauxElement], document.createElement('div')));
849849
expect(store).toMatchSnapshot('1: mount');
850850
});
851+
852+
it('should show the right display names for special component types', async done => {
853+
async function fakeImport(result) {
854+
return {default: result};
855+
}
856+
857+
const MyComponent = (props, ref) => null;
858+
const FowardRefComponent = React.forwardRef(MyComponent);
859+
const MemoComponent = React.memo(MyComponent);
860+
const MemoForwardRefComponent = React.memo(FowardRefComponent);
861+
const LazyComponent = React.lazy(() => fakeImport(MyComponent));
862+
863+
const App = () => (
864+
<React.Fragment>
865+
<MyComponent />
866+
<FowardRefComponent />
867+
<MemoComponent />
868+
<MemoForwardRefComponent />
869+
<React.Suspense fallback="Loading...">
870+
<LazyComponent />
871+
</React.Suspense>
872+
</React.Fragment>
873+
);
874+
875+
const container = document.createElement('div');
876+
877+
// Render once to start fetching the lazy component
878+
act(() =>
879+
ReactDOM.render(
880+
<App />,
881+
container,
882+
),
883+
);
884+
885+
await Promise.resolve();
886+
887+
// Render again after it resolves
888+
act(() =>
889+
ReactDOM.render(
890+
<App />,
891+
container,
892+
),
893+
);
894+
895+
expect(store).toMatchSnapshot();
896+
897+
done();
898+
});
851899
});

packages/react-devtools-shared/src/devtools/utils.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
* @flow
88
*/
99

10+
import {
11+
ElementTypeForwardRef,
12+
ElementTypeMemo,
13+
} from 'react-devtools-shared/src/types';
14+
1015
import type {Element} from './views/Components/types';
1116
import type Store from './store';
1217

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

24-
let hocs = '';
29+
let hocDisplayNames = null;
2530
if (element.hocDisplayNames !== null) {
26-
hocs = ` [${element.hocDisplayNames.join('][')}]`;
31+
hocDisplayNames = [...element.hocDisplayNames];
2732
}
33+
if (element.type === ElementTypeMemo) {
34+
if (hocDisplayNames === null) {
35+
hocDisplayNames = ['Memo'];
36+
} else {
37+
hocDisplayNames.push('Memo');
38+
}
39+
} else if (element.type === ElementTypeForwardRef) {
40+
if (hocDisplayNames === null) {
41+
hocDisplayNames = ['ForwardRef'];
42+
} else {
43+
hocDisplayNames.push('ForwardRef');
44+
}
45+
}
46+
47+
let hocs = hocDisplayNames === null ? '' : ` [${hocDisplayNames.join('][')}]`;
2848

2949
let suffix = '';
3050
if (includeWeight) {

0 commit comments

Comments
 (0)