|
| 1 | +const rule = require('../../../lib/rules/template-require-valid-form-groups'); |
| 2 | +const RuleTester = require('eslint').RuleTester; |
| 3 | + |
| 4 | +const ruleTester = new RuleTester({ |
| 5 | + parser: require.resolve('ember-eslint-parser'), |
| 6 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 7 | +}); |
| 8 | + |
| 9 | +ruleTester.run('template-require-valid-form-groups', rule, { |
| 10 | + valid: [ |
| 11 | + `<template> |
| 12 | + <fieldset> |
| 13 | + <legend>Preferred Mascot Version</legend> |
| 14 | + <div> |
| 15 | + <label for="radio-001">Chicago Zoey</label> |
| 16 | + <input id="radio-001" type="radio" name="prefMascot-Zoey" value="chicago zoey" /> |
| 17 | + </div> |
| 18 | + <div> |
| 19 | + <label for="radio-002">Office Hours Tomster</label> |
| 20 | + <input |
| 21 | + id="radio-002" |
| 22 | + type="radio" |
| 23 | + name="prefMascot-OfficeHoursTomster" |
| 24 | + value="office hours tomster" |
| 25 | + /> |
| 26 | + </div> |
| 27 | + <div> |
| 28 | + <label for="radio-003">A11y Zoey</label> |
| 29 | + <input id="radio-003" type="radio" name="prefMascot-Zoey" value="a11y zoey" /> |
| 30 | + </div> |
| 31 | + </fieldset> |
| 32 | + </template>`, |
| 33 | + `<template> |
| 34 | + <div role="group" aria-labelledby="preferred-mascot-heading"> |
| 35 | + <div id="preferred-mascot-heading">Preferred Mascot Version</div> |
| 36 | + <label for="radio-001">Chicago Zoey</label> |
| 37 | + <input id="radio-001" type="radio" name="prefMascot-Zoey" value="chicago zoey" /> |
| 38 | + <label for="radio-002">Office Hours Tomster</label> |
| 39 | + <input |
| 40 | + id="radio-002" |
| 41 | + type="radio" |
| 42 | + name="prefMascot-OfficeHoursTomster" |
| 43 | + value="office hours tomster" |
| 44 | + /> |
| 45 | + <label for="radio-003">A11y Zoey</label> |
| 46 | + <input id="radio-003" type="radio" name="prefMascot-Zoey" value="a11y zoey" /> |
| 47 | + </div> |
| 48 | + </template>`, |
| 49 | + `<template> |
| 50 | + <div> |
| 51 | + <label for="radio-001">Chicago Zoey</label> |
| 52 | + <input id="radio-001" type="radio" name="prefMascot-Zoey" value="chicago zoey" /> |
| 53 | + </div> |
| 54 | + </template>`, |
| 55 | + |
| 56 | + `<template><fieldset> |
| 57 | + <legend>Preferred Mascot Version</legend> |
| 58 | + <div> |
| 59 | + <label for="radio-001">Chicago Zoey</label> |
| 60 | + <input id="radio-001" type="radio" name="prefMascot-Zoey" value="chicago zoey"> |
| 61 | + </div> |
| 62 | + <div> |
| 63 | + <label for="radio-002">Office Hours Tomster</label> |
| 64 | + <input id="radio-002" type="radio" name="prefMascot-OfficeHoursTomster" value="office hours tomster"> |
| 65 | + </div> |
| 66 | + <div> |
| 67 | + <label for="radio-003">A11y Zoey</label> |
| 68 | + <input id="radio-003" type="radio" name="prefMascot-Zoey" value="a11y zoey"> |
| 69 | + </div> |
| 70 | + </fieldset></template>`, |
| 71 | + `<template><div role="group" aria-labelledby="preferred-mascot-heading"> |
| 72 | + <div id="preferred-mascot-heading">Preferred Mascot Version</div> |
| 73 | + <label for="radio-001">Chicago Zoey</label> |
| 74 | + <input id="radio-001" type="radio" name="prefMascot-Zoey" value="chicago zoey"> |
| 75 | + <label for="radio-002">Office Hours Tomster</label> |
| 76 | + <input id="radio-002" type="radio" name="prefMascot-OfficeHoursTomster" value="office hours tomster"> |
| 77 | + <label for="radio-003">A11y Zoey</label> |
| 78 | + <input id="radio-003" type="radio" name="prefMascot-Zoey" value="a11y zoey"> |
| 79 | + </div></template>`, |
| 80 | + `<template><div> |
| 81 | + <label for="radio-001">Chicago Zoey</label> |
| 82 | + <input id="radio-001" type="radio" name="prefMascot-Zoey" value="chicago zoey"> |
| 83 | + </div></template>`, |
| 84 | + ], |
| 85 | + invalid: [ |
| 86 | + { |
| 87 | + code: '<template><div><input name="a1">Chicago Zoey<input name="a2">Chicago Tom</div></template>', |
| 88 | + output: null, |
| 89 | + errors: [{ messageId: 'requireValidFormGroups' }, { messageId: 'requireValidFormGroups' }], |
| 90 | + }, |
| 91 | + { |
| 92 | + code: '<template><div><input id="prefMascot-Zoey"><label for="prefMascot-Zoey" /><input id="prefMascot-tom"><label for="prefMascot-tom" /></div></template>', |
| 93 | + output: null, |
| 94 | + errors: [{ messageId: 'requireValidFormGroups' }, { messageId: 'requireValidFormGroups' }], |
| 95 | + }, |
| 96 | + ], |
| 97 | +}); |
| 98 | + |
| 99 | +const hbsRuleTester = new RuleTester({ |
| 100 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 101 | + parserOptions: { |
| 102 | + ecmaVersion: 2022, |
| 103 | + sourceType: 'module', |
| 104 | + }, |
| 105 | +}); |
| 106 | + |
| 107 | +hbsRuleTester.run('template-require-valid-form-groups', rule, { |
| 108 | + valid: [ |
| 109 | + `<fieldset> |
| 110 | + <legend>Preferred Mascot Version</legend> |
| 111 | + <div> |
| 112 | + <label for="radio-001">Chicago Zoey</label> |
| 113 | + <input id="radio-001" type="radio" name="prefMascot-Zoey" value="chicago zoey"> |
| 114 | + </div> |
| 115 | + <div> |
| 116 | + <label for="radio-002">Office Hours Tomster</label> |
| 117 | + <input id="radio-002" type="radio" name="prefMascot-OfficeHoursTomster" value="office hours tomster"> |
| 118 | + </div> |
| 119 | + <div> |
| 120 | + <label for="radio-003">A11y Zoey</label> |
| 121 | + <input id="radio-003" type="radio" name="prefMascot-Zoey" value="a11y zoey"> |
| 122 | + </div> |
| 123 | + </fieldset>`, |
| 124 | + `<div role="group" aria-labelledby="preferred-mascot-heading"> |
| 125 | + <div id="preferred-mascot-heading">Preferred Mascot Version</div> |
| 126 | + <label for="radio-001">Chicago Zoey</label> |
| 127 | + <input id="radio-001" type="radio" name="prefMascot-Zoey" value="chicago zoey"> |
| 128 | + <label for="radio-002">Office Hours Tomster</label> |
| 129 | + <input id="radio-002" type="radio" name="prefMascot-OfficeHoursTomster" value="office hours tomster"> |
| 130 | + <label for="radio-003">A11y Zoey</label> |
| 131 | + <input id="radio-003" type="radio" name="prefMascot-Zoey" value="a11y zoey"> |
| 132 | + </div>`, |
| 133 | + `<div> |
| 134 | + <label for="radio-001">Chicago Zoey</label> |
| 135 | + <input id="radio-001" type="radio" name="prefMascot-Zoey" value="chicago zoey"> |
| 136 | + </div>`, |
| 137 | + ], |
| 138 | + invalid: [ |
| 139 | + { |
| 140 | + code: '<div><input name="a1">Chicago Zoey<input name="a2">Chicago Tom</div>', |
| 141 | + output: null, |
| 142 | + errors: [ |
| 143 | + { |
| 144 | + message: |
| 145 | + 'Grouped form controls should have appropriate semantics such as fieldset and legend or WAI-ARIA labels', |
| 146 | + }, |
| 147 | + { |
| 148 | + message: |
| 149 | + 'Grouped form controls should have appropriate semantics such as fieldset and legend or WAI-ARIA labels', |
| 150 | + }, |
| 151 | + ], |
| 152 | + }, |
| 153 | + { |
| 154 | + code: '<div><input id="prefMascot-Zoey"><label for="prefMascot-Zoey" /><input id="prefMascot-tom"><label for="prefMascot-tom" /></div>', |
| 155 | + output: null, |
| 156 | + errors: [ |
| 157 | + { |
| 158 | + message: |
| 159 | + 'Grouped form controls should have appropriate semantics such as fieldset and legend or WAI-ARIA labels', |
| 160 | + }, |
| 161 | + { |
| 162 | + message: |
| 163 | + 'Grouped form controls should have appropriate semantics such as fieldset and legend or WAI-ARIA labels', |
| 164 | + }, |
| 165 | + ], |
| 166 | + }, |
| 167 | + ], |
| 168 | +}); |
0 commit comments