Skip to content

Commit

Permalink
[enzyme-adapter-react-helper] safeSFC: React 0.14 doesn’t support S…
Browse files Browse the repository at this point in the history
…FCs returning `null`.
  • Loading branch information
ljharb committed Nov 27, 2017
1 parent c35b4a3 commit 0f54867
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/enzyme-adapter-react-helper/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "airbnb-base",
"extends": "airbnb",
"root": true,
"rules": {
"max-len": 0,
Expand Down
5 changes: 3 additions & 2 deletions packages/enzyme-adapter-react-helper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@
"babel-plugin-transform-replace-object-assign": "^0.2.1",
"babel-preset-airbnb": "^2.4.0",
"enzyme": "^3.1.0",
"eslint": "^4.11.0",
"eslint-config-airbnb-base": "^12.1.0",
"eslint": "^4.12.0",
"eslint-config-airbnb": "^16.1.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-jsx-a11y": "^6.0.2",
"eslint-plugin-react": "^7.5.1",
"react": ">=0.13",
"rimraf": "^2.6.1"
Expand Down
39 changes: 27 additions & 12 deletions packages/enzyme-adapter-react-helper/src/safeSFC.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,36 @@ function assertFunction(fn) {
return fn;
}

function safeSFC(fn) {
assertFunction(fn);
function copyStatics(source, target) {
assertFunction(source);

class SafeSFC extends React.Component {
render() {
return fn(this.props, this.context);
}
}
SafeSFC.displayName = fn.displayName || fn.name;
// eslint-disable-next-line no-param-reassign
target.displayName = source.displayName || source.name;
const {
prototype: oldProto,
...descriptors
} = gOPDs(fn);
Object.defineProperties(SafeSFC, descriptors);
return SafeSFC;
} = gOPDs(source);
Object.defineProperties(target, descriptors);

return target;
}

function nullToNoScript(fn) {
// eslint-disable-next-line prefer-arrow-callback
return copyStatics(fn, function NullHandler(...args) {
const element = fn(...args);
return element === null ? <noscript /> : element;
});
}

const maybeNullWrapper = ifReact('^0.14', nullToNoScript, assertFunction);

function safeSFC(fn) {
return copyStatics(fn, class SafeSFC extends React.Component {
render() {
return maybeNullWrapper(fn(this.props, this.context));
}
});
}

export default ifReact('>= 0.14', assertFunction, safeSFC);
export default ifReact('>= 0.14', maybeNullWrapper, safeSFC);

0 comments on commit 0f54867

Please sign in to comment.