diff --git a/src/__tests__/element-queries.js b/src/__tests__/element-queries.js index 1fa122b3..0880562a 100644 --- a/src/__tests__/element-queries.js +++ b/src/__tests__/element-queries.js @@ -800,3 +800,10 @@ test('get/query textarea element by current value', () => { expect(getByDisplayValue('World').id).toEqual('content-textarea') expect(queryByDisplayValue('World').id).toEqual('content-textarea') }) + +test('can get a textarea with children', () => { + const {getByLabelText} = renderIntoDocument(` + + `) + getByLabelText('Label') +}) diff --git a/src/queries/label-text.js b/src/queries/label-text.js index 2a3aee66..9b9ba908 100644 --- a/src/queries/label-text.js +++ b/src/queries/label-text.js @@ -16,9 +16,17 @@ function queryAllLabelsByText( ) { const matcher = exact ? matches : fuzzyMatches const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer}) - return Array.from(container.querySelectorAll('label')).filter(label => - matcher(label.textContent, label, text, matchNormalizer), - ) + return Array.from(container.querySelectorAll('label')).filter(label => { + let textToMatch = label.textContent + + // The children of a textarea are part of `textContent` as well. We + // need to remove them from the string so we can match it afterwards. + label.querySelectorAll('textarea').forEach(textarea => { + textToMatch = textToMatch.replace(textarea.value, '') + }) + + return matcher(textToMatch, label, text, matchNormalizer) + }) } function queryAllByLabelText(