Open RikvdSar opened 3 months ago
Hi @RikvdSar.
This current issue doesn't have enough details for us to try and debug it, also it looks like you wrote getByRole
but in the example you've used getByText
.
Can you please create a minimal reproduction using https://testing-library.com/new-dtl so we'll be able to help?
I got a similar issue with the link
and img
roles. If I explicitly add the role="link"
to the <a>
tag then the tests pass.
This started to happen when we used any versions of @testing-library/dom
>= 10.0.0
, with version 9.3.4
everything works as expected.
The libraries that I'm using:
"@testing-library/dom": "10.0.0",
"@testing-library/jest-dom": "6.5.0",
"@testing-library/react": "16.0.0",
Below is an example where it can't find the img
in the test
My React component
import React, { forwardRef } from 'react';
import * as styles from './styles';
type TextIconVariant = 'leading' | 'trailing';
export interface TextIconProps {
children: React.ReactNode;
icon: string;
iconHeight: number;
iconWidth: number;
onClick?: (e: React.MouseEvent<HTMLButtonElement>) => void;
variant?: TextIconVariant;
}
export const TextIcon = forwardRef<HTMLButtonElement | null, TextIconProps>(
(
{ children, icon, iconHeight, iconWidth, variant = 'trailing', ...buttonProps }: TextIconProps,
forwardedRef,
) => (
<button css={styles.iconButton} data-variant={variant} ref={forwardedRef} {...buttonProps}>
{children}
<img alt="" height={iconHeight} src={icon} width={iconWidth} />
</button>
),
);
and the test file:
import React from 'react';
import { render, screen } from '@testing-library/react';
import { TextIcon } from './TextIcon';
test('TextIcon renders a button containing an icon and child elements', () => {
const text = 'title';
render(
<TextIcon icon="icon" iconHeight={25} iconWidth={20} onClick={jest.fn()}>
{text}
</TextIcon>,
);
expect(screen.getByRole('button', { name: text })).toBeInTheDocument();
expect(screen.getByRole('img')).toBeInTheDocument();
});
The error I'm getting is:
TestingLibraryElementError: Unable to find an accessible element with the role "img"
Here are the accessible roles:
button:
Name "title":
<button
css="
cursor: inherit;
padding: 0;
border: none;
background: none;
color: black;
border-radius: 0;
&:hover {
.,button__content, {
color: inherit;
}
}
.,button__content, {
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
&::after {
display: none;
}
}
&[data-variant='leading'] {
.,button__content, {
flex-direction: row-reverse;
}
}
"
data-variant="trailing"
/>
--------------------------------------------------
presentation:
Name "":
<img
alt=""
height="25"
src="icon"
width="20"
/>
--------------------------------------------------
Ignored nodes: comments, script, style
<body>
<div>
<button
css="
cursor: inherit;
padding: 0;
border: none;
background: none;
color: black;
border-radius: 0;
&:hover {
.,button__content, {
color: inherit;
}
}
.,button__content, {
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
&::after {
display: none;
}
}
&[data-variant='leading'] {
.,button__content, {
flex-direction: row-reverse;
}
}
"
data-variant="trailing"
>
title
<img
alt=""
height="25"
src="icon"
width="20"
/>
</button>
</div>
</body>
14 |
15 | expect(screen.getByRole('button', { name: text })).toBeInTheDocument();
> 16 | expect(screen.getByRole('img')).toBeInTheDocument();
| ^
17 | });
18 |
Do I need to look for role presentation
because the image is a descendent of the button?
https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/button_role#all_descendants_are_presentational:~:text=There%20are%20some,support%20semantic%20children
@eianc, if I understand correctly, your issue is different and is related to a change in the ARIA spec.
The spec dictates that an empty alt will imply role "none" or "presentation". If you remove the alt
attribute, it will get role img
.
Thank you @MatanBobi that makes sense! I fixed my tests
@testing-library/dom
version: 10.4.0Situation
RTL can't find the second fieldset element and can't find any accessible roles within that fieldset element. It CAN find all element with getByText. The HTML for the first fieldset elements looks like this (CAN find this element with getByRole("radiogroup")):
The HTML for the second (sibling) fieldset elements looks like this (can't find this element with getByRole("group"), but can find it with getByText):
Trying to getByRole the button within the fieldset with getByRole:
expect(screen.getByText("form.privacy.show-extra-info")).toBeInTheDocument();
What happened:
(relevant) test output: