Skip to content

Commit

Permalink
[enzyme-adapter-react-helper] [New] add safeSFC
Browse files Browse the repository at this point in the history
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
ljharb committed Nov 24, 2017
1 parent 57fffb4 commit 9f3c45c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/enzyme-adapter-react-helper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"install-peerdeps": "^1.4.1",
"npm-run": "^4.1.2",
"object.assign": "^4.0.4",
"object.getownpropertydescriptors": "^2.0.3",
"semver": "^5.4.1"
}
}
30 changes: 30 additions & 0 deletions packages/enzyme-adapter-react-helper/src/safeSFC.jsx
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);

0 comments on commit 9f3c45c

Please sign in to comment.