Skip to content

fix(ByLabelText): improve error message when label is associated with… #720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 113 additions & 3 deletions src/__tests__/element-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,17 +352,19 @@ test('label with no form control', () => {
`)
})

test('label with no form control and fuzzy matcher', () => {
test('label with "for" attribute but no form control and fuzzy matcher', () => {
const {getByLabelText, queryByLabelText} = render(
`<label>All alone label</label>`,
`<label for="foo">All alone label</label>`,
)
expect(queryByLabelText('alone', {exact: false})).toBeNull()
expect(() => getByLabelText('alone', {exact: false}))
.toThrowErrorMatchingInlineSnapshot(`
"Found a label with the text of: alone, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly.

<div>
<label>
<label
for="foo"
>
All alone label
</label>
</div>"
Expand Down Expand Up @@ -407,6 +409,114 @@ test('label with children with no form control', () => {
`)
})

test('label with non-labellable element', () => {
const {getByLabelText, queryByLabelText} = render(`
<div>
<label for="div1">Label 1</label>
<div id="div1">
Hello
</div>
</div>
`)

expect(queryByLabelText(/Label/)).toBeNull()
expect(() => getByLabelText(/Label/)).toThrowErrorMatchingInlineSnapshot(`
"Found a label with the text of: /Label/, however the element associated with this label (<div />) is non-labellable [https://html.spec.whatwg.org/multipage/forms.html#category-label]. If you really need to label a <div />, you can use aria-label or aria-labelledby instead.

<div>


<div>


<label
for="div1"
>
Label 1
</label>


<div
id="div1"
>

Hello

</div>


</div>


</div>"
`)
})

test('multiple labels with non-labellable elements', () => {
const {getAllByLabelText, queryAllByLabelText} = render(`
<div>
<label for="span1">Label 1</label>
<span id="span1">
Hello
</span>
<label for="p1">Label 2</label>
<p id="p1">
World
</p>
</div>
`)

expect(queryAllByLabelText(/Label/)).toEqual([])
expect(() => getAllByLabelText(/Label/)).toThrowErrorMatchingInlineSnapshot(`
"Found a label with the text of: /Label/, however the element associated with this label (<span />) is non-labellable [https://html.spec.whatwg.org/multipage/forms.html#category-label]. If you really need to label a <span />, you can use aria-label or aria-labelledby instead.

Found a label with the text of: /Label/, however the element associated with this label (<p />) is non-labellable [https://html.spec.whatwg.org/multipage/forms.html#category-label]. If you really need to label a <p />, you can use aria-label or aria-labelledby instead.

<div>


<div>


<label
for="span1"
>
Label 1
</label>


<span
id="span1"
>

Hello

</span>


<label
for="p1"
>
Label 2
</label>


<p
id="p1"
>

World

</p>


</div>


</div>"
`)
})

test('totally empty label', () => {
const {getByLabelText, queryByLabelText} = render(`<label />`)
expect(queryByLabelText('')).toBeNull()
Expand Down
35 changes: 31 additions & 4 deletions src/queries/label-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,27 @@ const getAllByLabelText = (container, text, ...rest) => {
if (!els.length) {
const labels = queryAllLabelsByText(container, text, ...rest)
if (labels.length) {
throw getConfig().getElementError(
`Found a label with the text of: ${text}, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly.`,
container,
)
const tagNames = labels
.map(label =>
getTagNameOfElementAssociatedWithLabelViaFor(container, label),
)
.filter(tagName => !!tagName)
if (tagNames.length) {
throw getConfig().getElementError(
tagNames
.map(
tagName =>
`Found a label with the text of: ${text}, however the element associated with this label (<${tagName} />) is non-labellable [https://html.spec.whatwg.org/multipage/forms.html#category-label]. If you really need to label a <${tagName} />, you can use aria-label or aria-labelledby instead.`,
)
.join('\n\n'),
container,
)
} else {
throw getConfig().getElementError(
`Found a label with the text of: ${text}, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly.`,
container,
)
}
} else {
throw getConfig().getElementError(
`Unable to find a label with the text of: ${text}`,
Expand All @@ -163,6 +180,16 @@ const getAllByLabelText = (container, text, ...rest) => {
return els
}

function getTagNameOfElementAssociatedWithLabelViaFor(container, label) {
const htmlFor = label.getAttribute('for')
if (!htmlFor) {
return null
}

const element = container.querySelector(`[id="${htmlFor}"]`)
return element ? element.tagName.toLowerCase() : null
}

// the reason mentioned above is the same reason we're not using buildQueries
const getMultipleError = (c, text) =>
`Found multiple elements with the text of: ${text}`
Expand Down