|
| 1 | +/** |
| 2 | + * @fileoverview Prevent usage of findDOMNode |
| 3 | + * @author Yannick Croissant |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +var rule = require('../../../lib/rules/no-find-dom-node'); |
| 12 | +var RuleTester = require('eslint').RuleTester; |
| 13 | + |
| 14 | +var parserOptions = { |
| 15 | + ecmaVersion: 6, |
| 16 | + ecmaFeatures: { |
| 17 | + jsx: true |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +// ------------------------------------------------------------------------------ |
| 22 | +// Tests |
| 23 | +// ------------------------------------------------------------------------------ |
| 24 | + |
| 25 | +var ruleTester = new RuleTester(); |
| 26 | +ruleTester.run('no-find-dom-node', rule, { |
| 27 | + |
| 28 | + valid: [{ |
| 29 | + code: [ |
| 30 | + 'var Hello = function() {};' |
| 31 | + ].join('\n'), |
| 32 | + parserOptions: parserOptions |
| 33 | + }, { |
| 34 | + code: [ |
| 35 | + 'var Hello = React.createClass({', |
| 36 | + ' render: function() {', |
| 37 | + ' return <div>Hello</div>;', |
| 38 | + ' }', |
| 39 | + '});' |
| 40 | + ].join('\n'), |
| 41 | + parserOptions: parserOptions |
| 42 | + }, { |
| 43 | + code: [ |
| 44 | + 'var Hello = React.createClass({', |
| 45 | + ' componentDidMount: function() {', |
| 46 | + ' someNonMemberFunction(arg);', |
| 47 | + ' this.someFunc = React.findDOMNode;', |
| 48 | + ' },', |
| 49 | + ' render: function() {', |
| 50 | + ' return <div>Hello</div>;', |
| 51 | + ' }', |
| 52 | + '});' |
| 53 | + ].join('\n'), |
| 54 | + parserOptions: parserOptions |
| 55 | + }], |
| 56 | + |
| 57 | + invalid: [{ |
| 58 | + code: [ |
| 59 | + 'var Hello = React.createClass({', |
| 60 | + ' componentDidMount: function() {', |
| 61 | + ' React.findDOMNode(this).scrollIntoView();', |
| 62 | + ' },', |
| 63 | + ' render: function() {', |
| 64 | + ' return <div>Hello</div>;', |
| 65 | + ' }', |
| 66 | + '});' |
| 67 | + ].join('\n'), |
| 68 | + parserOptions: parserOptions, |
| 69 | + errors: [{ |
| 70 | + message: 'Do not use findDOMNode' |
| 71 | + }] |
| 72 | + }, { |
| 73 | + code: [ |
| 74 | + 'var Hello = React.createClass({', |
| 75 | + ' componentDidMount: function() {', |
| 76 | + ' ReactDOM.findDOMNode(this).scrollIntoView();', |
| 77 | + ' },', |
| 78 | + ' render: function() {', |
| 79 | + ' return <div>Hello</div>;', |
| 80 | + ' }', |
| 81 | + '});' |
| 82 | + ].join('\n'), |
| 83 | + parserOptions: parserOptions, |
| 84 | + errors: [{ |
| 85 | + message: 'Do not use findDOMNode' |
| 86 | + }] |
| 87 | + }, { |
| 88 | + code: [ |
| 89 | + 'class Hello extends Component {', |
| 90 | + ' componentDidMount() {', |
| 91 | + ' findDOMNode(this).scrollIntoView();', |
| 92 | + ' }', |
| 93 | + ' render() {', |
| 94 | + ' return <div>Hello</div>;', |
| 95 | + ' }', |
| 96 | + '};' |
| 97 | + ].join('\n'), |
| 98 | + parserOptions: parserOptions, |
| 99 | + errors: [{ |
| 100 | + message: 'Do not use findDOMNode' |
| 101 | + }] |
| 102 | + }] |
| 103 | +}); |
0 commit comments