Skip to content

Commit 388db21

Browse files
authored
fix: getByTitle returning element matching textContent instead of title (#753)
Co-authored-by: Kent C. Dodds <me+github@kentcdodds.com> Closes #741
1 parent 6d6641d commit 388db21

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/__tests__/element-queries.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,12 +551,14 @@ test('query/get element by its title', () => {
551551
<span title="Ignore this" id="1"/>
552552
<span title="Delete" id="2"/>
553553
<span title="Ignore this as well" id="3"/>
554+
<div title="WrongTitle" id="4">HelloWorld</div>
554555
</div>
555556
`)
556557

557558
expect(getByTitle('Delete').id).toEqual('2')
558559
expect(queryByTitle('Delete').id).toEqual('2')
559560
expect(queryByTitle('Del', {exact: false}).id).toEqual('2')
561+
expect(queryByTitle("HelloWorld")).toBeNull()
560562
})
561563

562564
test('query/get title element of SVG', () => {

src/queries/title.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import {
88
buildQueries,
99
} from './all-utils'
1010

11+
const isSvgTitle = node =>
12+
node.tagName.toLowerCase() === 'title' &&
13+
node.parentElement?.tagName.toLowerCase() === 'svg'
14+
1115
function queryAllByTitle(
1216
container,
1317
text,
@@ -19,7 +23,8 @@ function queryAllByTitle(
1923
return Array.from(container.querySelectorAll('[title], svg > title')).filter(
2024
node =>
2125
matcher(node.getAttribute('title'), node, text, matchNormalizer) ||
22-
matcher(getNodeText(node), node, text, matchNormalizer),
26+
(isSvgTitle(node) &&
27+
matcher(getNodeText(node), node, text, matchNormalizer)),
2328
)
2429
}
2530

0 commit comments

Comments
 (0)