|
| 1 | +const rule = require('../enforce-button-for-link-with-nohref') |
| 2 | +const {RuleTester} = require('eslint') |
| 3 | + |
| 4 | +const ruleTester = new RuleTester({ |
| 5 | + parserOptions: { |
| 6 | + ecmaVersion: 'latest', |
| 7 | + sourceType: 'module', |
| 8 | + ecmaFeatures: { |
| 9 | + jsx: true, |
| 10 | + }, |
| 11 | + }, |
| 12 | +}) |
| 13 | + |
| 14 | +ruleTester.run('enforce-button-for-link-with-nohref', rule, { |
| 15 | + valid: [ |
| 16 | + // Link with href attribute |
| 17 | + `import {Link} from '@primer/react'; |
| 18 | + <Link href="https://example.com">Valid Link</Link>`, |
| 19 | + |
| 20 | + // Link with href and inline prop |
| 21 | + `import {Link} from '@primer/react'; |
| 22 | + <Link inline href="https://example.com">Valid Inline Link</Link>`, |
| 23 | + |
| 24 | + // Link with href and className |
| 25 | + `import {Link} from '@primer/react'; |
| 26 | + <Link className="some-class" href="https://example.com">Valid Link with Class</Link>`, |
| 27 | + |
| 28 | + // Link with href, inline, and className |
| 29 | + `import {Link} from '@primer/react'; |
| 30 | + <Link className="some-class" inline href="https://example.com">Valid Inline Link with Class</Link>`, |
| 31 | + |
| 32 | + // Link with href as variable |
| 33 | + `import {Link} from '@primer/react'; |
| 34 | + const url = '/about'; |
| 35 | + <Link href={url}>About</Link>`, |
| 36 | + |
| 37 | + // Button component (not Link) |
| 38 | + `import {Button} from '@primer/react'; |
| 39 | + <Button onClick={handleClick}>Click me</Button>`, |
| 40 | + |
| 41 | + // Regular HTML link (not Primer Link) |
| 42 | + `<a onClick={handleClick}>Click me</a>`, |
| 43 | + |
| 44 | + // Link from different package |
| 45 | + `import {Link} from 'react-router-dom'; |
| 46 | + <Link to="/about">About</Link>`, |
| 47 | + ], |
| 48 | + invalid: [ |
| 49 | + { |
| 50 | + code: `import {Link} from '@primer/react'; |
| 51 | + <Link>Invalid Link without href</Link>`, |
| 52 | + errors: [ |
| 53 | + { |
| 54 | + messageId: 'noLinkWithoutHref', |
| 55 | + }, |
| 56 | + ], |
| 57 | + }, |
| 58 | + { |
| 59 | + code: `import {Link} from '@primer/react'; |
| 60 | + <Link className="some-class">Invalid Link with class but no href</Link>`, |
| 61 | + errors: [ |
| 62 | + { |
| 63 | + messageId: 'noLinkWithoutHref', |
| 64 | + }, |
| 65 | + ], |
| 66 | + }, |
| 67 | + { |
| 68 | + code: `import {Link} from '@primer/react'; |
| 69 | + <Link inline>Invalid inline Link without href</Link>`, |
| 70 | + errors: [ |
| 71 | + { |
| 72 | + messageId: 'noLinkWithoutHref', |
| 73 | + }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + { |
| 77 | + code: `import {Link} from '@primer/react'; |
| 78 | + <Link onClick={handleClick}>Invalid Link with onClick but no href</Link>`, |
| 79 | + errors: [ |
| 80 | + { |
| 81 | + messageId: 'noLinkWithoutHref', |
| 82 | + }, |
| 83 | + ], |
| 84 | + }, |
| 85 | + ], |
| 86 | +}) |
0 commit comments