Skip to content

Commit 661bbb0

Browse files
Add Test cases for additional coverage.
1 parent 94aedd5 commit 661bbb0

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

tests/lib/rules/combobox-needs-labelling.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ ruleTester.run("combobox-needs-labelling", rule as unknown as Rule.RuleModule, {
2222
'<div><label id="my-label">Best pet</label><Combobox aria-labelledby="my-label" placeholder="Select an animal" {...props}><Option>{"Cat"}</Option></Combobox></div>',
2323
'<div><Label id="my-label">Best pet</Label><Combobox aria-labelledby="my-label" placeholder="Select an animal" {...props}><Option>{"Cat"}</Option></Combobox></div>',
2424
'<div><Label id={"my-label"}>Best pet</Label><Combobox aria-labelledby={"my-label"} placeholder="Select an animal" {...props}><Option>{"Cat"}</Option></Combobox></div>',
25+
'<div><Label id={"my-label" + 1}>Best pet</Label><Combobox aria-labelledby={"my-label" + 1} placeholder="Select an animal" {...props}><Option>{"Cat"}</Option></Combobox></div>',
26+
'<div><Label id={`my-label-${value}`}>Best pet</Label><Combobox aria-labelledby={`my-label-${value}`} placeholder="Select an animal" {...props}><Option>{"Cat"}</Option></Combobox></div>',
2527
'<div><label htmlFor="my-input">Best pet</label><Combobox id="my-input" placeholder="Select an animal" {...props}><Option>{"Cat"}</Option></Combobox></div>',
2628
'<div><Label htmlFor="my-input">Best pet</Label><Combobox id="my-input" placeholder="Select an animal" {...props}><Option>{"Cat"}</Option></Combobox></div>',
2729
'<div><Label htmlFor={myInputId}>Best pet</Label><Combobox id={myInputId} placeholder="Select an animal" {...props}><Option>{"Cat"}</Option></Combobox></div>',
@@ -39,6 +41,10 @@ ruleTester.run("combobox-needs-labelling", rule as unknown as Rule.RuleModule, {
3941
{
4042
code: "<><Field></Field><Combobox /></>",
4143
errors: [{ messageId: "noUnlabelledCombobox" }]
44+
},
45+
{
46+
code: '<div><Label id={my-label}>Best pet</Label><Combobox aria-labelledby={my-label} placeholder="Select an animal" {...props}><Option>{"Cat"}</Option></Combobox></div>',
47+
errors: [{ messageId: "noUnlabelledCombobox" }]
4248
}
4349
]
4450
});

tests/lib/rules/dropdown-needs-labelling.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ ruleTester.run("dropdown-needs-labelling", rule as unknown as Rule.RuleModule, {
1717
valid: [
1818
`<><Label htmlFor={comboId}>Best pet</Label> <Dropdown id={comboId} multiselect={true} placeholder="Select an animal" {...props} > {options.map((option) => ( <Option key={option} disabled={option === "Ferret"}> {option} </Option> ))}</Dropdown></>`,
1919
`<><Label id="my-dropdownid" /><Dropdown aria-labelledby="my-dropdownid" /></>`,
20+
21+
// expression forms: binary concatenation and template literal (cover expression-literal forms)
22+
`<><Label id={"my-label" + 1}>Best pet</Label> <Dropdown aria-labelledby={"my-label" + 1} multiselect={true} placeholder="Select an animal" {...props} > {options.map((option) => ( <Option key={option} disabled={option === "Ferret"}> {option} </Option> ))}</Dropdown></>`,
23+
`<><Label id={\`my-label-\${value}\`}>Best pet</Label> <Dropdown aria-labelledby={\`my-label-\${value}\`} multiselect={true} placeholder="Select an animal" {...props} > {options.map((option) => ( <Option key={option} disabled={option === "Ferret"}> {option} </Option> ))}</Dropdown></>`,
24+
2025
`<><Label id={comboId}>Best pet</Label> <Dropdown aria-labelledby={comboId} multiselect={true} placeholder="Select an animal" {...props} > {options.map((option) => ( <Option key={option} disabled={option === "Ferret"}> {option} </Option> ))}</Dropdown></>`,
2126
`<><Label id={comboId2}>This is a Dropdown</Label><Dropdown aria-labelledby={comboId2} /></>`,
2227
`<Dropdown aria-label="This is a Dropdown" />`,

tests/lib/rules/input-components-require-accessible-name.test.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,22 @@ import { labelBasedComponents, elementsUsedAsLabels } from "../../../lib/applica
1717
function generateTestCases(labelComponent: string, componentName: string) {
1818
return {
1919
valid: [
20-
`<><${labelComponent} id="test-span">Some Label</${labelComponent}><${componentName} id="some-id" aria-labelledby="test-span"/></>`
20+
`<><${labelComponent} id="test-span">Some Label</${labelComponent}><${componentName} id="some-id" aria-labelledby="test-span"/></>`,
21+
// expression forms: binary concatenation and template literal (cover expression-literal forms)
22+
"<><" +
23+
labelComponent +
24+
' id={"my-label" + 1}>Some Label</' +
25+
labelComponent +
26+
"><" +
27+
componentName +
28+
' aria-labelledby={"my-label" + 1}/></>',
29+
"<><" +
30+
labelComponent +
31+
" id={`my-label-${value}`}>Some Label</" +
32+
labelComponent +
33+
"><" +
34+
componentName +
35+
" aria-labelledby={`my-label-${value}`}/></>"
2136
],
2237
invalid: [
2338
{
@@ -38,7 +53,22 @@ function generateTestCasesLabel(labelComponent: string, componentName: string) {
3853
`<${labelComponent}>test<SomeNesting><${componentName} /></SomeNesting></${labelComponent}>`,
3954
`<Field label="this is my label"><${componentName} /></Field>`,
4055
`<${componentName} aria-label="this is my component" />`,
41-
`<><${labelComponent} id="paragraph_label-2">type here</${labelComponent}><${componentName} aria-labelledby="paragraph_label-2"></${componentName}><${labelComponent} id="paragraph_label-3">type here</${labelComponent}><${componentName} aria-labelledby="paragraph_label-3"></${componentName}></>`
56+
`<><${labelComponent} id="paragraph_label-2">type here</${labelComponent}><${componentName} aria-labelledby="paragraph_label-2"></${componentName}><${labelComponent} id="paragraph_label-3">type here</${labelComponent}><${componentName} aria-labelledby="paragraph_label-3"></${componentName}></>`,
57+
// expression forms for htmlFor/id pairing
58+
"<><" +
59+
labelComponent +
60+
' htmlFor={"my-input" + 1}>Some Label</' +
61+
labelComponent +
62+
"><" +
63+
componentName +
64+
' id={"my-input" + 1}/></>',
65+
"<><" +
66+
labelComponent +
67+
" htmlFor={`my-input-${value}`}>Some Label</" +
68+
labelComponent +
69+
"><" +
70+
componentName +
71+
" id={`my-input-${value}`}/></>"
4272
],
4373
invalid: [
4474
{

0 commit comments

Comments
 (0)