MCPcopy
hub / github.com/testing-library/dom-testing-library / getAllByLabelText

Function getAllByLabelText

src/queries/label-text.ts:131–165  ·  view source on GitHub ↗
(container, text, ...rest)

Source from the content-addressed store, hash-verified

129// however, we can give a more helpful error message than the generic one,
130// so we're writing this one out by hand.
131const getAllByLabelText: AllByText = (container, text, ...rest) => {
132 const els = queryAllByLabelText(container, text, ...rest)
133 if (!els.length) {
134 const labels = queryAllLabelsByText(container, text, ...rest)
135 if (labels.length) {
136 const tagNames = labels
137 .map(label =>
138 getTagNameOfElementAssociatedWithLabelViaFor(container, label),
139 )
140 .filter(tagName => !!tagName)
141 if (tagNames.length) {
142 throw getConfig().getElementError(
143 tagNames
144 .map(
145 tagName =>
146 `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.`,
147 )
148 .join('\n\n'),
149 container,
150 )
151 } else {
152 throw getConfig().getElementError(
153 `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.`,
154 container,
155 )
156 }
157 } else {
158 throw getConfig().getElementError(
159 `Unable to find a label with the text of: ${text}`,
160 container,
161 )
162 }
163 }
164 return els
165}
166
167function getTagNameOfElementAssociatedWithLabelViaFor(
168 container: Element,

Callers 1

element-queries.jsFile · 0.85

Calls 4

getConfigFunction · 0.90
queryAllByLabelTextFunction · 0.85
queryAllLabelsByTextFunction · 0.85

Tested by

no test coverage detected