@@ -9,19 +9,34 @@ import { Rule } from "eslint";
99import ruleTester from "./helper/ruleTester" ;
1010import rule from "../../../lib/rules/input-components-require-accessible-name" ;
1111import { applicableComponents } from "../../../lib/applicableComponents/inputBasedComponents" ;
12+ import { labelBasedComponents , elementsUsedAsLabels } from "../../../lib/applicableComponents/labelBasedComponents" ;
1213
1314//------------------------------------------------------------------------------
1415// Helper function to generate test cases
1516//------------------------------------------------------------------------------
16- function generateTestCases ( componentName : string ) {
17+ function generateTestCases ( labelComponent : string , componentName : string ) {
1718 return {
1819 valid : [
19- `<><Label htmlFor="some-id">Some Label</Label><${ componentName } id="some-id"/></>` ,
20- `<><Label id="test-span">Some Label</Label><${ componentName } id="some-id" aria-labelledby="test-span"/></>` ,
21- `<Label>test</Label>` ,
22- `<Label>test<${ componentName } /></Label>` ,
23- `<Label>test<SomeNesting><${ componentName } /></SomeNesting></Label>` ,
24- `<Field label="${ componentName } "><${ componentName } /></Field>` ,
20+ `<><${ labelComponent } id="test-span">Some Label</${ labelComponent } ><${ componentName } id="some-id" aria-labelledby="test-span"/></>`
21+ ] ,
22+ invalid : [
23+ {
24+ code : `<><${ labelComponent } id="test-span-2">Some Label</${ labelComponent } ><${ componentName } id="some-id" aria-labelledby="test-span"/></>` ,
25+ errors : [ { messageId : "missingLabelOnInput" } ]
26+ }
27+ ]
28+ } ;
29+ }
30+
31+ function generateTestCasesLabel ( labelComponent : string , componentName : string ) {
32+ return {
33+ valid : [
34+ `<><${ labelComponent } htmlFor="some-id">Some Label</${ labelComponent } ><${ componentName } id="some-id"/></>` ,
35+ `<><${ labelComponent } id="test-span">Some Label</${ labelComponent } ><${ componentName } id="some-id" aria-labelledby="test-span"/></>` ,
36+ `<${ labelComponent } >test</${ labelComponent } >` ,
37+ `<${ labelComponent } >test<${ componentName } /></${ labelComponent } >` ,
38+ `<${ labelComponent } >test<SomeNesting><${ componentName } /></SomeNesting></${ labelComponent } >` ,
39+ `<Field label="this is my label"><${ componentName } /></Field>` ,
2540 `<${ componentName } aria-label="this is my component" />`
2641 ] ,
2742 invalid : [
@@ -30,19 +45,19 @@ function generateTestCases(componentName: string) {
3045 errors : [ { messageId : "missingLabelOnInput" } ]
3146 } ,
3247 {
33- code : `<><Label /><${ componentName } /></>` ,
48+ code : `<><${ labelComponent } /><${ componentName } /></>` ,
3449 errors : [ { messageId : "missingLabelOnInput" } ]
3550 } ,
3651 {
37- code : `<><Label htmlFor="id"/><${ componentName } /></>` ,
52+ code : `<><${ labelComponent } htmlFor="id"/><${ componentName } /></>` ,
3853 errors : [ { messageId : "missingLabelOnInput" } ]
3954 } ,
4055 {
4156 code : `<${ componentName } id="some-id"/>` ,
4257 errors : [ { messageId : "missingLabelOnInput" } ]
4358 } ,
4459 {
45- code : `<><Label >Some Label</Label ><${ componentName } id="some-id"/></>` ,
60+ code : `<><${ labelComponent } >Some Label</${ labelComponent } ><${ componentName } id="some-id"/></>` ,
4661 errors : [ { messageId : "missingLabelOnInput" } ]
4762 } ,
4863 {
@@ -53,14 +68,33 @@ function generateTestCases(componentName: string) {
5368 } ;
5469}
5570
71+ function generateAllTestCases ( ) {
72+ const testSets : any [ ] = [ ] ;
73+
74+ // For each input-based component, generate test cases
75+ applicableComponents . forEach ( components => {
76+ elementsUsedAsLabels . forEach ( labels => {
77+ testSets . push ( generateTestCases ( labels , components ) ) ;
78+ } ) ;
79+
80+ // Also generate test cases for each native DOM element
81+ labelBasedComponents . forEach ( labels => {
82+ testSets . push ( generateTestCasesLabel ( labels , components ) ) ;
83+ } ) ;
84+ } ) ;
85+
86+ return testSets ;
87+ }
88+
5689// Collect all test cases for all applicable components
57- const allTestCases = applicableComponents . flatMap ( component => generateTestCases ( component ) ) ;
90+ const allTestCases = generateAllTestCases ( ) ;
5891
5992//------------------------------------------------------------------------------
6093// Tests
6194//------------------------------------------------------------------------------
62-
63- ruleTester . run ( "input-missing-label" , rule as unknown as Rule . RuleModule , {
64- valid : allTestCases . flatMap ( test => test . valid ) ,
65- invalid : allTestCases . flatMap ( test => test . invalid )
95+ allTestCases . forEach ( ( testCaseSet , index ) => {
96+ ruleTester . run ( `input-missing-label test set ${ index + 1 } ` , rule as unknown as Rule . RuleModule , {
97+ valid : testCaseSet . valid ,
98+ invalid : testCaseSet . invalid
99+ } ) ;
66100} ) ;
0 commit comments