-
Notifications
You must be signed in to change notification settings - Fork 49.8k
Support ForwardRef type of work in TestRenderer #12392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
5ddf6f4
b9d091c
8a25f8c
a8d6721
610df95
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
|
@@ -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" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this require updates to our release/build script to keep these deps in sync? Needs follow up.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm i don't know, I was conflating workspaces and lerna, the latter of which handles the updates
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These versions are bumped by our release script manually. It's fine. I'll add it to my list of follow up things.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed via 610df95 |
||
| }, | ||
| "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/"] | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'; | ||
|
|
@@ -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) | ||
|
|
@@ -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, null); | ||
|
||
| } else if (shouldConstruct(element.type)) { | ||
| this._instance = new element.type( | ||
| element.props, | ||
| this._context, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -168,6 +168,26 @@ describe('ReactShallowRenderer', () => { | |
| ]); | ||
| }); | ||
|
|
||
| it('should handle ForwardRef', () => { | ||
| const SomeComponent = React.forwardRef((props, ref) => ( | ||
| <div> | ||
| {(expect(ref).toEqual(null), null)} | ||
| <span className="child1" /> | ||
| <span className="child2" /> | ||
| </div> | ||
| )); | ||
|
|
||
| const shallowRenderer = createRenderer(); | ||
| const result = shallowRenderer.render(<SomeComponent />); | ||
|
|
||
| expect(result.type).toBe('div'); | ||
| expect(result.props.children).toEqual([ | ||
| null, | ||
| <span className="child1" />, | ||
| <span className="child2" />, | ||
| ]); | ||
| }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: 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 { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's possible to have a null React element type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably not in practice but the ShallowRenderer tests do
<NullIdentifer>which was how i was hitting this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see. 👍