Skip to content

Commit fe44799

Browse files
wyzeKent C. Dodds
authored and
Kent C. Dodds
committed
feat(getByTitle): Add ability to select title element of SVG with getByTitle (testing-library#86)
* Add ability to select `title` element of SVG with `getByTitle` Close testing-library#83 * Add assertion with `{ exact: false }`
1 parent 19c2868 commit fe44799

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

.all-contributorsrc

+9
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,15 @@
296296
"contributions": [
297297
"code"
298298
]
299+
},
300+
{
301+
"login": "wyze",
302+
"name": "Neil Kistner",
303+
"avatar_url": "https://avatars1.githubusercontent.com/u/186971?v=4",
304+
"profile": "https://neilkistner.com/",
305+
"contributions": [
306+
"code"
307+
]
299308
}
300309
]
301310
}

README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
[![downloads][downloads-badge]][npmtrends]
1717
[![MIT License][license-badge]][license]
1818

19-
[![All Contributors](https://img.shields.io/badge/all_contributors-29-orange.svg?style=flat-square)](#contributors)
19+
[![All Contributors](https://img.shields.io/badge/all_contributors-30-orange.svg?style=flat-square)](#contributors)
2020
[![PRs Welcome][prs-badge]][prs]
2121
[![Code of Conduct][coc-badge]][coc]
2222

@@ -324,6 +324,13 @@ Returns the element that has the matching `title` attribute.
324324
const deleteElement = getByTitle(container, 'Delete')
325325
```
326326

327+
Will also find a `title` element within an SVG.
328+
329+
```javascript
330+
// <svg> <title>Close</title> <g> <path /> </g> </svg>
331+
const closeElement = getByTitle(container, 'Close')
332+
```
333+
327334
### `getByValue`
328335

329336
```typescript
@@ -917,7 +924,7 @@ Thanks goes to these people ([emoji key][emojis]):
917924
| [<img src="https://avatars1.githubusercontent.com/u/1241511?s=460&v=4" width="100px;"/><br /><sub><b>Anto Aravinth</b></sub>](https://github.com/antoaravinth)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Tests") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/3462296?v=4" width="100px;"/><br /><sub><b>Jonah Moses</b></sub>](https://github.com/JonahMoses)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=JonahMoses "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/4002543?v=4" width="100px;"/><br /><sub><b>Łukasz Gandecki</b></sub>](http://team.thebrain.pro)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Tests") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/498274?v=4" width="100px;"/><br /><sub><b>Ivan Babak</b></sub>](https://sompylasar.github.io)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Asompylasar "Bug reports") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=sompylasar "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=sompylasar "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/4439618?v=4" width="100px;"/><br /><sub><b>Jesse Day</b></sub>](https://github.com/jday3)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=jday3 "Code") | [<img src="https://avatars0.githubusercontent.com/u/15199?v=4" width="100px;"/><br /><sub><b>Ernesto García</b></sub>](http://gnapse.github.io)<br />[💬](#question-gnapse "Answering Questions") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=gnapse "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=gnapse "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/2747424?v=4" width="100px;"/><br /><sub><b>Josef Maxx Blake</b></sub>](http://jomaxx.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Tests") |
918925
| [<img src="https://avatars3.githubusercontent.com/u/725236?v=4" width="100px;"/><br /><sub><b>Alex Cook</b></sub>](https://github.com/alecook)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=alecook "Documentation") [💡](#example-alecook "Examples") | [<img src="https://avatars3.githubusercontent.com/u/10348212?v=4" width="100px;"/><br /><sub><b>Daniel Cook</b></sub>](https://github.com/dfcook)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Tests") | [<img src="https://avatars2.githubusercontent.com/u/21194045?s=400&v=4" width="100px;"/><br /><sub><b>Thomas Chia</b></sub>](https://github.com/thchia)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Athchia "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=thchia "Code") | [<img src="https://avatars1.githubusercontent.com/u/28659384?v=4" width="100px;"/><br /><sub><b>Tim Deschryver</b></sub>](https://github.com/tdeschryver)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Tests") | [<img src="https://avatars3.githubusercontent.com/u/1571667?v=4" width="100px;"/><br /><sub><b>Alex Krolick</b></sub>](https://alexkrolick.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=alexkrolick "Code") | [<img src="https://avatars2.githubusercontent.com/u/2224291?v=4" width="100px;"/><br /><sub><b>Maddi Joyce</b></sub>](http://www.maddijoyce.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=maddijoyce "Code") | [<img src="https://avatars1.githubusercontent.com/u/25429764?v=4" width="100px;"/><br /><sub><b>Peter Kamps</b></sub>](https://github.com/npeterkamps)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Anpeterkamps "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=npeterkamps "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=npeterkamps "Tests") |
919926
| [<img src="https://avatars2.githubusercontent.com/u/21689428?v=4" width="100px;"/><br /><sub><b>Jonathan Stoye</b></sub>](http://jonathanstoye.de)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=JonathanStoye "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/4126644?v=4" width="100px;"/><br /><sub><b>Sanghyeon Lee</b></sub>](https://github.com/yongdamsh)<br />[💡](#example-yongdamsh "Examples") | [<img src="https://avatars3.githubusercontent.com/u/8015514?v=4" width="100px;"/><br /><sub><b>Justice Mba </b></sub>](https://github.com/Dajust)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=Dajust "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=Dajust "Documentation") [🤔](#ideas-Dajust "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/340761?v=4" width="100px;"/><br /><sub><b>Wayne Crouch</b></sub>](https://github.com/wgcrouch)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=wgcrouch "Code") | [<img src="https://avatars1.githubusercontent.com/u/4996462?v=4" width="100px;"/><br /><sub><b>Ben Elliott</b></sub>](http://benjaminelliott.co.uk)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=benelliott "Code") | [<img src="https://avatars3.githubusercontent.com/u/577921?v=4" width="100px;"/><br /><sub><b>Ruben Costa</b></sub>](http://nuances.co)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=rubencosta "Code") | [<img src="https://avatars2.githubusercontent.com/u/4982001?v=4" width="100px;"/><br /><sub><b>Robert Smith</b></sub>](http://rbrtsmith.com/)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Arbrtsmith "Bug reports") [🤔](#ideas-rbrtsmith "Ideas, Planning, & Feedback") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=rbrtsmith "Documentation") |
920-
| [<img src="https://avatars3.githubusercontent.com/u/881986?v=4" width="100px;"/><br /><sub><b>dadamssg</b></sub>](https://github.com/dadamssg)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dadamssg "Code") |
927+
| [<img src="https://avatars3.githubusercontent.com/u/881986?v=4" width="100px;"/><br /><sub><b>dadamssg</b></sub>](https://github.com/dadamssg)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dadamssg "Code") | [<img src="https://avatars1.githubusercontent.com/u/186971?v=4" width="100px;"/><br /><sub><b>Neil Kistner</b></sub>](https://neilkistner.com/)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=wyze "Code") |
921928

922929
<!-- ALL-CONTRIBUTORS-LIST:END -->
923930

src/__tests__/element-queries.js

+17
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,23 @@ test('query/get element by its title', () => {
146146

147147
expect(getByTitle('Delete').id).toEqual('2')
148148
expect(queryByTitle('Delete').id).toEqual('2')
149+
expect(queryByTitle('Del', {exact: false}).id).toEqual('2')
150+
})
151+
152+
test('query/get title element of SVG', () => {
153+
const {getByTitle, queryByTitle} = render(`
154+
<div>
155+
<svg>
156+
<title id="svg-title">Close</title>
157+
<g>
158+
<path />
159+
</g>
160+
</svg>
161+
</div>
162+
`)
163+
164+
expect(getByTitle('Close').id).toEqual('svg-title')
165+
expect(queryByTitle('Close').id).toEqual('svg-title')
149166
})
150167

151168
test('query/get element by its value', () => {

src/queries.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,28 @@ function queryByText(...args) {
8282
return firstResultOrNull(queryAllByText, ...args)
8383
}
8484

85+
function queryAllByTitle(
86+
container,
87+
text,
88+
{exact = true, collapseWhitespace = true, trim = true} = {},
89+
) {
90+
const matcher = exact ? matches : fuzzyMatches
91+
const matchOpts = {collapseWhitespace, trim}
92+
return Array.from(container.querySelectorAll('[title], svg > title')).filter(
93+
node =>
94+
matcher(node.getAttribute('title'), node, text, matchOpts) ||
95+
matcher(getNodeText(node), node, text, matchOpts),
96+
)
97+
}
98+
99+
function queryByTitle(...args) {
100+
return firstResultOrNull(queryAllByTitle, ...args)
101+
}
102+
85103
const queryByPlaceholderText = queryByAttribute.bind(null, 'placeholder')
86104
const queryAllByPlaceholderText = queryAllByAttribute.bind(null, 'placeholder')
87105
const queryByTestId = queryByAttribute.bind(null, 'data-testid')
88106
const queryAllByTestId = queryAllByAttribute.bind(null, 'data-testid')
89-
const queryByTitle = queryByAttribute.bind(null, 'title')
90-
const queryAllByTitle = queryAllByAttribute.bind(null, 'title')
91107
const queryByValue = queryByAttribute.bind(null, 'value')
92108
const queryAllByValue = queryAllByAttribute.bind(null, 'value')
93109

0 commit comments

Comments
 (0)