Skip to content

Commit 60ba723

Browse files
authored
Add SuspenseList to devTools (#19684)
* ensure getDisplayName is only called on functions * add SuspenseList to Dev tools element names * Add SuspenseList and pass tests * Import SuspenseList directly * run prettier * Refactor tests to use real components * run linter
1 parent 5564f2c commit 60ba723

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

Diff for: packages/react-devtools-shared/src/__tests__/utils-test.js

+43-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@
77
* @flow
88
*/
99

10-
import {getDisplayName} from 'react-devtools-shared/src/utils';
10+
import {
11+
getDisplayName,
12+
getDisplayNameForReactElement,
13+
} from 'react-devtools-shared/src/utils';
14+
import {
15+
REACT_SUSPENSE_LIST_TYPE as SuspenseList,
16+
REACT_STRICT_MODE_TYPE as StrictMode,
17+
} from 'shared/ReactSymbols';
18+
import {createElement} from 'react/src/ReactElement';
1119

1220
describe('utils', () => {
1321
describe('getDisplayName', () => {
@@ -37,4 +45,38 @@ describe('utils', () => {
3745
expect(getDisplayName(FauxComponent, 'Fallback')).toEqual('Fallback');
3846
});
3947
});
48+
describe('getDisplayNameForReactElement', () => {
49+
it('should return correct display name for an element with function type', () => {
50+
function FauxComponent() {}
51+
FauxComponent.displayName = 'OverrideDisplayName';
52+
const element = createElement(FauxComponent);
53+
expect(getDisplayNameForReactElement(element)).toEqual(
54+
'OverrideDisplayName',
55+
);
56+
});
57+
it('should return correct display name for an element with a type of StrictMode', () => {
58+
const element = createElement(StrictMode);
59+
expect(getDisplayNameForReactElement(element)).toEqual('StrictMode');
60+
});
61+
it('should return correct display name for an element with a type of SuspenseList', () => {
62+
const element = createElement(SuspenseList);
63+
expect(getDisplayNameForReactElement(element)).toEqual('SuspenseList');
64+
});
65+
it('should return NotImplementedInDevtools for an element with invalid symbol type', () => {
66+
const element = createElement(Symbol('foo'));
67+
expect(getDisplayNameForReactElement(element)).toEqual(
68+
'NotImplementedInDevtools',
69+
);
70+
});
71+
it('should return NotImplementedInDevtools for an element with invalid type', () => {
72+
const element = createElement(true);
73+
expect(getDisplayNameForReactElement(element)).toEqual(
74+
'NotImplementedInDevtools',
75+
);
76+
});
77+
it('should return Element for null type', () => {
78+
const element = createElement();
79+
expect(getDisplayNameForReactElement(element)).toEqual('Element');
80+
});
81+
});
4082
});

Diff for: packages/react-devtools-shared/src/utils.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
StrictMode,
2323
Suspense,
2424
} from 'react-is';
25+
import {REACT_SUSPENSE_LIST_TYPE as SuspenseList} from 'shared/ReactSymbols';
2526
import {
2627
TREE_OPERATION_ADD,
2728
TREE_OPERATION_REMOVE,
@@ -43,7 +44,6 @@ import {
4344
} from 'react-devtools-shared/src/types';
4445
import {localStorageGetItem, localStorageSetItem} from './storage';
4546
import {meta} from './hydration';
46-
4747
import type {ComponentFilter, ElementType} from './types';
4848

4949
const cachedDisplayNames: WeakMap<Function, string> = new WeakMap();
@@ -489,12 +489,16 @@ export function getDisplayNameForReactElement(
489489
return 'StrictMode';
490490
case Suspense:
491491
return 'Suspense';
492+
case SuspenseList:
493+
return 'SuspenseList';
492494
default:
493495
const {type} = element;
494496
if (typeof type === 'string') {
495497
return type;
496-
} else if (type != null) {
498+
} else if (typeof type === 'function') {
497499
return getDisplayName(type, 'Anonymous');
500+
} else if (type != null) {
501+
return 'NotImplementedInDevtools';
498502
} else {
499503
return 'Element';
500504
}

Diff for: packages/react-is/src/ReactIs.js

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
REACT_PROVIDER_TYPE,
2222
REACT_STRICT_MODE_TYPE,
2323
REACT_SUSPENSE_TYPE,
24+
REACT_SUSPENSE_LIST_TYPE,
2425
} from 'shared/ReactSymbols';
2526
import isValidElementType from 'shared/isValidElementType';
2627

@@ -36,6 +37,7 @@ export function typeOf(object: any) {
3637
case REACT_PROFILER_TYPE:
3738
case REACT_STRICT_MODE_TYPE:
3839
case REACT_SUSPENSE_TYPE:
40+
case REACT_SUSPENSE_LIST_TYPE:
3941
return type;
4042
default:
4143
const $$typeofType = type && type.$$typeof;

0 commit comments

Comments
 (0)