Skip to content

Commit c530eaa

Browse files
author
Bianca Del Carretto
committed
fix: getByLabelText ignores input labels (#545)
1 parent af7392f commit c530eaa

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/__tests__/element-queries.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ test('can get form controls by label text', () => {
168168
<label id="fifth-label-two">5th two</label>
169169
<input aria-labelledby="fifth-label-one fifth-label-two" id="fifth-id" />
170170
</div>
171+
<div>
172+
<label id="sixth-label-one">6th one</label>
173+
<label id="sixth-label-two">6th two</label>
174+
<input id="sixth-label-three" type="text" value="6th three"/>
175+
<input aria-labelledby="sixth-label-one sixth-label-two sixth-label-three" id="sixth-id" />
176+
</div>
171177
</div>
172178
`)
173179
expect(getByLabelText('1st').id).toBe('first-id')
@@ -176,6 +182,9 @@ test('can get form controls by label text', () => {
176182
expect(getByLabelText('4th').id).toBe('fourth.id')
177183
expect(getByLabelText('5th one').id).toBe('fifth-id')
178184
expect(getByLabelText('5th two').id).toBe('fifth-id')
185+
expect(getByLabelText('6th one').id).toBe('sixth-id')
186+
expect(getByLabelText('6th two').id).toBe('sixth-id')
187+
expect(getByLabelText('6th three').id).toBe('sixth-id')
179188
})
180189

181190
test('can get elements labelled with aria-labelledby attribute', () => {

src/queries/label-text.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,25 @@ function queryAllLabelsByText(
1818
) {
1919
const matcher = exact ? matches : fuzzyMatches
2020
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
21-
return Array.from(container.querySelectorAll('label')).filter(label => {
22-
let textToMatch = label.textContent
21+
return Array.from(container.querySelectorAll('label,input')).filter(node => {
22+
let textToMatch =
23+
node.tagName.toLowerCase() === 'label'
24+
? node.textContent
25+
: node.value || null
2326

2427
// The children of a textarea are part of `textContent` as well. We
2528
// need to remove them from the string so we can match it afterwards.
26-
Array.from(label.querySelectorAll('textarea')).forEach(textarea => {
29+
Array.from(node.querySelectorAll('textarea')).forEach(textarea => {
2730
textToMatch = textToMatch.replace(textarea.value, '')
2831
})
2932

3033
// The children of a select are also part of `textContent`, so we
3134
// need also to remove their text.
32-
Array.from(label.querySelectorAll('select')).forEach(select => {
35+
Array.from(node.querySelectorAll('select')).forEach(select => {
3336
textToMatch = textToMatch.replace(select.textContent, '')
3437
})
3538

36-
return matcher(textToMatch, label, text, matchNormalizer)
39+
return matcher(textToMatch, node, text, matchNormalizer)
3740
})
3841
}
3942

0 commit comments

Comments
 (0)