Skip to content

Commit

Permalink
Warn if childContextType is defined on SFC (#6933)
Browse files Browse the repository at this point in the history
Add console.error message content check

Use appropriate name in warning/test
  • Loading branch information
aweary authored and jimfb committed Jun 8, 2016
1 parent 518336e commit c47830d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit c47830d

Please sign in to comment.