-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(is/plainObject): extend isPlainObject check for React components (#…
…51)
- Loading branch information
Showing
9 changed files
with
132 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const { createElement, PureComponent, memo, lazy } = require('react'); | ||
|
||
let mockReactIs; | ||
|
||
jest.mock('react-is', () => mockReactIs); | ||
|
||
class Component extends PureComponent {} | ||
|
||
describe('utils/is/reactComponent', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
}); | ||
|
||
it('test', () => { | ||
mockReactIs = require.requireActual('react-is'); | ||
const isReactComponent = require('../reactComponent'); | ||
|
||
expect(isReactComponent({ test: 'i' })).toBe(false); | ||
expect(isReactComponent(createElement('i'))).toBe(false); | ||
expect(isReactComponent({ $$typeof: Symbol.for('react.element') })).toBe(false); | ||
expect(isReactComponent('test')).toBe(true); | ||
expect(isReactComponent(() => createElement('i'))).toBe(true); | ||
expect(isReactComponent(Component)).toBe(true); | ||
expect(isReactComponent(memo(Component))).toBe(true); | ||
expect(isReactComponent(lazy(Component))).toBe(true); | ||
}); | ||
|
||
it('test when react-is not defined', () => { | ||
mockReactIs = null; | ||
const isReactComponent = require('../reactComponent'); | ||
|
||
expect(isReactComponent({ test: 'i' })).toBe(false); | ||
expect(isReactComponent(createElement('i'))).toBe(true); | ||
expect(isReactComponent({ $$typeof: Symbol.for('react.element') })).toBe(true); | ||
expect(isReactComponent('test')).toBe(true); | ||
expect(isReactComponent(() => createElement('i'))).toBe(true); | ||
expect(isReactComponent(Component)).toBe(true); | ||
expect(isReactComponent(memo(Component))).toBe(true); | ||
expect(isReactComponent(lazy(Component))).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import has from '../object/has'; | ||
|
||
let isComponent: (test: any) => boolean; | ||
|
||
try { | ||
isComponent = require('react-is').isValidElementType; | ||
} catch (e) {} | ||
|
||
if (!isComponent) { | ||
isComponent = (test) => typeof test === 'string' || typeof test === 'function' || (!!test && has('$$typeof', test)); | ||
} | ||
|
||
/** | ||
* Returns whether a value is a valid React component | ||
* | ||
* **Note:** uses `react-is` library internally. If the host environment does not has `react-is` library, | ||
* any strings, function or object with $$typeof property are considered valid. | ||
* | ||
* **Note:** | ||
* | ||
* @param {*} test a reference being tested | ||
* @returns whether a value is a React component | ||
*/ | ||
export default isComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,24 @@ | ||
import has from '../object/has'; | ||
|
||
const HAS_SYMBOL_SUPPORT = typeof Symbol === 'function' && typeof Symbol.for === 'function'; | ||
const REACT_ELEMENT_TYPE = HAS_SYMBOL_SUPPORT && Symbol.for('react.element'); | ||
let isElement: (test: any) => boolean; | ||
|
||
try { | ||
isElement = require('react-is').isElement; | ||
} catch (e) {} | ||
|
||
if (!isElement) { | ||
isElement = (test) => !!test && has('$$typeof', test); | ||
} | ||
|
||
/** | ||
* Returns whether a value is a valid React element | ||
* | ||
* **Note:** this won't work with any API-compatible libraries like Inferno | ||
* or Preact which check virtual DOM node integrity through other means | ||
* (binary flags and instanceof checks respectively) | ||
* **Note:** uses `react-is` library internally. If the host environment does not has `react-is` library, | ||
* any object with $$typeof property is considered valid. | ||
* | ||
* **Note:** if the host environment does not support Symbol, any $$typeof | ||
* property value is considered valid. | ||
* **Note:** | ||
* | ||
* @param {*} test a reference being tested | ||
* @returns whether a value is a React element | ||
*/ | ||
export default function isReactElement(test): boolean { | ||
return !!test && has('$$typeof', test) && (!HAS_SYMBOL_SUPPORT || test.$$typeof === REACT_ELEMENT_TYPE); | ||
} | ||
export default isElement; |