Skip to content

Commit f9714ed

Browse files
amannnKent C. Dodds
authored and
Kent C. Dodds
committed
feat: support textareas with children. (#344)
1 parent e418614 commit f9714ed

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/__tests__/element-queries.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,3 +800,10 @@ test('get/query textarea element by current value', () => {
800800
expect(getByDisplayValue('World').id).toEqual('content-textarea')
801801
expect(queryByDisplayValue('World').id).toEqual('content-textarea')
802802
})
803+
804+
test('can get a textarea with children', () => {
805+
const {getByLabelText} = renderIntoDocument(`
806+
<label>Label<textarea>Value</textarea></label>
807+
`)
808+
getByLabelText('Label')
809+
})

src/queries/label-text.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@ function queryAllLabelsByText(
1616
) {
1717
const matcher = exact ? matches : fuzzyMatches
1818
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
19-
return Array.from(container.querySelectorAll('label')).filter(label =>
20-
matcher(label.textContent, label, text, matchNormalizer),
21-
)
19+
return Array.from(container.querySelectorAll('label')).filter(label => {
20+
let textToMatch = label.textContent
21+
22+
// The children of a textarea are part of `textContent` as well. We
23+
// need to remove them from the string so we can match it afterwards.
24+
label.querySelectorAll('textarea').forEach(textarea => {
25+
textToMatch = textToMatch.replace(textarea.value, '')
26+
})
27+
28+
return matcher(textToMatch, label, text, matchNormalizer)
29+
})
2230
}
2331

2432
function queryAllByLabelText(

0 commit comments

Comments
 (0)