Warn if childContextType is defined on SFC (#6933)

Add console.error message content check

Use appropriate name in warning/test
(cherry picked from commit c47830d12c)
This commit is contained in:
Brandon Dail
2016-06-08 22:32:43 +02:00
committed by Paul O’Shannessy
parent e1c2c42c24
commit 89746fa70f
2 changed files with 26 additions and 0 deletions
@@ -46,6 +46,11 @@ function warnIfInvalidElement(Component, element) {
'returned undefined, an array or some other invalid object.',
Component.displayName || Component.name || 'Component'
);
warning(
!Component.childContextTypes,
'%s(...): childContextTypes cannot be defined on a functional component.',
Component.displayName || Component.name || 'Component'
);
}
}
@@ -98,6 +98,27 @@ describe('ReactStatelessComponent', function() {
expect(el.textContent).toBe('mest');
});
it('should warn for childContextTypes on a functional component', () => {
spyOn(console, 'error');
function StatelessComponentWithChildContext(props) {
return <div>{props.name}</div>;
}
StatelessComponentWithChildContext.childContextTypes = {
foo: React.PropTypes.string,
};
var container = document.createElement('div');
ReactDOM.render(<StatelessComponentWithChildContext name="A" />, container);
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toContain(
'StatelessComponentWithChildContext(...): childContextTypes cannot ' +
'be defined on a functional component.'
);
});
it('should warn when stateless component returns array', function() {
spyOn(console, 'error');
function NotAComponent() {