|
| 1 | +const rule = require('../a11y-no-duplicate-form-labels') |
| 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('a11y-no-duplicate-form-labels', rule, { |
| 15 | + valid: [ |
| 16 | + // TextInput without aria-label is valid |
| 17 | + `import {FormControl, TextInput} from '@primer/react'; |
| 18 | + <FormControl> |
| 19 | + <FormControl.Label>Form Input Label</FormControl.Label> |
| 20 | + <TextInput /> |
| 21 | + </FormControl>`, |
| 22 | + |
| 23 | + // TextInput with aria-label but no FormControl.Label is valid |
| 24 | + `import {FormControl, TextInput} from '@primer/react'; |
| 25 | + <FormControl> |
| 26 | + <TextInput aria-label="Form Input Label" /> |
| 27 | + </FormControl>`, |
| 28 | + |
| 29 | + // TextInput with aria-label outside FormControl is valid |
| 30 | + `import {TextInput} from '@primer/react'; |
| 31 | + <TextInput aria-label="Form Input Label" />`, |
| 32 | + |
| 33 | + // TextInput with visuallyHidden FormControl.Label is valid |
| 34 | + `import {FormControl, TextInput} from '@primer/react'; |
| 35 | + <FormControl> |
| 36 | + <FormControl.Label visuallyHidden>Form Input Label</FormControl.Label> |
| 37 | + <TextInput /> |
| 38 | + </FormControl>`, |
| 39 | + |
| 40 | + // FormControl without FormControl.Label but with aria-label is valid |
| 41 | + `import {FormControl, TextInput} from '@primer/react'; |
| 42 | + <FormControl> |
| 43 | + <TextInput aria-label="Form Input Label" /> |
| 44 | + </FormControl>`, |
| 45 | + |
| 46 | + // Multiple TextInputs with different approaches |
| 47 | + `import {FormControl, TextInput} from '@primer/react'; |
| 48 | + <div> |
| 49 | + <FormControl> |
| 50 | + <FormControl.Label>Visible Label</FormControl.Label> |
| 51 | + <TextInput /> |
| 52 | + </FormControl> |
| 53 | + <FormControl> |
| 54 | + <TextInput aria-label="Standalone Input" /> |
| 55 | + </FormControl> |
| 56 | + </div>`, |
| 57 | + ], |
| 58 | + invalid: [ |
| 59 | + { |
| 60 | + code: `import {FormControl, TextInput} from '@primer/react'; |
| 61 | + <FormControl> |
| 62 | + <FormControl.Label>Form Input Label</FormControl.Label> |
| 63 | + <TextInput aria-label="Form Input Label" /> |
| 64 | + </FormControl>`, |
| 65 | + errors: [ |
| 66 | + { |
| 67 | + messageId: 'duplicateLabel', |
| 68 | + }, |
| 69 | + ], |
| 70 | + }, |
| 71 | + { |
| 72 | + code: `import {FormControl, TextInput} from '@primer/react'; |
| 73 | + <FormControl> |
| 74 | + <FormControl.Label>Username</FormControl.Label> |
| 75 | + <TextInput aria-label="Enter your username" /> |
| 76 | + </FormControl>`, |
| 77 | + errors: [ |
| 78 | + { |
| 79 | + messageId: 'duplicateLabel', |
| 80 | + }, |
| 81 | + ], |
| 82 | + }, |
| 83 | + { |
| 84 | + code: `import {FormControl, TextInput} from '@primer/react'; |
| 85 | + <FormControl> |
| 86 | + <FormControl.Label visuallyHidden>Password</FormControl.Label> |
| 87 | + <TextInput aria-label="Enter password" /> |
| 88 | + </FormControl>`, |
| 89 | + errors: [ |
| 90 | + { |
| 91 | + messageId: 'duplicateLabel', |
| 92 | + }, |
| 93 | + ], |
| 94 | + }, |
| 95 | + { |
| 96 | + code: `import {FormControl, TextInput} from '@primer/react'; |
| 97 | + <div> |
| 98 | + <FormControl> |
| 99 | + <FormControl.Label>Email</FormControl.Label> |
| 100 | + <div> |
| 101 | + <TextInput aria-label="Email address" /> |
| 102 | + </div> |
| 103 | + </FormControl> |
| 104 | + </div>`, |
| 105 | + errors: [ |
| 106 | + { |
| 107 | + messageId: 'duplicateLabel', |
| 108 | + }, |
| 109 | + ], |
| 110 | + }, |
| 111 | + ], |
| 112 | +}) |
0 commit comments