|
| 1 | +/** |
| 2 | + * @fileoverview Tests for no-comment-textnodes |
| 3 | + * @author Ben Vinegar |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +var rule = require('../../../lib/rules/no-comment-textnodes'); |
| 12 | +var RuleTester = require('eslint').RuleTester; |
| 13 | + |
| 14 | +// ------------------------------------------------------------------------------ |
| 15 | +// Tests |
| 16 | +// ------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +var ruleTester = new RuleTester(); |
| 19 | +ruleTester.run('no-comment-textnodes', rule, { |
| 20 | + |
| 21 | + valid: [ |
| 22 | + { |
| 23 | + code: [ |
| 24 | + 'class Comp1 extends Component {', |
| 25 | + ' render() {', |
| 26 | + ' return (', |
| 27 | + ' <div>', |
| 28 | + ' {/* valid */}', |
| 29 | + ' </div>', |
| 30 | + ' );', |
| 31 | + ' }', |
| 32 | + '}' |
| 33 | + ].join('\n'), |
| 34 | + args: [1], |
| 35 | + parser: 'babel-eslint' |
| 36 | + }, { |
| 37 | + code: [ |
| 38 | + 'class Comp1 extends Component {', |
| 39 | + ' render() {', |
| 40 | + ' return (<div>{/* valid */}</div>);', |
| 41 | + ' }', |
| 42 | + '}' |
| 43 | + ].join('\n'), |
| 44 | + args: [1], |
| 45 | + parser: 'babel-eslint' |
| 46 | + }, { |
| 47 | + code: [ |
| 48 | + 'class Comp1 extends Component {', |
| 49 | + ' render() {', |
| 50 | + ' const bar = (<div>{/* valid */}</div>);', |
| 51 | + ' return bar;', |
| 52 | + ' }', |
| 53 | + '}' |
| 54 | + ].join('\n'), |
| 55 | + args: [1], |
| 56 | + parser: 'babel-eslint' |
| 57 | + }, { |
| 58 | + code: [ |
| 59 | + 'var Hello = React.createClass({', |
| 60 | + ' foo: (<div>{/* valid */}</div>),', |
| 61 | + ' render() {', |
| 62 | + ' return this.foo;', |
| 63 | + ' },', |
| 64 | + '});' |
| 65 | + ].join('\n'), |
| 66 | + args: [1], |
| 67 | + parser: 'babel-eslint' |
| 68 | + }, { |
| 69 | + code: [ |
| 70 | + 'class Comp1 extends Component {', |
| 71 | + ' render() {', |
| 72 | + ' return (', |
| 73 | + ' <div>', |
| 74 | + ' {/* valid */}', |
| 75 | + ' {/* valid 2 */}', |
| 76 | + ' {/* valid 3 */}', |
| 77 | + ' </div>', |
| 78 | + ' );', |
| 79 | + ' }', |
| 80 | + '}' |
| 81 | + ].join('\n'), |
| 82 | + args: [1], |
| 83 | + parser: 'babel-eslint' |
| 84 | + }, { |
| 85 | + code: [ |
| 86 | + 'class Comp1 extends Component {', |
| 87 | + ' render() {', |
| 88 | + ' return (', |
| 89 | + ' <div>', |
| 90 | + ' </div>', |
| 91 | + ' );', |
| 92 | + ' }', |
| 93 | + '}' |
| 94 | + ].join('\n'), |
| 95 | + args: [1], |
| 96 | + parser: 'babel-eslint' |
| 97 | + }, { |
| 98 | + code: [ |
| 99 | + 'var foo = require(\'foo\');' |
| 100 | + ].join('\n'), |
| 101 | + args: [1], |
| 102 | + parser: 'babel-eslint' |
| 103 | + }, { |
| 104 | + code: [ |
| 105 | + '<Foo bar=\'test\'>', |
| 106 | + ' {/* valid */}', |
| 107 | + '</Foo>' |
| 108 | + ].join('\n'), |
| 109 | + args: [1], |
| 110 | + parser: 'babel-eslint' |
| 111 | + }, |
| 112 | + { |
| 113 | + code: [ |
| 114 | + '<strong>', |
| 115 | + ' https://www.example.com/attachment/download/1', |
| 116 | + '</strong>' |
| 117 | + ].join('\n'), |
| 118 | + args: [1], |
| 119 | + parser: 'babel-eslint' |
| 120 | + }, |
| 121 | + |
| 122 | + // inside element declarations |
| 123 | + { |
| 124 | + code: [ |
| 125 | + '<Foo /* valid */ placeholder={\'foo\'}/>' |
| 126 | + ].join('\n'), |
| 127 | + args: [1], |
| 128 | + parser: 'babel-eslint' |
| 129 | + }, |
| 130 | + { |
| 131 | + code: [ |
| 132 | + '<Foo title={\'foo\' /* valid */}/>' |
| 133 | + ].join('\n'), |
| 134 | + args: [1], |
| 135 | + parser: 'babel-eslint' |
| 136 | + } |
| 137 | + ], |
| 138 | + |
| 139 | + invalid: [ |
| 140 | + { |
| 141 | + code: [ |
| 142 | + 'class Comp1 extends Component {', |
| 143 | + ' render() {', |
| 144 | + ' return (<div>// invalid</div>);', |
| 145 | + ' }', |
| 146 | + '}' |
| 147 | + ].join('\n'), |
| 148 | + args: [1], |
| 149 | + parser: 'babel-eslint', |
| 150 | + errors: [{message: 'Comments inside children section of tag should be placed inside braces'}] |
| 151 | + }, { |
| 152 | + code: [ |
| 153 | + 'class Comp1 extends Component {', |
| 154 | + ' render() {', |
| 155 | + ' return (<div>/* invalid */</div>);', |
| 156 | + ' }', |
| 157 | + '}' |
| 158 | + ].join('\n'), |
| 159 | + args: [1], |
| 160 | + parser: 'babel-eslint', |
| 161 | + errors: [{message: 'Comments inside children section of tag should be placed inside braces'}] |
| 162 | + }, { |
| 163 | + code: [ |
| 164 | + 'class Comp1 extends Component {', |
| 165 | + ' render() {', |
| 166 | + ' return (', |
| 167 | + ' <div>', |
| 168 | + ' // invalid', |
| 169 | + ' </div>', |
| 170 | + ' );', |
| 171 | + ' }', |
| 172 | + '}' |
| 173 | + ].join('\n'), |
| 174 | + args: [1], |
| 175 | + parser: 'babel-eslint', |
| 176 | + errors: [{message: 'Comments inside children section of tag should be placed inside braces'}] |
| 177 | + }, { |
| 178 | + code: [ |
| 179 | + 'class Comp1 extends Component {', |
| 180 | + ' render() {', |
| 181 | + ' return (', |
| 182 | + ' <div>', |
| 183 | + ' asdjfl', |
| 184 | + ' /* invalid */', |
| 185 | + ' foo', |
| 186 | + ' </div>', |
| 187 | + ' );', |
| 188 | + ' }', |
| 189 | + '}' |
| 190 | + ].join('\n'), |
| 191 | + args: [1], |
| 192 | + parser: 'babel-eslint', |
| 193 | + errors: [{message: 'Comments inside children section of tag should be placed inside braces'}] |
| 194 | + }, { |
| 195 | + code: [ |
| 196 | + 'class Comp1 extends Component {', |
| 197 | + ' render() {', |
| 198 | + ' return (', |
| 199 | + ' <div>', |
| 200 | + ' {\'asdjfl\'}', |
| 201 | + ' // invalid', |
| 202 | + ' {\'foo\'}', |
| 203 | + ' </div>', |
| 204 | + ' );', |
| 205 | + ' }', |
| 206 | + '}' |
| 207 | + ].join('\n'), |
| 208 | + args: [1], |
| 209 | + parser: 'babel-eslint', |
| 210 | + errors: [{message: 'Comments inside children section of tag should be placed inside braces'}] |
| 211 | + } |
| 212 | + ] |
| 213 | +}); |
0 commit comments