I am trying to test that when I hover over an icon, a tooltip appears with certain text.
Using Jest, react testing library, and the tooltip is a MUI 4 component.
The icon and tooltip
<div
data-testid="tooltip-icon"
className={classes.infoTooltipIcon}
>
<Tooltip
data-testid="tooltip-text"
title="Tooltip text"
>
<InfoIcon/>
</Tooltip>
</div>
Slimmed down test
it('has a tooltip on the info icon', () => {
render(<Component />);
const infoIcon = screen.getByTestId('tooltip-icon');
userEvent.hover(infoIcon); // works
const infoTooltip = screen.getByTestId('tooltip-text')
expect(infoTooltip.title).toEqual('Tooltip text);
});
Currently, this results in
actual: undefined
expected: 'Tooltip text'
If I screen.debug(infoTooltip) the title is there and has the correct text, but its an <svg/> could this cause an issue?
Output of screen.debug(infoTooltip)
<svg
aria-hidden="true"
class="MuiSvgIcon-root makeStyles-center-52"
data-testid="tooltip-text"
focusable="false"
title="Tooltip text"
viewBox="0 0 24 24"
>
<path
d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-6h2v6zm0-8h-2V7h2v2z"
/>
</svg>
Additionally, I have also tried screen.getByTitle('Tooltip text') and it does not work.
queryByTestIdrather thangetByTestId?actual: undefined, I have posted the output ofscreen.debug()