|
| 1 | +/** |
| 2 | + * @fileoverview Tests for forbid-foreign-prop-types |
| 3 | + */ |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +// ----------------------------------------------------------------------------- |
| 7 | +// Requirements |
| 8 | +// ----------------------------------------------------------------------------- |
| 9 | + |
| 10 | +var rule = require('../../../lib/rules/forbid-foreign-prop-types'); |
| 11 | +var RuleTester = require('eslint').RuleTester; |
| 12 | + |
| 13 | +var parserOptions = { |
| 14 | + ecmaVersion: 6, |
| 15 | + ecmaFeatures: { |
| 16 | + experimentalObjectRestSpread: true, |
| 17 | + jsx: true |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +require('babel-eslint'); |
| 22 | + |
| 23 | +// ----------------------------------------------------------------------------- |
| 24 | +// Tests |
| 25 | +// ----------------------------------------------------------------------------- |
| 26 | + |
| 27 | +var ERROR_MESSAGE = 'Using another component\'s propTypes is forbidden'; |
| 28 | + |
| 29 | +var ruleTester = new RuleTester(); |
| 30 | +ruleTester.run('forbid-foreign-prop-types', rule, { |
| 31 | + |
| 32 | + valid: [{ |
| 33 | + code: 'import { propTypes } from "SomeComponent";', |
| 34 | + parser: 'babel-eslint' |
| 35 | + }, { |
| 36 | + code: 'import { propTypes as someComponentPropTypes } from "SomeComponent";', |
| 37 | + parser: 'babel-eslint' |
| 38 | + }], |
| 39 | + |
| 40 | + invalid: [{ |
| 41 | + code: [ |
| 42 | + 'var Foo = React.createClass({', |
| 43 | + ' propTypes: Bar.propTypes,', |
| 44 | + ' render: function() {', |
| 45 | + ' return <Foo className="bar" />;', |
| 46 | + ' }', |
| 47 | + '});' |
| 48 | + ].join('\n'), |
| 49 | + parserOptions: parserOptions, |
| 50 | + errors: [{ |
| 51 | + message: ERROR_MESSAGE, |
| 52 | + type: 'Identifier' |
| 53 | + }] |
| 54 | + }, |
| 55 | + { |
| 56 | + code: [ |
| 57 | + 'var Foo = React.createClass({', |
| 58 | + ' propTypes: Bar["propTypes"],', |
| 59 | + ' render: function() {', |
| 60 | + ' return <Foo className="bar" />;', |
| 61 | + ' }', |
| 62 | + '});' |
| 63 | + ].join('\n'), |
| 64 | + parserOptions: parserOptions, |
| 65 | + errors: [{ |
| 66 | + message: ERROR_MESSAGE, |
| 67 | + type: 'Literal' |
| 68 | + }] |
| 69 | + }, |
| 70 | + { |
| 71 | + code: [ |
| 72 | + 'var { propTypes } = SomeComponent', |
| 73 | + 'var Foo = React.createClass({', |
| 74 | + ' propTypes,', |
| 75 | + ' render: function() {', |
| 76 | + ' return <Foo className="bar" />;', |
| 77 | + ' }', |
| 78 | + '});' |
| 79 | + ].join('\n'), |
| 80 | + parserOptions: parserOptions, |
| 81 | + errors: [{ |
| 82 | + message: ERROR_MESSAGE, |
| 83 | + type: 'Property' |
| 84 | + }] |
| 85 | + }, |
| 86 | + { |
| 87 | + code: [ |
| 88 | + 'var { propTypes: typesOfProps } = SomeComponent', |
| 89 | + 'var Foo = React.createClass({', |
| 90 | + ' propTypes: typesOfProps,', |
| 91 | + ' render: function() {', |
| 92 | + ' return <Foo className="bar" />;', |
| 93 | + ' }', |
| 94 | + '});' |
| 95 | + ].join('\n'), |
| 96 | + parserOptions: parserOptions, |
| 97 | + errors: [{ |
| 98 | + message: ERROR_MESSAGE, |
| 99 | + type: 'Property' |
| 100 | + }] |
| 101 | + }] |
| 102 | +}); |
0 commit comments