@testing-library/dom version: 7.22.2// component
export default () => <div title="WrongTitle">HelloWorld</div>;
// test
const wantTitle = "HelloWorld";
const { getByTitle } = render(<Hello />);
// I would expect this to fail here
expect(getByTitle(wantTitle)).toBeInTheDocument();
// ...but it fails here
expect(getByTitle(wantTitle).title).toBe(wantTitle);
I have a React component with some text, and a title attribute that matches that text. I was modifying the component (and tests) to have a different title (but the same text) when I disabled it.
I started getting unexpected behavior from my tests because they were expecting the title to have changed, and getByTitle was finding the element text.
Maybe I'm missing something, but it seems very unintuitive that this line:
expect(getByTitle(wantTitle).title).toBe(wantTitle)
can fail on the toBe. If it's going to fail, shouldn't it fail on the getByTitle?
Looking at queries/title.js, I see this part:
return Array.from(container.querySelectorAll('[title], svg > title')).filter(
node =>
matcher(node.getAttribute('title'), node, text, matchNormalizer) ||
matcher(getNodeText(node), node, text, matchNormalizer),
)
I assume the matcher(getNodeText(node) part is intended for svg title elements, which makes sense, but it looks to me like this will find the text for any element. Forcing that second condition to only be true for title elements would solve it, I think.
Hi @awesomeunleashed,
You found a bug! 馃悰 Thanks. I think that you're right, we should probably scope that second part to only apply to svg > title elements. Would you like to work on fixing this?
I can certainly make an attempt at it when I get the time.
I can give it a try if you guys need help.
Anyone is welcome to work on this. Just post in here when you start working on it so we don't have multiple people working on it at once :)
I did the initial implementation, and I got a PR ready to go on this.
:tada: This issue has been resolved in version 7.22.6 :tada:
The release is available on:
npm package (@latest dist-tag)Your semantic-release bot :package::rocket:
Thanks folks, looks like it works as expected now!
Most helpful comment
I did the initial implementation, and I got a PR ready to go on this.