mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Merge pull request #3675 from spicyj/gh-3655
Add warning for getDefaultProps on ES6 classes
This commit is contained in:
@@ -170,6 +170,14 @@ var ReactCompositeComponentMixin = {
|
|||||||
'Did you mean to define a state property instead?',
|
'Did you mean to define a state property instead?',
|
||||||
this.getName() || 'a component'
|
this.getName() || 'a component'
|
||||||
);
|
);
|
||||||
|
warning(
|
||||||
|
!inst.getDefaultProps ||
|
||||||
|
inst.getDefaultProps.isReactClassApproved,
|
||||||
|
'getDefaultProps was defined on %s, a plain JavaScript class. ' +
|
||||||
|
'This is only supported for classes created using React.createClass. ' +
|
||||||
|
'Use a static property to define defaultProps instead.',
|
||||||
|
this.getName() || 'a component'
|
||||||
|
);
|
||||||
warning(
|
warning(
|
||||||
!inst.propTypes,
|
!inst.propTypes,
|
||||||
'propTypes was defined as an instance property on %s. Use a static ' +
|
'propTypes was defined as an instance property on %s. Use a static ' +
|
||||||
|
|||||||
@@ -266,6 +266,7 @@ describe 'ReactCoffeeScriptClass', ->
|
|||||||
but does not invoke them.', ->
|
but does not invoke them.', ->
|
||||||
spyOn console, 'error'
|
spyOn console, 'error'
|
||||||
getInitialStateWasCalled = false
|
getInitialStateWasCalled = false
|
||||||
|
getDefaultPropsWasCalled = false
|
||||||
class Foo extends React.Component
|
class Foo extends React.Component
|
||||||
constructor: ->
|
constructor: ->
|
||||||
@contextTypes = {}
|
@contextTypes = {}
|
||||||
@@ -275,20 +276,28 @@ describe 'ReactCoffeeScriptClass', ->
|
|||||||
getInitialStateWasCalled = true
|
getInitialStateWasCalled = true
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
getDefaultProps: ->
|
||||||
|
getDefaultPropsWasCalled = true
|
||||||
|
{}
|
||||||
|
|
||||||
render: ->
|
render: ->
|
||||||
span
|
span
|
||||||
className: 'foo'
|
className: 'foo'
|
||||||
|
|
||||||
test React.createElement(Foo), 'SPAN', 'foo'
|
test React.createElement(Foo), 'SPAN', 'foo'
|
||||||
expect(getInitialStateWasCalled).toBe false
|
expect(getInitialStateWasCalled).toBe false
|
||||||
expect(console.error.calls.length).toBe 3
|
expect(getDefaultPropsWasCalled).toBe false
|
||||||
|
expect(console.error.calls.length).toBe 4
|
||||||
expect(console.error.calls[0].args[0]).toContain(
|
expect(console.error.calls[0].args[0]).toContain(
|
||||||
'getInitialState was defined on Foo, a plain JavaScript class.'
|
'getInitialState was defined on Foo, a plain JavaScript class.'
|
||||||
)
|
)
|
||||||
expect(console.error.calls[1].args[0]).toContain(
|
expect(console.error.calls[1].args[0]).toContain(
|
||||||
'propTypes was defined as an instance property on Foo.'
|
'getDefaultProps was defined on Foo, a plain JavaScript class.'
|
||||||
)
|
)
|
||||||
expect(console.error.calls[2].args[0]).toContain(
|
expect(console.error.calls[2].args[0]).toContain(
|
||||||
|
'propTypes was defined as an instance property on Foo.'
|
||||||
|
)
|
||||||
|
expect(console.error.calls[3].args[0]).toContain(
|
||||||
'contextTypes was defined as an instance property on Foo.'
|
'contextTypes was defined as an instance property on Foo.'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -297,6 +297,7 @@ describe('ReactES6Class', function() {
|
|||||||
it('warns when classic properties are defined on the instance, ' +
|
it('warns when classic properties are defined on the instance, ' +
|
||||||
'but does not invoke them.', function() {
|
'but does not invoke them.', function() {
|
||||||
spyOn(console, 'error');
|
spyOn(console, 'error');
|
||||||
|
var getDefaultPropsWasCalled = false;
|
||||||
var getInitialStateWasCalled = false;
|
var getInitialStateWasCalled = false;
|
||||||
class Foo extends React.Component {
|
class Foo extends React.Component {
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -307,20 +308,28 @@ describe('ReactES6Class', function() {
|
|||||||
getInitialStateWasCalled = true;
|
getInitialStateWasCalled = true;
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
getDefaultProps() {
|
||||||
|
getDefaultPropsWasCalled = true;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
render() {
|
render() {
|
||||||
return <span className="foo" />;
|
return <span className="foo" />;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
test(<Foo />, 'SPAN', 'foo');
|
test(<Foo />, 'SPAN', 'foo');
|
||||||
expect(getInitialStateWasCalled).toBe(false);
|
expect(getInitialStateWasCalled).toBe(false);
|
||||||
expect(console.error.calls.length).toBe(3);
|
expect(getDefaultPropsWasCalled).toBe(false);
|
||||||
|
expect(console.error.calls.length).toBe(4);
|
||||||
expect(console.error.calls[0].args[0]).toContain(
|
expect(console.error.calls[0].args[0]).toContain(
|
||||||
'getInitialState was defined on Foo, a plain JavaScript class.'
|
'getInitialState was defined on Foo, a plain JavaScript class.'
|
||||||
);
|
);
|
||||||
expect(console.error.calls[1].args[0]).toContain(
|
expect(console.error.calls[1].args[0]).toContain(
|
||||||
'propTypes was defined as an instance property on Foo.'
|
'getDefaultProps was defined on Foo, a plain JavaScript class.'
|
||||||
);
|
);
|
||||||
expect(console.error.calls[2].args[0]).toContain(
|
expect(console.error.calls[2].args[0]).toContain(
|
||||||
|
'propTypes was defined as an instance property on Foo.'
|
||||||
|
);
|
||||||
|
expect(console.error.calls[3].args[0]).toContain(
|
||||||
'contextTypes was defined as an instance property on Foo.'
|
'contextTypes was defined as an instance property on Foo.'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -235,9 +235,14 @@ class NormalLifeCycles {
|
|||||||
// warns when classic properties are defined on the instance,
|
// warns when classic properties are defined on the instance,
|
||||||
// but does not invoke them.
|
// but does not invoke them.
|
||||||
var getInitialStateWasCalled = false;
|
var getInitialStateWasCalled = false;
|
||||||
|
var getDefaultPropsWasCalled = false;
|
||||||
class ClassicProperties extends React.Component {
|
class ClassicProperties extends React.Component {
|
||||||
contextTypes = {};
|
contextTypes = {};
|
||||||
propTypes = {};
|
propTypes = {};
|
||||||
|
getDefaultProps() {
|
||||||
|
getDefaultPropsWasCalled = true;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
getInitialState() {
|
getInitialState() {
|
||||||
getInitialStateWasCalled = true;
|
getInitialStateWasCalled = true;
|
||||||
return {};
|
return {};
|
||||||
@@ -410,17 +415,23 @@ describe('ReactTypeScriptClass', function() {
|
|||||||
var warn = jest.genMockFn();
|
var warn = jest.genMockFn();
|
||||||
console.error = warn;
|
console.error = warn;
|
||||||
getInitialStateWasCalled = false;
|
getInitialStateWasCalled = false;
|
||||||
|
getDefaultPropsWasCalled = false;
|
||||||
test(React.createElement(ClassicProperties), 'SPAN', 'foo');
|
test(React.createElement(ClassicProperties), 'SPAN', 'foo');
|
||||||
expect(getInitialStateWasCalled).toBe(false);
|
expect(getInitialStateWasCalled).toBe(false);
|
||||||
expect(warn.mock.calls.length).toBe(3);
|
expect(getDefaultPropsWasCalled).toBe(false);
|
||||||
|
expect(warn.mock.calls.length).toBe(4);
|
||||||
expect(warn.mock.calls[0][0]).toContain(
|
expect(warn.mock.calls[0][0]).toContain(
|
||||||
'getInitialState was defined on ClassicProperties, ' +
|
'getInitialState was defined on ClassicProperties, ' +
|
||||||
'a plain JavaScript class.'
|
'a plain JavaScript class.'
|
||||||
);
|
);
|
||||||
expect(warn.mock.calls[1][0]).toContain(
|
expect(warn.mock.calls[1][0]).toContain(
|
||||||
'propTypes was defined as an instance property on ClassicProperties.'
|
'getDefaultProps was defined on ClassicProperties, ' +
|
||||||
|
'a plain JavaScript class.'
|
||||||
);
|
);
|
||||||
expect(warn.mock.calls[2][0]).toContain(
|
expect(warn.mock.calls[2][0]).toContain(
|
||||||
|
'propTypes was defined as an instance property on ClassicProperties.'
|
||||||
|
);
|
||||||
|
expect(warn.mock.calls[3][0]).toContain(
|
||||||
'contextTypes was defined as an instance property on ClassicProperties.'
|
'contextTypes was defined as an instance property on ClassicProperties.'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user