From 0a4ba3d7953ecf3def4b9c14748aa6aaa0831ec4 Mon Sep 17 00:00:00 2001 From: Raphael Costa Date: Wed, 12 Feb 2020 13:23:06 -0400 Subject: [PATCH] Fixed ByLabel queries to work with selects --- src/__tests__/element-queries.js | 13 +++++++++++++ src/queries/label-text.js | 6 ++++++ 2 files changed, 19 insertions(+) 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) }) }