Skip to content
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

Open
RikvdSar opened this issue Aug 13, 2024 · 5 comments
Open

RTL can't find <fieldset> element and it's children with getByRole #1332

RikvdSar opened this issue Aug 13, 2024 · 5 comments

Comments

@RikvdSar
Copy link

RikvdSar commented Aug 13, 2024

  • @testing-library/dom version: 10.4.0
  • Testing Framework and version: testing-library/react: 16.0.0
  • vitest:^1.6.0
  • DOM Environment: jsdom: 24.1.0

Situation

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")):

<fieldset class="..." role="radiogroup" aria-labelledby="..." aria-describedby="...">
   <label class="..." id="...">...</label>
</fieldset>

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):

<fieldset class="..." role="group" aria-labelledby="...">
   <legend class="..." id="...">...</legend>
   <button type="button" aria-expanded="false" aria-controls="...">...</button>
   "More accessible elements in here like heading which cannot be found by role"
</fieldset>

Trying to getByRole the button within the fieldset with getByRole:

expect(screen.getByText("form.privacy.show-extra-info")).toBeInTheDocument();

What happened:

(relevant) test output:

TestingLibraryElementError: Unable to find an accessible element with the role "button" and name "form.privacy.show-extra-info"

Here are the accessible roles:

  --------------------------------------------------
  radiogroup:

  Name "form.frequency.label":
  <fieldset
    aria-describedby="subscribeRadioGroupHelp"
    aria-labelledby="subscribeRadioGroupLabel"
    class="field-container radio-group has-help-text"
    role="radiogroup"
  />

  --------------------------------------------------
  button: _=> this is a button outside the <fieldset> element_

  Name "form.submit.label":
  <button
    aria-live="polite"
    class="button button--large button--default"
    type="submit"
  />

  --------------------------------------------------
        <fieldset
            aria-labelledby="privacyLegend"
            class="ro-form__fieldset"
            role="group"
          >
            <legend
              class="ro-form__legend"
              id="privacyLegend"
            >
              form.privacy.legend
            </legend>
            <div
              class="ro-form__fieldset-contents"
            >
              <div
                class="rich-text avg-block__intro on-gray-background"
              >
                <p>
                  form.privacy.intro.p1
                </p>
                <p>
                  form.privacy.intro.p2
                </p>
                <p>
                  form.privacy.intro.p3
                </p>
              </div>
              <div
                class="accordion ro-form__field"
              >
                <div
                  class="default-accordion-item accordion-item"
                >
                  <h3
                    class="default-accordion-item__header accordion-item__header"
                  >
                    <button
                      aria-controls="accordionPanel:r3:0"
                      aria-expanded="false"
                      class="default-accordion-item__button accordion-item__button"
                      id="accordionItemTitle:r3:0"
                      type="button"
                    >
                      <div
                        class="default-accordion-item__icon accordion-item__icon"
                        data-testid="icon"
                      >
                      </div>
                      <span
                        class="default-accordion-item__button-content"
                      >
                        <span
                          class="default-accordion-item__button-text"
                        >
                          <span
                            class="default-accordion-item__title"
                          >
                            form.privacy.show-extra-info
                          </span>
                        </span>
                      </span>
                    </button>
                  </h3>
@MatanBobi
Copy link
Member

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?

@eianc
Copy link

eianc commented Aug 29, 2024

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 |

@eianc
Copy link

eianc commented Aug 29, 2024

@MatanBobi
Copy link
Member

MatanBobi commented Aug 30, 2024

@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.
screenshot from the ARIA spec showing what an img with no alt text implies

@testing-library testing-library deleted a comment from github-actions bot Aug 30, 2024
@eianc
Copy link

eianc commented Aug 30, 2024

Thank you @MatanBobi that makes sense! I fixed my tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants