testing-library / dom-testing-library

🐙 Simple and complete DOM testing utilities that encourage good testing practices.
https://testing-library.com/dom
MIT License
3.28k stars 466 forks source link

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

Open RikvdSar opened 3 months ago

RikvdSar commented 3 months ago

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 commented 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?

eianc commented 2 months ago

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 commented 2 months ago

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

MatanBobi commented 2 months ago

@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
eianc commented 2 months ago

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