Skip to content
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

Elements from functional components in TestUtils should have no owner #6362

Merged
merged 1 commit into from
Apr 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 40 additions & 36 deletions src/renderers/shared/reconciler/ReactCompositeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ function warnIfInvalidElement(Component, element) {
}
}

function shouldConstruct(Component) {
return Component.prototype && Component.prototype.isReactComponent;
}

/**
* ------------------ The Life-Cycle of a Composite Component ------------------
*
Expand Down Expand Up @@ -159,44 +163,22 @@ var ReactCompositeComponentMixin = {
var Component = this._currentElement.type;

// Initialize the public class
var inst;
var inst = this._constructComponent(publicProps, publicContext);
var renderedElement;

if (Component.prototype && Component.prototype.isReactComponent) {
if (__DEV__) {
ReactCurrentOwner.current = this;
try {
inst = new Component(publicProps, publicContext, ReactUpdateQueue);
} finally {
ReactCurrentOwner.current = null;
}
} else {
inst = new Component(publicProps, publicContext, ReactUpdateQueue);
}
} else {
if (__DEV__) {
ReactCurrentOwner.current = this;
try {
inst = Component(publicProps, publicContext, ReactUpdateQueue);
} finally {
ReactCurrentOwner.current = null;
}
} else {
inst = Component(publicProps, publicContext, ReactUpdateQueue);
}
if (inst == null || inst.render == null) {
renderedElement = inst;
warnIfInvalidElement(Component, renderedElement);
invariant(
inst === null ||
inst === false ||
ReactElement.isValidElement(inst),
'%s(...): A valid React element (or null) must be returned. You may have ' +
'returned undefined, an array or some other invalid object.',
Component.displayName || Component.name || 'Component'
);
inst = new StatelessComponent(Component);
}
// Support functional components
if (!shouldConstruct(Component) && (inst == null || inst.render == null)) {
renderedElement = inst;
warnIfInvalidElement(Component, renderedElement);
invariant(
inst === null ||
inst === false ||
ReactElement.isValidElement(inst),
'%s(...): A valid React element (or null) must be returned. You may have ' +
'returned undefined, an array or some other invalid object.',
Component.displayName || Component.name || 'Component'
);
inst = new StatelessComponent(Component);
}

if (__DEV__) {
Expand Down Expand Up @@ -324,6 +306,28 @@ var ReactCompositeComponentMixin = {
return markup;
},

_constructComponent: function(publicProps, publicContext) {
if (__DEV__) {
ReactCurrentOwner.current = this;
try {
return this._constructComponentWithoutOwner(publicProps, publicContext);
} finally {
ReactCurrentOwner.current = null;
}
} else {
return this._constructComponentWithoutOwner(publicProps, publicContext);
}
},

_constructComponentWithoutOwner: function(publicProps, publicContext) {
var Component = this._currentElement.type;
if (shouldConstruct(Component)) {
return new Component(publicProps, publicContext, ReactUpdateQueue);
} else {
return Component(publicProps, publicContext, ReactUpdateQueue);
}
},

performInitialMountWithErrorHandling: function(
renderedElement,
nativeParent,
Expand Down
2 changes: 2 additions & 0 deletions src/test/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@ var ShallowComponentWrapper = function(element) {
assign(
ShallowComponentWrapper.prototype,
ReactCompositeComponent.Mixin, {
_constructComponent:
ReactCompositeComponent.Mixin._constructComponentWithoutOwner,
_instantiateReactComponent: function(element) {
return new NoopInternalComponent(element);
},
Expand Down
20 changes: 20 additions & 0 deletions src/test/__tests__/ReactTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ describe('ReactTestUtils', function() {
]);
});

it('should shallow render a functional component', function() {
function SomeComponent() {
return (
<div>
<span className="child1" />
<span className="child2" />
</div>
);
}

var shallowRenderer = ReactTestUtils.createRenderer();
var result = shallowRenderer.render(<SomeComponent />);

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

it('should throw for invalid elements', function() {
var SomeComponent = React.createClass({
render: function() {
Expand Down