Skip to content

Commit a3c5699

Browse files
authored
fix(ByLabel): make it work with selects in labels (#451)
1 parent a98dc9f commit a3c5699

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/__tests__/element-queries.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,3 +830,16 @@ test('can get a textarea with children', () => {
830830
`)
831831
getByLabelText('Label')
832832
})
833+
834+
test('can get a select with options', () => {
835+
const {getByLabelText} = renderIntoDocument(`
836+
<label>
837+
Label
838+
<select>
839+
<option>Some</option>
840+
<option>Options</option>
841+
</select>
842+
</label>
843+
`)
844+
getByLabelText('Label')
845+
})

src/queries/label-text.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ function queryAllLabelsByText(
2525
textToMatch = textToMatch.replace(textarea.value, '')
2626
})
2727

28+
// The children of a select are also part of `textContent`, so we
29+
// need also to remove their text.
30+
Array.from(label.querySelectorAll('select')).forEach(select => {
31+
textToMatch = textToMatch.replace(select.textContent, '')
32+
})
33+
2834
return matcher(textToMatch, label, text, matchNormalizer)
2935
})
3036
}

0 commit comments

Comments
 (0)