|
| 1 | +/** |
| 2 | + * @fileoverview Tests for no-redundant-should-component-update |
| 3 | + */ |
| 4 | + |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +// ----------------------------------------------------------------------------- |
| 8 | +// Requirements |
| 9 | +// ----------------------------------------------------------------------------- |
| 10 | + |
| 11 | +var rule = require('../../../lib/rules/no-redundant-should-component-update'); |
| 12 | +var RuleTester = require('eslint').RuleTester; |
| 13 | + |
| 14 | +var parserOptions = { |
| 15 | + ecmaVersion: 6, |
| 16 | + ecmaFeatures: { |
| 17 | + experimentalObjectRestSpread: true, |
| 18 | + jsx: true |
| 19 | + } |
| 20 | +}; |
| 21 | + |
| 22 | +function errorMessage(node) { |
| 23 | + return `${node} does not need shouldComponentUpdate when extending React.PureComponent.`; |
| 24 | +} |
| 25 | + |
| 26 | +// ----------------------------------------------------------------------------- |
| 27 | +// Tests |
| 28 | +// ----------------------------------------------------------------------------- |
| 29 | + |
| 30 | +var ruleTester = new RuleTester(); |
| 31 | +ruleTester.run('no-redundant-should-component-update', rule, { |
| 32 | + valid: [ |
| 33 | + { |
| 34 | + code: [ |
| 35 | + 'class Foo extends React.Component {', |
| 36 | + ' shouldComponentUpdate() {', |
| 37 | + ' return true;', |
| 38 | + ' }', |
| 39 | + '}' |
| 40 | + ].join('\n'), |
| 41 | + parserOptions: parserOptions |
| 42 | + }, |
| 43 | + { |
| 44 | + code: [ |
| 45 | + 'class Foo extends React.Component {', |
| 46 | + ' shouldComponentUpdate = () => {', |
| 47 | + ' return true;', |
| 48 | + ' }', |
| 49 | + '}' |
| 50 | + ].join('\n'), |
| 51 | + parser: 'babel-eslint', |
| 52 | + parserOptions: parserOptions |
| 53 | + }, |
| 54 | + { |
| 55 | + code: [ |
| 56 | + 'class Foo extends React.Component {', |
| 57 | + ' shouldComponentUpdate() {', |
| 58 | + ' return true;', |
| 59 | + ' }', |
| 60 | + '}' |
| 61 | + ].join('\n'), |
| 62 | + parserOptions: parserOptions |
| 63 | + }, |
| 64 | + { |
| 65 | + code: [ |
| 66 | + 'function Foo() {', |
| 67 | + ' return class Bar extends React.Component {', |
| 68 | + ' shouldComponentUpdate() {', |
| 69 | + ' return true;', |
| 70 | + ' }', |
| 71 | + ' };', |
| 72 | + '}' |
| 73 | + ].join('\n'), |
| 74 | + parserOptions: parserOptions |
| 75 | + } |
| 76 | + ], |
| 77 | + invalid: [ |
| 78 | + { |
| 79 | + code: [ |
| 80 | + 'class Foo extends React.PureComponent {', |
| 81 | + ' shouldComponentUpdate() {', |
| 82 | + ' return true;', |
| 83 | + ' }', |
| 84 | + '}' |
| 85 | + ].join('\n'), |
| 86 | + errors: [{message: errorMessage('Foo')}], |
| 87 | + parserOptions: parserOptions |
| 88 | + }, |
| 89 | + { |
| 90 | + code: [ |
| 91 | + 'class Foo extends PureComponent {', |
| 92 | + ' shouldComponentUpdate() {', |
| 93 | + ' return true;', |
| 94 | + ' }', |
| 95 | + '}' |
| 96 | + ].join('\n'), |
| 97 | + errors: [{message: errorMessage('Foo')}], |
| 98 | + parserOptions: parserOptions |
| 99 | + }, |
| 100 | + { |
| 101 | + code: [ |
| 102 | + 'class Foo extends React.PureComponent {', |
| 103 | + ' shouldComponentUpdate = () => {', |
| 104 | + ' return true;', |
| 105 | + ' }', |
| 106 | + '}' |
| 107 | + ].join('\n'), |
| 108 | + errors: [{message: errorMessage('Foo')}], |
| 109 | + parser: 'babel-eslint', |
| 110 | + parserOptions: parserOptions |
| 111 | + }, |
| 112 | + { |
| 113 | + code: [ |
| 114 | + 'function Foo() {', |
| 115 | + ' return class Bar extends React.PureComponent {', |
| 116 | + ' shouldComponentUpdate() {', |
| 117 | + ' return true;', |
| 118 | + ' }', |
| 119 | + ' };', |
| 120 | + '}' |
| 121 | + ].join('\n'), |
| 122 | + errors: [{message: errorMessage('Bar')}], |
| 123 | + parserOptions: parserOptions |
| 124 | + }, |
| 125 | + { |
| 126 | + code: [ |
| 127 | + 'function Foo() {', |
| 128 | + ' return class Bar extends PureComponent {', |
| 129 | + ' shouldComponentUpdate() {', |
| 130 | + ' return true;', |
| 131 | + ' }', |
| 132 | + ' };', |
| 133 | + '}' |
| 134 | + ].join('\n'), |
| 135 | + errors: [{message: errorMessage('Bar')}], |
| 136 | + parserOptions: parserOptions |
| 137 | + }, |
| 138 | + { |
| 139 | + code: [ |
| 140 | + 'var Foo = class extends PureComponent {', |
| 141 | + ' shouldComponentUpdate() {', |
| 142 | + ' return true;', |
| 143 | + ' }', |
| 144 | + '}' |
| 145 | + ].join('\n'), |
| 146 | + errors: [{message: errorMessage('Foo')}], |
| 147 | + parserOptions: parserOptions |
| 148 | + } |
| 149 | + ] |
| 150 | +}); |
0 commit comments