Skip to content

Commit a22880e

Browse files
pleunvbvaughn
authored andcommitted
Add support for Suspense & lazy() to the react-is package (#14423)
* Add support for lazy & Suspense to react-is
1 parent 947bddd commit a22880e

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

packages/react-is/src/ReactIs.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ import {
1616
REACT_ELEMENT_TYPE,
1717
REACT_FORWARD_REF_TYPE,
1818
REACT_FRAGMENT_TYPE,
19+
REACT_LAZY_TYPE,
1920
REACT_MEMO_TYPE,
2021
REACT_PORTAL_TYPE,
2122
REACT_PROFILER_TYPE,
2223
REACT_PROVIDER_TYPE,
2324
REACT_STRICT_MODE_TYPE,
25+
REACT_SUSPENSE_TYPE,
2426
} from 'shared/ReactSymbols';
2527
import isValidElementType from 'shared/isValidElementType';
2628
import lowPriorityWarning from 'shared/lowPriorityWarning';
@@ -38,6 +40,7 @@ export function typeOf(object: any) {
3840
case REACT_FRAGMENT_TYPE:
3941
case REACT_PROFILER_TYPE:
4042
case REACT_STRICT_MODE_TYPE:
43+
case REACT_SUSPENSE_TYPE:
4144
return type;
4245
default:
4346
const $$typeofType = type && type.$$typeof;
@@ -51,6 +54,7 @@ export function typeOf(object: any) {
5154
return $$typeof;
5255
}
5356
}
57+
case REACT_LAZY_TYPE:
5458
case REACT_MEMO_TYPE:
5559
case REACT_PORTAL_TYPE:
5660
return $$typeof;
@@ -68,10 +72,12 @@ export const ContextProvider = REACT_PROVIDER_TYPE;
6872
export const Element = REACT_ELEMENT_TYPE;
6973
export const ForwardRef = REACT_FORWARD_REF_TYPE;
7074
export const Fragment = REACT_FRAGMENT_TYPE;
71-
export const Profiler = REACT_PROFILER_TYPE;
72-
export const Portal = REACT_PORTAL_TYPE;
75+
export const Lazy = REACT_LAZY_TYPE;
7376
export const Memo = REACT_MEMO_TYPE;
77+
export const Portal = REACT_PORTAL_TYPE;
78+
export const Profiler = REACT_PROFILER_TYPE;
7479
export const StrictMode = REACT_STRICT_MODE_TYPE;
80+
export const Suspense = REACT_SUSPENSE_TYPE;
7581

7682
export {isValidElementType};
7783

@@ -114,15 +120,21 @@ export function isForwardRef(object: any) {
114120
export function isFragment(object: any) {
115121
return typeOf(object) === REACT_FRAGMENT_TYPE;
116122
}
117-
export function isProfiler(object: any) {
118-
return typeOf(object) === REACT_PROFILER_TYPE;
123+
export function isLazy(object: any) {
124+
return typeOf(object) === REACT_LAZY_TYPE;
119125
}
120126
export function isMemo(object: any) {
121127
return typeOf(object) === REACT_MEMO_TYPE;
122128
}
123129
export function isPortal(object: any) {
124130
return typeOf(object) === REACT_PORTAL_TYPE;
125131
}
132+
export function isProfiler(object: any) {
133+
return typeOf(object) === REACT_PROFILER_TYPE;
134+
}
126135
export function isStrictMode(object: any) {
127136
return typeOf(object) === REACT_STRICT_MODE_TYPE;
128137
}
138+
export function isSuspense(object: any) {
139+
return typeOf(object) === REACT_SUSPENSE_TYPE;
140+
}

packages/react-is/src/__tests__/ReactIs-test.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,19 @@ describe('ReactIs', () => {
3939
}
4040

4141
const FunctionComponent = () => React.createElement('div');
42-
4342
const ForwardRefComponent = React.forwardRef((props, ref) =>
4443
React.createElement(Component, {forwardedRef: ref, ...props}),
4544
);
46-
45+
const LazyComponent = React.lazy(() => Component);
46+
const MemoComponent = React.memo(Component);
4747
const Context = React.createContext(false);
4848

4949
expect(ReactIs.isValidElementType('div')).toEqual(true);
5050
expect(ReactIs.isValidElementType(Component)).toEqual(true);
5151
expect(ReactIs.isValidElementType(FunctionComponent)).toEqual(true);
5252
expect(ReactIs.isValidElementType(ForwardRefComponent)).toEqual(true);
53+
expect(ReactIs.isValidElementType(LazyComponent)).toEqual(true);
54+
expect(ReactIs.isValidElementType(MemoComponent)).toEqual(true);
5355
expect(ReactIs.isValidElementType(Context.Provider)).toEqual(true);
5456
expect(ReactIs.isValidElementType(Context.Consumer)).toEqual(true);
5557
expect(ReactIs.isValidElementType(React.createFactory('div'))).toEqual(
@@ -60,6 +62,7 @@ describe('ReactIs', () => {
6062
true,
6163
);
6264
expect(ReactIs.isValidElementType(React.StrictMode)).toEqual(true);
65+
expect(ReactIs.isValidElementType(React.Suspense)).toEqual(true);
6366

6467
expect(ReactIs.isValidElementType(true)).toEqual(false);
6568
expect(ReactIs.isValidElementType(123)).toEqual(false);
@@ -116,6 +119,7 @@ describe('ReactIs', () => {
116119
expect(ReactIs.isElement(<React.Fragment />)).toBe(true);
117120
expect(ReactIs.isElement(<React.unstable_ConcurrentMode />)).toBe(true);
118121
expect(ReactIs.isElement(<React.StrictMode />)).toBe(true);
122+
expect(ReactIs.isElement(<React.Suspense />)).toBe(true);
119123
});
120124

121125
it('should identify ref forwarding component', () => {
@@ -152,6 +156,14 @@ describe('ReactIs', () => {
152156
expect(ReactIs.isMemo(Component)).toBe(false);
153157
});
154158

159+
it('should identify lazy', () => {
160+
const Component = () => React.createElement('div');
161+
const lazyComponent = React.lazy(() => Component);
162+
expect(ReactIs.typeOf(lazyComponent)).toBe(ReactIs.Lazy);
163+
expect(ReactIs.isLazy(lazyComponent)).toBe(true);
164+
expect(ReactIs.isLazy(Component)).toBe(false);
165+
});
166+
155167
it('should identify strict mode', () => {
156168
expect(ReactIs.typeOf(<React.StrictMode />)).toBe(ReactIs.StrictMode);
157169
expect(ReactIs.isStrictMode(<React.StrictMode />)).toBe(true);
@@ -160,6 +172,14 @@ describe('ReactIs', () => {
160172
expect(ReactIs.isStrictMode(<div />)).toBe(false);
161173
});
162174

175+
it('should identify suspense', () => {
176+
expect(ReactIs.typeOf(<React.Suspense />)).toBe(ReactIs.Suspense);
177+
expect(ReactIs.isSuspense(<React.Suspense />)).toBe(true);
178+
expect(ReactIs.isSuspense({type: ReactIs.Suspense})).toBe(false);
179+
expect(ReactIs.isSuspense('React.Suspense')).toBe(false);
180+
expect(ReactIs.isSuspense(<div />)).toBe(false);
181+
});
182+
163183
it('should identify profile root', () => {
164184
expect(
165185
ReactIs.typeOf(<React.unstable_Profiler id="foo" onRender={jest.fn()} />),

0 commit comments

Comments
 (0)