|
| 1 | +/** |
| 2 | + * @fileoverview Enforce links may not point to just #. |
| 3 | + * @author Ethan Cohen |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +// ----------------------------------------------------------------------------- |
| 9 | +// Requirements |
| 10 | +// ----------------------------------------------------------------------------- |
| 11 | + |
| 12 | +import rule from '../../../src/rules/no-hash-href'; |
| 13 | +import { RuleTester } from 'eslint'; |
| 14 | + |
| 15 | +const parserOptions = { |
| 16 | + ecmaVersion: 6, |
| 17 | + ecmaFeatures: { |
| 18 | + jsx: true |
| 19 | + } |
| 20 | +}; |
| 21 | + |
| 22 | +// ----------------------------------------------------------------------------- |
| 23 | +// Tests |
| 24 | +// ----------------------------------------------------------------------------- |
| 25 | + |
| 26 | +const ruleTester = new RuleTester(); |
| 27 | + |
| 28 | +const expectedError = { |
| 29 | + message: 'Links must not point to "#". Use a more descriptive href or use a button instead.', |
| 30 | + type: 'JSXOpeningElement' |
| 31 | +}; |
| 32 | + |
| 33 | +ruleTester.run('no-hash-href', rule, { |
| 34 | + valid: [ |
| 35 | + { code: '<a />;', parserOptions }, |
| 36 | + { code: '<a {...props} />', parserOptions }, |
| 37 | + { code: '<a href="foo" />', parserOptions }, |
| 38 | + { code: '<a href={foo} />', parserOptions }, |
| 39 | + { code: '<a href="/foo" />', parserOptions }, |
| 40 | + { code: '<a href={`${undefined}`} />', parserOptions }, |
| 41 | + { code: '<div href="foo" />', parserOptions }, |
| 42 | + { code: '<a href={`${undefined}foo`}/>', parserOptions }, |
| 43 | + { code: '<a href={`#${undefined}foo`}/>', parserOptions }, |
| 44 | + { code: '<a href={`#foo`}/>', parserOptions }, |
| 45 | + { code: '<a href={"foo"}/>', parserOptions }, |
| 46 | + { code: '<a href="#foo" />', parserOptions } |
| 47 | + ], |
| 48 | + invalid: [ |
| 49 | + { code: '<a href="#" />', errors: [ expectedError ], parserOptions }, |
| 50 | + { code: '<a href={"#"} />', errors: [ expectedError ], parserOptions }, |
| 51 | + { code: '<a href={`#${undefined}`} />', errors: [ expectedError ], parserOptions } |
| 52 | + ] |
| 53 | +}); |
0 commit comments