|
| 1 | +require('babel-eslint'); |
| 2 | + |
| 3 | +const rule = require('../../../lib/rules/mapStateToProps-prefer-selectors'); |
| 4 | +const RuleTester = require('eslint').RuleTester; |
| 5 | + |
| 6 | +const parserOptions = { |
| 7 | + ecmaVersion: 6, |
| 8 | + sourceType: 'module', |
| 9 | + ecmaFeatures: { |
| 10 | + experimentalObjectRestSpread: true, |
| 11 | + }, |
| 12 | +}; |
| 13 | + |
| 14 | +const ruleTester = new RuleTester({ parserOptions }); |
| 15 | + |
| 16 | +ruleTester.run('mapStateToProps-prefer-selectors', rule, { |
| 17 | + valid: [ |
| 18 | + 'const mapStateToProps = (state) => 1', |
| 19 | + 'const mapStateToProps = (state) => ({})', |
| 20 | + 'const mapStateToProps = (state) => ({ x: xSelector(state) })', |
| 21 | + 'const mapStateToProps = (state, ownProps) => ({ x: xSelector(state, ownProps) })', |
| 22 | + 'const mapStateToProps = (state) => ({ x: xSelector(state), y: ySelector(state) })', |
| 23 | + 'const mapStateToProps = (state) => { return { x: xSelector(state) }; }', |
| 24 | + 'const mapStateToProps = (state) => { doSomethingElse(); return { x: xSelector(state) }; }', |
| 25 | + 'const mapStateToProps = function(state) { return { x: xSelector(state) }; }', |
| 26 | + 'function mapStateToProps(state) { doSomethingElse(); return { x: xSelector(state) }; }', |
| 27 | + 'connect((state) => ({ x: xSelector(state) }), {})(Comp)', |
| 28 | + 'const mapStateToProps = () => ({ x: xSelector() })', |
| 29 | + 'const mapStateToProps = function(state) { return { x: getX() }; }', |
| 30 | + 'const mapStateToProps = function(state) { return { x: getX(state) }; }', |
| 31 | + 'connect((state, ownProps) => ({ x: selector() }), {})(Comp)', |
| 32 | + 'connect((state, ownProps) => ({ x: selector(state) }), {})(Comp)', |
| 33 | + 'connect((state, ownProps) => ({ x: selector(state, ownProps) }), {})(Comp)', |
| 34 | + { |
| 35 | + code: 'const mapStateToProps = (state) => ({ x: xSelector(state) })', |
| 36 | + options: [{ |
| 37 | + matching: '^.*Selector$', |
| 38 | + }], |
| 39 | + }, |
| 40 | + { |
| 41 | + code: 'const mapStateToProps = function(state) { return { x: getX(state) }; }', |
| 42 | + options: [{ |
| 43 | + matching: '^get.*$', |
| 44 | + }], |
| 45 | + }, |
| 46 | + { |
| 47 | + code: 'connect((state) => ({ x: selector(state) }), {})(Comp)', |
| 48 | + options: [{ |
| 49 | + matching: '^selector$', |
| 50 | + }], |
| 51 | + }, |
| 52 | + { |
| 53 | + code: 'const mapStateToProps = (state) => ({ x: xSelector(differentParam) })', |
| 54 | + options: [{ |
| 55 | + validateParams: false, |
| 56 | + }], |
| 57 | + }, |
| 58 | + { |
| 59 | + code: 'const mapStateToProps = function(state) { return { x: getX(state, ownProps2) }; }', |
| 60 | + options: [{ |
| 61 | + validateParams: false, |
| 62 | + }], |
| 63 | + }, |
| 64 | + { |
| 65 | + code: 'connect(() => ({ x: selector(state) }), {})(Comp)', |
| 66 | + options: [{ |
| 67 | + validateParams: false, |
| 68 | + }], |
| 69 | + }, |
| 70 | + ], |
| 71 | + invalid: [{ |
| 72 | + code: 'const mapStateToProps = (state) => ({ x: state.b })', |
| 73 | + errors: [ |
| 74 | + { |
| 75 | + message: 'mapStateToProps property "x" should use a selector function.', |
| 76 | + }, |
| 77 | + ], |
| 78 | + }, { |
| 79 | + code: 'const mapStateToProps = (state) => ({ x: state.x, y: state.y })', |
| 80 | + errors: [ |
| 81 | + { |
| 82 | + message: 'mapStateToProps property "x" should use a selector function.', |
| 83 | + }, |
| 84 | + { |
| 85 | + message: 'mapStateToProps property "y" should use a selector function.', |
| 86 | + }, |
| 87 | + ], |
| 88 | + }, { |
| 89 | + code: 'const mapStateToProps = (state) => ({ x: state.x, y: ySelector(state) })', |
| 90 | + errors: [ |
| 91 | + { |
| 92 | + message: 'mapStateToProps property "x" should use a selector function.', |
| 93 | + }, |
| 94 | + ], |
| 95 | + }, { |
| 96 | + code: 'const mapStateToProps = (state) => { return { x: state.b }; }', |
| 97 | + errors: [ |
| 98 | + { |
| 99 | + message: 'mapStateToProps property "x" should use a selector function.', |
| 100 | + }, |
| 101 | + ], |
| 102 | + }, { |
| 103 | + code: 'const mapStateToProps = (state) => { doSomethingElse(); return { x: state.b }; }', |
| 104 | + errors: [ |
| 105 | + { |
| 106 | + message: 'mapStateToProps property "x" should use a selector function.', |
| 107 | + }, |
| 108 | + ], |
| 109 | + }, { |
| 110 | + code: 'const mapStateToProps = function(state) { return { x: state.x }; }', |
| 111 | + errors: [ |
| 112 | + { |
| 113 | + message: 'mapStateToProps property "x" should use a selector function.', |
| 114 | + }, |
| 115 | + ], |
| 116 | + }, { |
| 117 | + code: 'function mapStateToProps(state) { doSomethingElse(); return { x: state.b }; }', |
| 118 | + errors: [ |
| 119 | + { |
| 120 | + message: 'mapStateToProps property "x" should use a selector function.', |
| 121 | + }, |
| 122 | + ], |
| 123 | + }, { |
| 124 | + code: 'connect((state) => ({ x: state.x }), {})(Comp)', |
| 125 | + errors: [ |
| 126 | + { |
| 127 | + message: 'mapStateToProps property "x" should use a selector function.', |
| 128 | + }, |
| 129 | + ], |
| 130 | + }, { |
| 131 | + code: 'const mapStateToProps = (state) => ({ x: xSelector(state) })', |
| 132 | + options: [{ |
| 133 | + matching: '^get.*$', |
| 134 | + }], |
| 135 | + errors: [{ |
| 136 | + message: 'mapStateToProps "x"\'s selector "xSelector" does not match "^get.*$".', |
| 137 | + }], |
| 138 | + }, { |
| 139 | + code: 'const mapStateToProps = function(state) { return { x: getX(state) }; }', |
| 140 | + options: [{ |
| 141 | + matching: '^.*Selector$', |
| 142 | + }], |
| 143 | + errors: [{ |
| 144 | + message: 'mapStateToProps "x"\'s selector "getX" does not match "^.*Selector$".', |
| 145 | + }], |
| 146 | + }, { |
| 147 | + code: 'connect((state) => ({ x: selectorr(state) }), {})(Comp)', |
| 148 | + options: [{ |
| 149 | + matching: '^selector$', |
| 150 | + }], |
| 151 | + errors: [{ |
| 152 | + message: 'mapStateToProps "x"\'s selector "selectorr" does not match "^selector$".', |
| 153 | + }], |
| 154 | + }, { |
| 155 | + code: 'const mapStateToProps = (state) => ({ x: xSelector(state, ownProps) })', |
| 156 | + errors: [{ |
| 157 | + message: 'mapStateToProps "x"\'s selector "xSelector" parameter #1 is not expected.', |
| 158 | + }], |
| 159 | + }, { |
| 160 | + code: 'const mapStateToProps = (state, ownProps) => ({ x: xSelector(state, ownProps, someOtherValue) })', |
| 161 | + errors: [{ |
| 162 | + message: 'mapStateToProps "x"\'s selector "xSelector" parameter #2 is not expected.', |
| 163 | + }], |
| 164 | + }, { |
| 165 | + code: 'const mapStateToProps = function(state) { return { x: getX(notState) }; }', |
| 166 | + errors: [{ |
| 167 | + message: 'mapStateToProps "x"\'s selector "getX" parameter #0 should be "state".', |
| 168 | + }], |
| 169 | + }, { |
| 170 | + code: 'connect((state, ownProps) => ({ x: getX(state, notOwnProps) }), {})(Comp)', |
| 171 | + errors: [{ |
| 172 | + message: 'mapStateToProps "x"\'s selector "getX" parameter #1 should be "ownProps".', |
| 173 | + }], |
| 174 | + }, { |
| 175 | + code: 'connect((state2, ownProps) => ({ x: getX(state) }), {})(Comp)', |
| 176 | + errors: [{ |
| 177 | + message: 'mapStateToProps "x"\'s selector "getX" parameter #0 should be "state2".', |
| 178 | + }], |
| 179 | + }, { |
| 180 | + code: 'connect((state, ownProps2) => ({ x: getX(state, ownProps) }), {})(Comp)', |
| 181 | + errors: [{ |
| 182 | + message: 'mapStateToProps "x"\'s selector "getX" parameter #1 should be "ownProps2".', |
| 183 | + }], |
| 184 | + }], |
| 185 | + |
| 186 | +}); |
0 commit comments