Skip to content

Commit

Permalink
Support ForwardRef type of work in TestRenderer (#12392)
Browse files Browse the repository at this point in the history
* Support ForwardRef type of work in TestRenderer and ShallowRenderer.
* Release script now updates inter-package dependencies too (e.g. react-test-renderer depends on react-is).
  • Loading branch information
jquense authored and bvaughn committed Mar 16, 2018
1 parent 7525dc5 commit a6e6618
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 15 deletions.
17 changes: 4 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
"description": "React package for snapshot testing.",
"main": "index.js",
"repository": "facebook/react",
"keywords": [
"react",
"react-native",
"react-testing"
],
"keywords": ["react", "react-native", "react-testing"],
"license": "MIT",
"bugs": {
"url": "https://github.com/facebook/react/issues"
Expand All @@ -17,16 +13,11 @@
"dependencies": {
"fbjs": "^0.8.16",
"object-assign": "^4.1.1",
"prop-types": "^15.6.0"
"prop-types": "^15.6.0",
"react-is": "^16.3.0-alpha.2"
},
"peerDependencies": {
"react": "^16.0.0 || 16.3.0-alpha.2"
},
"files": [
"LICENSE",
"README.md",
"index.js",
"shallow.js",
"cjs/"
]
"files": ["LICENSE", "README.md", "index.js", "shallow.js", "cjs/"]
}
7 changes: 5 additions & 2 deletions src/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import React from 'react';
import {isForwardRef} from 'react-is';
import {warnAboutDeprecatedLifecycles} from 'shared/ReactFeatureFlags';
import describeComponentFrame from 'shared/describeComponentFrame';
import getComponentName from 'shared/getComponentName';
Expand Down Expand Up @@ -77,7 +78,7 @@ class ReactShallowRenderer {
element.type,
);
invariant(
typeof element.type === 'function',
isForwardRef(element) || typeof element.type === 'function',
'ReactShallowRenderer render(): Shallow rendering works only with custom ' +
'components, but the provided element type was `%s`.',
Array.isArray(element.type)
Expand All @@ -96,7 +97,9 @@ class ReactShallowRenderer {
if (this._instance) {
this._updateClassComponent(element, this._context);
} else {
if (shouldConstruct(element.type)) {
if (isForwardRef(element)) {
this._rendered = element.type.render(element.props, element.ref);
} else if (shouldConstruct(element.type)) {
this._instance = new element.type(
element.props,
this._context,
Expand Down
3 changes: 3 additions & 0 deletions src/ReactTestRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
ContextConsumer,
ContextProvider,
Mode,
ForwardRef,
} from 'shared/ReactTypeOfWork';
import invariant from 'fbjs/lib/invariant';

Expand Down Expand Up @@ -372,6 +373,7 @@ function toTree(node: ?Fiber) {
case ContextProvider:
case ContextConsumer:
case Mode:
case ForwardRef:
return childrenToTree(node.child);
default:
invariant(
Expand Down Expand Up @@ -472,6 +474,7 @@ class ReactTestInstance {
case ContextProvider:
case ContextConsumer:
case Mode:
case ForwardRef:
descend = true;
break;
default:
Expand Down
22 changes: 22 additions & 0 deletions src/__tests__/ReactShallowRenderer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,28 @@ describe('ReactShallowRenderer', () => {
]);
});

it('should handle ForwardRef', () => {
const testRef = React.createRef();
const SomeComponent = React.forwardRef((props, ref) => {
expect(ref).toEqual(testRef);
return (
<div>
<span className="child1" />
<span className="child2" />
</div>
);
});

const shallowRenderer = createRenderer();
const result = shallowRenderer.render(<SomeComponent ref={testRef} />);

expect(result.type).toBe('div');
expect(result.props.children).toEqual([
<span className="child1" />,
<span className="child2" />,
]);
});

it('should enable shouldComponentUpdate to prevent a re-render', () => {
let renderCounter = 0;
class SimpleComponent extends React.Component {
Expand Down
43 changes: 43 additions & 0 deletions src/__tests__/ReactTestRenderer-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ describe('ReactTestRenderer', () => {
log.push('Angry render');
throw new Error('Please, do not render me.');
}

componentDidMount() {
log.push('Angry componentDidMount');
}
Expand Down Expand Up @@ -957,4 +958,46 @@ describe('ReactTestRenderer', () => {
}),
);
});

it('supports forwardRef', () => {
const InnerRefed = React.forwardRef((props, ref) => (
<div>
<span ref={ref} />
</div>
));

class App extends React.Component {
render() {
return <InnerRefed ref={r => (this.ref = r)} />;
}
}

const renderer = ReactTestRenderer.create(<App />);
const tree = renderer.toTree();
cleanNodeOrArray(tree);

expect(prettyFormat(tree)).toEqual(
prettyFormat({
instance: null,
nodeType: 'component',
props: {},
rendered: {
instance: null,
nodeType: 'host',
props: {},
rendered: [
{
instance: null,
nodeType: 'host',
props: {},
rendered: [],
type: 'span',
},
],
type: 'div',
},
type: App,
}),
);
});
});

0 comments on commit a6e6618

Please sign in to comment.