From bfb0e9e89ae1605dcd1e79eacb745e819ae45f27 Mon Sep 17 00:00:00 2001 From: michael faith Date: Fri, 20 Dec 2024 16:38:23 -0600 Subject: [PATCH] [patch] `label-has-associated-control`: improve error messages This change updates the error messages of the label-has-associated-control rule so that each assert type gets an error message with verbiage specific to the assertion. I wanted to land this before adding support for matching a label's htmlFor attribute with the associated control's id --- .../label-has-associated-control-test.js | 156 ++++++++++-------- src/rules/label-has-associated-control.js | 58 ++++--- 2 files changed, 123 insertions(+), 91 deletions(-) diff --git a/__tests__/src/rules/label-has-associated-control-test.js b/__tests__/src/rules/label-has-associated-control-test.js index 633275c3..01d02e06 100644 --- a/__tests__/src/rules/label-has-associated-control-test.js +++ b/__tests__/src/rules/label-has-associated-control-test.js @@ -21,15 +21,20 @@ const ruleTester = new RuleTester(); const ruleName = 'label-has-associated-control'; -const expectedError = { - message: 'A form label must be associated with a control.', - type: 'JSXOpeningElement', -}; - -const expectedErrorNoLabel = { - message: 'A form label must have accessible text.', - type: 'JSXOpeningElement', +const errorMessages = { + accessibleLabel: 'A form label must have accessible text.', + htmlFor: 'A form label must have a valid htmlFor attribute.', + nesting: 'A form label must have an associated control as a descendant.', + either: 'A form label must either have a valid htmlFor attribute or a control as a descendant.', + both: 'A form label must have a valid htmlFor attribute and a control as a descendant.', }; +const expectedErrors = {}; +Object.keys(errorMessages).forEach((key) => { + expectedErrors[key] = { + message: errorMessages[key], + type: 'JSXOpeningElement', + }; +}); const componentsSettings = { 'jsx-a11y': { @@ -123,59 +128,68 @@ const alwaysValid = [ { code: '' }, ]; -const htmlForInvalid = [ - { code: '', options: [{ depth: 4 }], errors: [expectedError] }, - { code: '