Skip to content

Commit e672e0f

Browse files
author
Joachim Seminck
committed
Add a bunch of failing tests.
The valid tests have two props, one is used in the render() function and the other in nextProps using the dot operator in the folllowing lifecycle methods: * shouldComponentUpdate * componentWillUpdate * componentDidUpdate (uses prevProps instead of nextProps). The invalid tests are the same as the valid tests but without the render() method. In here there should be an error that one of the props is unused, but there currently is no error.
1 parent fa51017 commit e672e0f

File tree

1 file changed

+186
-1
lines changed

1 file changed

+186
-1
lines changed

tests/lib/rules/no-unused-prop-types.js

Lines changed: 186 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,127 @@ ruleTester.run('no-unused-prop-types', rule, {
14841484
' c: React.PropTypes.string.isRequired,',
14851485
'};'
14861486
].join('\n')
1487+
}, {
1488+
code: [
1489+
'class Hello extends Component {',
1490+
' static propTypes = {',
1491+
' foo: PropTypes.string,',
1492+
' bar: PropTypes.string,',
1493+
' };',
1494+
'',
1495+
' shouldComponentUpdate (props) {',
1496+
' if (props.foo) {',
1497+
' return true;',
1498+
' }',
1499+
' }',
1500+
'',
1501+
' render() {',
1502+
' return (<div>{this.props.bar}</div>);',
1503+
' }',
1504+
'}'
1505+
].join('\n'),
1506+
parser: 'babel-eslint'
1507+
}, {
1508+
code: [
1509+
'class Hello extends Component {',
1510+
' static propTypes = {',
1511+
' foo: PropTypes.string,',
1512+
' bar: PropTypes.string,',
1513+
' };',
1514+
'',
1515+
' componentWillUpdate (props) {',
1516+
' if (props.foo) {',
1517+
' return true;',
1518+
' }',
1519+
' }',
1520+
'',
1521+
' render() {',
1522+
' return (<div>{this.props.bar}</div>);',
1523+
' }',
1524+
'}'
1525+
].join('\n'),
1526+
parser: 'babel-eslint'
1527+
}, {
1528+
code: [
1529+
'class Hello extends Component {',
1530+
' static propTypes = {',
1531+
' foo: PropTypes.string,',
1532+
' bar: PropTypes.string,',
1533+
' };',
1534+
'',
1535+
' componentWillReceiveProps (nextProps) {',
1536+
' const {foo} = nextProps;',
1537+
' if (foo) {',
1538+
' return true;',
1539+
' }',
1540+
' }',
1541+
'',
1542+
' render() {',
1543+
' return (<div>{this.props.bar}</div>);',
1544+
' }',
1545+
'}'
1546+
].join('\n'),
1547+
parser: 'babel-eslint'
1548+
}, {
1549+
code: [
1550+
'class Hello extends Component {',
1551+
' static propTypes = {',
1552+
' foo: PropTypes.string,',
1553+
' bar: PropTypes.string,',
1554+
' };',
1555+
'',
1556+
' shouldComponentUpdate (nextProps) {',
1557+
' if (nextProps.foo) {',
1558+
' return true;',
1559+
' }',
1560+
' }',
1561+
'',
1562+
' render() {',
1563+
' return (<div>{this.props.bar}</div>);',
1564+
' }',
1565+
'}'
1566+
].join('\n'),
1567+
parser: 'babel-eslint'
1568+
}, {
1569+
code: [
1570+
'class Hello extends Component {',
1571+
' static propTypes = {',
1572+
' foo: PropTypes.string,',
1573+
' bar: PropTypes.string,',
1574+
' };',
1575+
'',
1576+
' componentWillUpdate (nextProps) {',
1577+
' if (nextProps.foo) {',
1578+
' return true;',
1579+
' }',
1580+
' }',
1581+
'',
1582+
' render() {',
1583+
' return (<div>{this.props.bar}</div>);',
1584+
' }',
1585+
'}'
1586+
].join('\n'),
1587+
parser: 'babel-eslint'
1588+
}, {
1589+
code: [
1590+
'class Hello extends Component {',
1591+
' static propTypes = {',
1592+
' foo: PropTypes.string,',
1593+
' bar: PropTypes.string,',
1594+
' };',
1595+
'',
1596+
' componentDidUpdate (prevProps) {',
1597+
' if (prevProps.foo) {',
1598+
' return true;',
1599+
' }',
1600+
' }',
1601+
'',
1602+
' render() {',
1603+
' return (<div>{this.props.bar}</div>);',
1604+
' }',
1605+
'}'
1606+
].join('\n'),
1607+
parser: 'babel-eslint'
14871608
}
14881609
],
14891610

@@ -2430,7 +2551,71 @@ ruleTester.run('no-unused-prop-types', rule, {
24302551
line: 3,
24312552
column: 16
24322553
}]
2433-
}/* , {
2554+
}, {
2555+
code: [
2556+
'class Hello extends Component {',
2557+
' static propTypes = {',
2558+
' foo: PropTypes.string,',
2559+
' bar: PropTypes.string,',
2560+
' };',
2561+
'',
2562+
' componentWillUpdate (nextProps) {',
2563+
' if (nextProps.foo) {',
2564+
' return true;',
2565+
' }',
2566+
' }',
2567+
'}'
2568+
].join('\n'),
2569+
parser: 'babel-eslint',
2570+
errors: [{
2571+
message: '\'bar\' PropType is defined but prop is never used',
2572+
line: 4,
2573+
column: 10
2574+
}]
2575+
}, , {
2576+
code: [
2577+
'class Hello extends Component {',
2578+
' static propTypes = {',
2579+
' foo: PropTypes.string,',
2580+
' bar: PropTypes.string,',
2581+
' };',
2582+
'',
2583+
' shouldComponentUpdate (nextProps) {',
2584+
' if (nextProps.foo) {',
2585+
' return true;',
2586+
' }',
2587+
' }',
2588+
'}'
2589+
].join('\n'),
2590+
parser: 'babel-eslint',
2591+
errors: [{
2592+
message: '\'bar\' PropType is defined but prop is never used',
2593+
line: 4,
2594+
column: 10
2595+
}]
2596+
}, , {
2597+
code: [
2598+
'class Hello extends Component {',
2599+
' static propTypes = {',
2600+
' foo: PropTypes.string,',
2601+
' bar: PropTypes.string,',
2602+
' };',
2603+
'',
2604+
' componentDidUpdate (nextProps) {',
2605+
' if (nextProps.foo) {',
2606+
' return true;',
2607+
' }',
2608+
' }',
2609+
'}'
2610+
].join('\n'),
2611+
parser: 'babel-eslint',
2612+
errors: [{
2613+
message: '\'bar\' PropType is defined but prop is never used',
2614+
line: 4,
2615+
column: 10
2616+
}]
2617+
}
2618+
/* , {
24342619
// Enable this when the following issue is fixed
24352620
// https://github.com/yannickcr/eslint-plugin-react/issues/296
24362621
code: [

0 commit comments

Comments
 (0)