Skip to content

Commit 881820b

Browse files
author
Bianca Del Carretto
committed
fix: getByLabelText does not get multiple concatenated labels(#545)
1 parent c530eaa commit 881820b

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

src/__tests__/element-queries.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ test('can get form controls by label text', () => {
185185
expect(getByLabelText('6th one').id).toBe('sixth-id')
186186
expect(getByLabelText('6th two').id).toBe('sixth-id')
187187
expect(getByLabelText('6th three').id).toBe('sixth-id')
188+
expect(getByLabelText('6th one 6th two 6th three', {concat: true}).id).toBe(
189+
'sixth-id',
190+
)
188191
})
189192

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

src/matches.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ function fuzzyMatches(textToMatch, node, matcher, normalizer) {
1313
}
1414
}
1515

16-
function matches(textToMatch, node, matcher, normalizer) {
16+
function matches(textToMatch, node, matcher, normalizer, concat = false) {
1717
if (typeof textToMatch !== 'string') {
1818
return false
1919
}
2020

2121
const normalizedText = normalizer(textToMatch)
2222
if (typeof matcher === 'string') {
23-
return normalizedText === matcher
23+
return concat
24+
? matcher.toLowerCase().includes(normalizedText.toLowerCase())
25+
: normalizedText === matcher
2426
} else if (typeof matcher === 'function') {
2527
return matcher(normalizedText, node)
2628
} else {

src/queries/label-text.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {queryAllByText} from './text'
1414
function queryAllLabelsByText(
1515
container,
1616
text,
17-
{exact = true, trim, collapseWhitespace, normalizer} = {},
17+
{exact = true, trim, collapseWhitespace, normalizer, concat = false} = {},
1818
) {
1919
const matcher = exact ? matches : fuzzyMatches
2020
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
@@ -36,20 +36,39 @@ function queryAllLabelsByText(
3636
textToMatch = textToMatch.replace(select.textContent, '')
3737
})
3838

39-
return matcher(textToMatch, node, text, matchNormalizer)
39+
return matcher(textToMatch, node, text, matchNormalizer, concat)
4040
})
4141
}
4242

4343
function queryAllByLabelText(
4444
container,
4545
text,
46-
{selector = '*', exact = true, collapseWhitespace, trim, normalizer} = {},
46+
{
47+
selector = '*',
48+
exact = true,
49+
collapseWhitespace,
50+
trim,
51+
normalizer,
52+
concat = false,
53+
} = {},
4754
) {
4855
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
4956
const labels = queryAllLabelsByText(container, text, {
5057
exact,
5158
normalizer: matchNormalizer,
59+
concat,
5260
})
61+
if (concat) {
62+
const labelsValues = labels.map(label => label.textContent || label.value)
63+
if (
64+
labelsValues.reduce(
65+
(agg, label) => agg.replace(`${label} `, ''),
66+
`${text} `,
67+
) !== ''
68+
) {
69+
return []
70+
}
71+
}
5372
const labelledElements = labels
5473
.reduce((matchedElements, label) => {
5574
const elementsForLabel = []

0 commit comments

Comments
 (0)