|
| 1 | +/* eslint-env jest */ |
| 2 | +/** |
| 3 | + * @fileoverview Enforce label tags have an associated control. |
| 4 | + * @author Jesse Beach |
| 5 | + */ |
| 6 | + |
| 7 | +// ----------------------------------------------------------------------------- |
| 8 | +// Requirements |
| 9 | +// ----------------------------------------------------------------------------- |
| 10 | + |
| 11 | +import { RuleTester } from 'eslint'; |
| 12 | +import parserOptionsMapper from '../../__util__/parserOptionsMapper'; |
| 13 | +import rule from '../../../src/rules/label-has-associated-control'; |
| 14 | + |
| 15 | +// ----------------------------------------------------------------------------- |
| 16 | +// Tests |
| 17 | +// ----------------------------------------------------------------------------- |
| 18 | + |
| 19 | +const ruleTester = new RuleTester(); |
| 20 | + |
| 21 | +const ruleName = 'label-has-associated-control'; |
| 22 | + |
| 23 | +const expectedError = { |
| 24 | + message: 'A form label must be associated with a control.', |
| 25 | + type: 'JSXOpeningElement', |
| 26 | +}; |
| 27 | + |
| 28 | +const alwaysValid = [ |
| 29 | + { code: '<div />' }, |
| 30 | + { code: '<CustomElement />' }, |
| 31 | + { code: '<label htmlFor="js_id"><span><span><span>A label</span></span></span></label>', options: [{ depth: 4 }] }, |
| 32 | + { code: '<label htmlFor="js_id" aria-label="A label" />' }, |
| 33 | + { code: '<label htmlFor="js_id" aria-labelledby="A label" />' }, |
| 34 | + { code: '<label>A label<input /></label>' }, |
| 35 | + { code: '<label><img alt="A label" /><input /></label>' }, |
| 36 | + { code: '<label><img aria-label="A label" /><input /></label>' }, |
| 37 | + { code: '<label><span>A label<input /></span></label>' }, |
| 38 | + { code: '<label><span><span>A label<input /></span></span></label>', options: [{ depth: 3 }] }, |
| 39 | + { code: '<label><span><span><span>A label<input /></span></span></span></label>', options: [{ depth: 4 }] }, |
| 40 | + { code: '<label><span><span><span><span>A label</span><input /></span></span></span></label>', options: [{ depth: 5 }] }, |
| 41 | + { code: '<label><span><span><span><span aria-label="A label" /><input /></span></span></span></label>', options: [{ depth: 5 }] }, |
| 42 | + { code: '<label><span><span><span><input aria-label="A label" /></span></span></span></label>', options: [{ depth: 5 }] }, |
| 43 | + // Custom label component. |
| 44 | + { code: '<CustomLabel htmlFor="js_id" aria-label="A label" />', options: [{ labelComponents: ['CustomLabel'] }] }, |
| 45 | + { code: '<CustomLabel htmlFor="js_id" label="A label" />', options: [{ labelAttributes: ['label'], labelComponents: ['CustomLabel'] }] }, |
| 46 | + // Custom label attributes. |
| 47 | + { code: '<label htmlFor="js_id" label="A label" />', options: [{ labelAttributes: ['label'] }] }, |
| 48 | + // Custom controlComponents. |
| 49 | + { code: '<label><span>A label<CustomInput /></span></label>', options: [{ controlComponents: ['CustomInput'] }] }, |
| 50 | + { code: '<CustomLabel><span>A label<CustomInput /></span></CustomLabel>', options: [{ controlComponents: ['CustomInput'], labelComponents: ['CustomLabel'] }] }, |
| 51 | + { code: '<CustomLabel><span label="A label"><CustomInput /></span></CustomLabel>', options: [{ controlComponents: ['CustomInput'], labelComponents: ['CustomLabel'], labelAttributes: ['label'] }] }, |
| 52 | +]; |
| 53 | +const neverValid = [ |
| 54 | + { code: '<label htmlFor="js_id" />', errors: [expectedError] }, |
| 55 | + { code: '<label htmlFor="js_id"><input /></label>', errors: [expectedError] }, |
| 56 | + { code: '<label></label>', errors: [expectedError] }, |
| 57 | + { code: '<label>A label</label>', errors: [expectedError] }, |
| 58 | + { code: '<div><label /><input /></div>', errors: [expectedError] }, |
| 59 | + { code: '<div><label>A label</label><input /></div>', errors: [expectedError] }, |
| 60 | + // Custom label component. |
| 61 | + { code: '<CustomLabel aria-label="A label" />', options: [{ labelComponents: ['CustomLabel'] }], errors: [expectedError] }, |
| 62 | + { code: '<CustomLabel label="A label" />', options: [{ labelAttributes: ['label'], labelComponents: ['CustomLabel'] }], errors: [expectedError] }, |
| 63 | + // Custom label attributes. |
| 64 | + { code: '<label label="A label" />', options: [{ labelAttributes: ['label'] }], errors: [expectedError] }, |
| 65 | + // Custom controlComponents. |
| 66 | + { code: '<label><span><CustomInput /></span></label>', options: [{ controlComponents: ['CustomInput'] }], errors: [expectedError] }, |
| 67 | + { code: '<CustomLabel><span><CustomInput /></span></CustomLabel>', options: [{ controlComponents: ['CustomInput'], labelComponents: ['CustomLabel'] }], errors: [expectedError] }, |
| 68 | + { code: '<CustomLabel><span><CustomInput /></span></CustomLabel>', options: [{ controlComponents: ['CustomInput'], labelComponents: ['CustomLabel'], labelAttributes: ['label'] }], errors: [expectedError] }, |
| 69 | +]; |
| 70 | + |
| 71 | +ruleTester.run(ruleName, rule, { |
| 72 | + valid: [ |
| 73 | + ...alwaysValid, |
| 74 | + ].map(parserOptionsMapper), |
| 75 | + invalid: [ |
| 76 | + ...neverValid, |
| 77 | + ].map(parserOptionsMapper), |
| 78 | +}); |
0 commit comments