-
Notifications
You must be signed in to change notification settings - Fork 466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RTL can't find <fieldset> element and it's children with getByRole #1332
Comments
Hi @RikvdSar. |
I got a similar issue with the 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 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 |
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:
The text was updated successfully, but these errors were encountered: