diff --git a/src/__tests__/element-queries.js b/src/__tests__/element-queries.js index f673b918..2b2a604b 100644 --- a/src/__tests__/element-queries.js +++ b/src/__tests__/element-queries.js @@ -830,3 +830,16 @@ test('can get a textarea with children', () => { `) getByLabelText('Label') }) + +test('can get a select with options', () => { + const {getByLabelText} = renderIntoDocument(` + + `) + getByLabelText('Label') +}) diff --git a/src/queries/label-text.js b/src/queries/label-text.js index 3669db20..0351c5e7 100644 --- a/src/queries/label-text.js +++ b/src/queries/label-text.js @@ -25,6 +25,12 @@ function queryAllLabelsByText( textToMatch = textToMatch.replace(textarea.value, '') }) + // The children of a select are also part of `textContent`, so we + // need also to remove their text. + Array.from(label.querySelectorAll('select')).forEach(select => { + textToMatch = textToMatch.replace(select.textContent, '') + }) + return matcher(textToMatch, label, text, matchNormalizer) }) }