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

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_TYPEasSuspenseList,
16+
REACT_STRICT_MODE_TYPEasStrictMode,
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+
functionFauxComponent(){}
51+
FauxComponent.displayName='OverrideDisplayName';
52+
constelement=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+
constelement=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+
constelement=createElement(SuspenseList);
63+
expect(getDisplayNameForReactElement(element)).toEqual('SuspenseList');
64+
});
65+
it('should return NotImplementedInDevtools for an element with invalid symbol type',()=>{
66+
constelement=createElement(Symbol('foo'));
67+
expect(getDisplayNameForReactElement(element)).toEqual(
68+
'NotImplementedInDevtools',
69+
);
70+
});
71+
it('should return NotImplementedInDevtools for an element with invalid type',()=>{
72+
constelement=createElement(true);
73+
expect(getDisplayNameForReactElement(element)).toEqual(
74+
'NotImplementedInDevtools',
75+
);
76+
});
77+
it('should return Element for null type',()=>{
78+
constelement=createElement();
79+
expect(getDisplayNameForReactElement(element)).toEqual('Element');
80+
});
81+
});
4082
});

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_TYPEasSuspenseList}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
importtype{ComponentFilter,ElementType}from'./types';
4848

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

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
importisValidElementTypefrom'shared/isValidElementType';
2627

@@ -36,6 +37,7 @@ export function typeOf(object: any) {
3637
caseREACT_PROFILER_TYPE:
3738
caseREACT_STRICT_MODE_TYPE:
3839
caseREACT_SUSPENSE_TYPE:
40+
caseREACT_SUSPENSE_LIST_TYPE:
3941
returntype;
4042
default:
4143
const$$typeofType=type&&type.$$typeof;

0 commit comments

Comments
 (0)
close