|
| 1 | +/* eslint-env jest */ |
| 2 | +/** |
| 3 | + * @fileoverview Enforce links may not point to just #. |
| 4 | + * @author Ethan Cohen |
| 5 | + */ |
| 6 | + |
| 7 | +// ----------------------------------------------------------------------------- |
| 8 | +// Requirements |
| 9 | +// ----------------------------------------------------------------------------- |
| 10 | + |
| 11 | +import { RuleTester } from 'eslint'; |
| 12 | +import parserOptionsMapper from '../../__util__/parserOptionsMapper'; |
| 13 | +import rule from '../../../src/rules/href-no-hash'; |
| 14 | + |
| 15 | +// ----------------------------------------------------------------------------- |
| 16 | +// Tests |
| 17 | +// ----------------------------------------------------------------------------- |
| 18 | + |
| 19 | +const ruleTester = new RuleTester(); |
| 20 | + |
| 21 | +const expectedError = { |
| 22 | + message: 'Links must not point to "#". Use a more descriptive href or use a button instead.', |
| 23 | + type: 'JSXOpeningElement', |
| 24 | +}; |
| 25 | + |
| 26 | +const components = [{ |
| 27 | + components: ['Anchor', 'Link'], |
| 28 | +}]; |
| 29 | +const specialLink = [{ |
| 30 | + specialLink: ['hrefLeft', 'hrefRight'], |
| 31 | +}]; |
| 32 | +const componentsAndSpecialLink = [{ |
| 33 | + components: ['Anchor'], |
| 34 | + specialLink: ['hrefLeft'], |
| 35 | +}]; |
| 36 | + |
| 37 | +ruleTester.run('href-no-hash', rule, { |
| 38 | + valid: [ |
| 39 | + // DEFAULT ELEMENT 'a' TESTS |
| 40 | + { code: '<Anchor />;' }, |
| 41 | + { code: '<a {...props} />' }, |
| 42 | + { code: '<a href="foo" />' }, |
| 43 | + { code: '<a href={foo} />' }, |
| 44 | + { code: '<a href="/foo" />' }, |
| 45 | + { code: '<a href={`${undefined}`} />' }, |
| 46 | + { code: '<div href="foo" />' }, |
| 47 | + { code: '<a href={`${undefined}foo`}/>' }, |
| 48 | + { code: '<a href={`#${undefined}foo`}/>' }, |
| 49 | + { code: '<a href={`#foo`}/>' }, |
| 50 | + { code: '<a href={"foo"}/>' }, |
| 51 | + { code: '<a href="#foo" />' }, |
| 52 | + { code: '<UX.Layout>test</UX.Layout>' }, |
| 53 | + { code: '<a href={this} />' }, |
| 54 | + |
| 55 | + // CUSTOM ELEMENT TEST FOR ARRAY OPTION |
| 56 | + { code: '<Anchor />;', options: components }, |
| 57 | + { code: '<Anchor {...props} />', options: components }, |
| 58 | + { code: '<Anchor href="foo" />', options: components }, |
| 59 | + { code: '<Anchor href={foo} />', options: components }, |
| 60 | + { code: '<Anchor href="/foo" />', options: components }, |
| 61 | + { code: '<Anchor href={`${undefined}`} />', options: components }, |
| 62 | + { code: '<div href="foo" />', options: components }, |
| 63 | + { code: '<Anchor href={`${undefined}foo`}/>', options: components }, |
| 64 | + { code: '<Anchor href={`#${undefined}foo`}/>', options: components }, |
| 65 | + { code: '<Anchor href={`#foo`}/>', options: components }, |
| 66 | + { code: '<Anchor href={"foo"}/>', options: components }, |
| 67 | + { code: '<Anchor href="#foo" />', options: components }, |
| 68 | + { code: '<Link />;', options: components }, |
| 69 | + { code: '<Link {...props} />', options: components }, |
| 70 | + { code: '<Link href="foo" />', options: components }, |
| 71 | + { code: '<Link href={foo} />', options: components }, |
| 72 | + { code: '<Link href="/foo" />', options: components }, |
| 73 | + { code: '<Link href={`${undefined}`} />', options: components }, |
| 74 | + { code: '<div href="foo" />', options: components }, |
| 75 | + { code: '<Link href={`${undefined}foo`}/>', options: components }, |
| 76 | + { code: '<Link href={`#${undefined}foo`}/>', options: components }, |
| 77 | + { code: '<Link href={`#foo`}/>', options: components }, |
| 78 | + { code: '<Link href={"foo"}/>', options: components }, |
| 79 | + { code: '<Link href="#foo" />', options: components }, |
| 80 | + |
| 81 | + // CUSTOM PROP TESTS |
| 82 | + { code: '<a />;', options: specialLink }, |
| 83 | + { code: '<a {...props} />', options: specialLink }, |
| 84 | + { code: '<a hrefLeft="foo" />', options: specialLink }, |
| 85 | + { code: '<a hrefLeft={foo} />', options: specialLink }, |
| 86 | + { code: '<a hrefLeft="/foo" />', options: specialLink }, |
| 87 | + { code: '<a hrefLeft={`${undefined}`} />', options: specialLink }, |
| 88 | + { code: '<div hrefLeft="foo" />', options: specialLink }, |
| 89 | + { code: '<a hrefLeft={`${undefined}foo`}/>', options: specialLink }, |
| 90 | + { code: '<a hrefLeft={`#${undefined}foo`}/>', options: specialLink }, |
| 91 | + { code: '<a hrefLeft={`#foo`}/>', options: specialLink }, |
| 92 | + { code: '<a hrefLeft={"foo"}/>', options: specialLink }, |
| 93 | + { code: '<a hrefLeft="#foo" />', options: specialLink }, |
| 94 | + { code: '<UX.Layout>test</UX.Layout>', options: specialLink }, |
| 95 | + { code: '<a hrefRight={this} />', options: specialLink }, |
| 96 | + { code: '<a />;', options: specialLink }, |
| 97 | + { code: '<a {...props} />', options: specialLink }, |
| 98 | + { code: '<a hrefRight="foo" />', options: specialLink }, |
| 99 | + { code: '<a hrefRight={foo} />', options: specialLink }, |
| 100 | + { code: '<a hrefRight="/foo" />', options: specialLink }, |
| 101 | + { code: '<a hrefRight={`${undefined}`} />', options: specialLink }, |
| 102 | + { code: '<div hrefRight="foo" />', options: specialLink }, |
| 103 | + { code: '<a hrefRight={`${undefined}foo`}/>', options: specialLink }, |
| 104 | + { code: '<a hrefRight={`#${undefined}foo`}/>', options: specialLink }, |
| 105 | + { code: '<a hrefRight={`#foo`}/>', options: specialLink }, |
| 106 | + { code: '<a hrefRight={"foo"}/>', options: specialLink }, |
| 107 | + { code: '<a hrefRight="#foo" />', options: specialLink }, |
| 108 | + { code: '<UX.Layout>test</UX.Layout>', options: specialLink }, |
| 109 | + { code: '<a hrefRight={this} />', options: specialLink }, |
| 110 | + |
| 111 | + // CUSTOM BOTH COMPONENTS AND SPECIALLINK TESTS |
| 112 | + { code: '<Anchor />;', options: componentsAndSpecialLink }, |
| 113 | + { code: '<Anchor {...props} />', options: componentsAndSpecialLink }, |
| 114 | + { code: '<Anchor hrefLeft="foo" />', options: componentsAndSpecialLink }, |
| 115 | + { code: '<Anchor hrefLeft={foo} />', options: componentsAndSpecialLink }, |
| 116 | + { code: '<Anchor hrefLeft="/foo" />', options: componentsAndSpecialLink }, |
| 117 | + { code: '<Anchor hrefLeft={`${undefined}`} />', options: componentsAndSpecialLink }, |
| 118 | + { code: '<div hrefLeft="foo" />', options: componentsAndSpecialLink }, |
| 119 | + { code: '<Anchor hrefLeft={`${undefined}foo`}/>', options: componentsAndSpecialLink }, |
| 120 | + { code: '<Anchor hrefLeft={`#${undefined}foo`}/>', options: componentsAndSpecialLink }, |
| 121 | + { code: '<Anchor hrefLeft={`#foo`}/>', options: componentsAndSpecialLink }, |
| 122 | + { code: '<Anchor hrefLeft={"foo"}/>', options: componentsAndSpecialLink }, |
| 123 | + { code: '<Anchor hrefLeft="#foo" />', options: componentsAndSpecialLink }, |
| 124 | + { code: '<UX.Layout>test</UX.Layout>', options: componentsAndSpecialLink }, |
| 125 | + ].map(parserOptionsMapper), |
| 126 | + invalid: [ |
| 127 | + // DEFAULT ELEMENT 'a' TESTS |
| 128 | + { code: '<a href="#" />', errors: [expectedError] }, |
| 129 | + { code: '<a href={"#"} />', errors: [expectedError] }, |
| 130 | + { code: '<a href={`#${undefined}`} />', errors: [expectedError] }, |
| 131 | + |
| 132 | + // CUSTOM ELEMENT TEST FOR ARRAY OPTION |
| 133 | + { code: '<Link href="#" />', errors: [expectedError], options: components }, |
| 134 | + { code: '<Link href={"#"} />', errors: [expectedError], options: components }, |
| 135 | + { |
| 136 | + code: '<Link href={`#${undefined}`} />', |
| 137 | + errors: [expectedError], |
| 138 | + options: components, |
| 139 | + }, |
| 140 | + { code: '<Anchor href="#" />', errors: [expectedError], options: components }, |
| 141 | + { code: '<Anchor href={"#"} />', errors: [expectedError], options: components }, |
| 142 | + { |
| 143 | + code: '<Anchor href={`#${undefined}`} />', |
| 144 | + errors: [expectedError], |
| 145 | + options: components, |
| 146 | + }, |
| 147 | + |
| 148 | + // CUSTOM PROP TESTS |
| 149 | + { code: '<a hrefLeft="#" />', errors: [expectedError], options: specialLink }, |
| 150 | + { code: '<a hrefLeft={"#"} />', errors: [expectedError], options: specialLink }, |
| 151 | + { |
| 152 | + code: '<a hrefLeft={`#${undefined}`} />', |
| 153 | + errors: [expectedError], |
| 154 | + options: specialLink, |
| 155 | + }, |
| 156 | + { code: '<a hrefRight="#" />', errors: [expectedError], options: specialLink }, |
| 157 | + { code: '<a hrefRight={"#"} />', errors: [expectedError], options: specialLink }, |
| 158 | + { |
| 159 | + code: '<a hrefRight={`#${undefined}`} />', |
| 160 | + errors: [expectedError], |
| 161 | + options: specialLink, |
| 162 | + }, |
| 163 | + |
| 164 | + // CUSTOM BOTH COMPONENTS AND SPECIALLINK TESTS |
| 165 | + { code: '<Anchor hrefLeft="#" />', errors: [expectedError], options: componentsAndSpecialLink }, |
| 166 | + { code: '<Anchor hrefLeft={"#"} />', errors: [expectedError], options: componentsAndSpecialLink }, |
| 167 | + { |
| 168 | + code: '<Anchor hrefLeft={`#${undefined}`} />', |
| 169 | + errors: [expectedError], |
| 170 | + options: componentsAndSpecialLink, |
| 171 | + }, |
| 172 | + ].map(parserOptionsMapper), |
| 173 | +}); |
0 commit comments