-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[enzyme-adapter-react-helper] [New] add
safeSFC
Where you’d otherwise have an SFC in tests, use safeSFC to make your tests work in React 0.13 also: ```js const Foo = safeSFC(function Foo(props, context) { return null; }); const wrapper = shallow(<Foo />); ``` Static properties are copied over.
- Loading branch information
Showing
2 changed files
with
31 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react'; | ||
import gOPDs from 'object.getownpropertydescriptors'; | ||
|
||
import ifReact from './ifReact'; | ||
|
||
function assertFunction(fn) { | ||
if (typeof fn !== 'function') { | ||
throw new TypeError('Component must be a function'); | ||
} | ||
return fn; | ||
} | ||
|
||
function safeSFC(fn) { | ||
assertFunction(fn); | ||
|
||
class SafeSFC extends React.Component { | ||
render() { | ||
return fn(this.props, this.context); | ||
} | ||
} | ||
SafeSFC.displayName = fn.displayName || fn.name; | ||
const { | ||
prototype: oldProto, | ||
...descriptors | ||
} = gOPDs(fn); | ||
Object.defineProperties(SafeSFC, descriptors); | ||
return SafeSFC; | ||
} | ||
|
||
export default ifReact('>= 0.14', assertFunction, safeSFC); |