`getByTitle` for SVGs canot find `title` if it's not a direct child of `svg`
See original GitHub issue@testing-library/react
version: 11.2.7- Testing Framework and version:
jest
26x || 27x
- DOM Environment: ‘jsdom’
16.x
Relevant code or config:
Source: https://testing-library.com/docs/queries/bytitle/
const Test = () => (
<>
<span title="Delete" id="2"></span>
<svg>
<title>Close</title>
<g><path /></g>
</svg>
</>
);
VS
const Test = () => (
<>
<span title="Delete" id="2"></span>
<svg>
<g>
<path>
<title>Close</title>
</path>
</g>
</svg>
</>
);
What you did:
render(<Test/>);
screen.getByTitle('Close');
What happened:
First Component - ✅ Second Component - ❌
TestingLibraryElementError: Unable to find an element with the title: Close.
Problem description:
Testing library cannot find Close
because it’s not a direct child of svg
.
Suggested solution:
I should be able to find Close
even if it’s not a direct child of svg
.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:8 (5 by maintainers)
Top Results From Across the Web
ByTitle - Testing Library
Returns the element that has the matching title attribute. Will also find a title element within an SVG. <span title ...
Read more ><title> — the SVG accessible name element - MDN Web Docs
If an element can be described by visible text, it is recommended to reference that text with an aria-labelledby attribute rather than using...
Read more >Test correct SVG component renders with jest and react ...
The second test uses findByTitle because if it is not in the document, getBy* makes the test fails directly. Note: to get this...
Read more >Contextually Marking up accessible images and SVGs
Using a title attribute on an image or SVG. ... if not using <use> then the child elements of the inline SVG would...
Read more >SVG icons - W3C Design System
This section specifically covers using inline SVG files as icons. ... This prevents the SVG from expanding to fill the whole page if...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
PRs welcome for https://github.com/testing-library/dom-testing-library/issues/974#issuecomment-858028323
Just guessing here:
getByText("Close", selector: 'title')
and then walk up to the nearest SVG container or graphics element.