question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

<li> is not an accessible element

See original GitHub issue
  • @testing-library/dom version: 7.22.0
  • Testing Framework and version: jest version 24.9.0
  • DOM Environment: jsdom version 11.12.0

Relevant code or config:

// spinnerButton.test.js
import React from 'react';
import { render, screen } from '@testing-library/react';
import SpinnerButton from '../spinnerButton.js';

test("renders a spinner when it's loading", () => {
    // render the button
    render(<SpinnerButton text={''} waiting={true} />);

    // expect a spinner to exist
    expect(screen.getByRole('status')).toBeInTheDocument();
});
// spinnerButton.js
import React from 'react';
import Spinner from 'react-bootstrap/Spinner';
import './styles/spinnerButton.css';

class SpinnerButton extends React.Component {
    render = () => {
        const buttonProps = { ...this.props };
        delete buttonProps.waiting;
        delete buttonProps.text;
        delete buttonProps.spinnerClassName;
        return (
            <button {...buttonProps}>
                {this.props.waiting ? (
                    <Spinner
                        as='li'
                        size='sm'
                        animation='border'
                        role='status'                     /* <= RELEVANT LINE OF CODE */
                        className={this.props.spinnerClassName}
                        aria-hidden='true'
                    >
                        <span></span>
                    </Spinner>
                ) : (
                    this.props.text
                )}
            </button>
        );
    };
}

export default SpinnerButton;

What you did:

I have a React component, SpinnerButton that shows some text before it has been clicked and a spinning wheel after it has been clicked. I gave a DOM node in that button a role called 'status'.

Then I tried to create a test that would find that element by its role, but it cannot be found.

What happened:

The test output looks like this:

 renders a spinner when it's loading

    TestingLibraryElementError: Unable to find an accessible element with the role "status"

    Here are the accessible roles:

      button:

      Name "":
      <button />

      --------------------------------------------------

    <body>
      <div>
        <button>
          <li
            aria-hidden="true"
            class="spinner-border spinner-border-sm"
            role="status"
          >
            <span />
          </li>
        </button>
      </div>
    </body>

      29 |
      30 |     // expect a spinner to exist
    > 31 |     expect(screen.getByRole('status')).toBeInTheDocument();
         |                   ^
      32 | });
      33 |

So I know that the <li> element is there and that it has role="status" set.

The question is, why is “unable to find an accessible element?” The element is there and it has the role.

Problem description:

I want to find the node that I labelled, but I cannot.

Suggested solution:

It is possible (heck, probable) that I am misinterpreting what it means to be an accessible element or what getByRole is supposed to do. In that case, some more clear documentation or error message would be good.

But if I interpreted it correctly, then it should be the case that this element can be found by its role because it has a role.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
winterlamoncommented, Aug 19, 2020

@nullromo What happens when you use expect(screen.getByRole('status', { hidden: true })).toBeInTheDocument()?

By default, getByRole will not check queries that are excluded from the accessibility tree, so not specifying the hidden option is likely skipping your spinner because you have aria-hidden="true".

1reaction
eps1loncommented, Aug 20, 2020

For loading spinners there’s quite a bit of helpful ressources out there but no concrete recommendation in the ARIA authoring practices.

I can’t give you a recommendation since I didn’t test any of those patterns.

Either way this issue tracker is for bug/feature tracking. For evaluating a11y patterns you should start curating a list of trustworthy resources or test it yourself. I hope that helps a bit.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Li elements not accessible - javascript - Stack Overflow
And here is my code. It only outputs color and size, without the values. Please help. t.desc= e.querySelector(".n_ck_s_itemoption_list") ...
Read more >
Accessibility rule: <ul> and <ol> must only directly contain <li ...
When content elements other than list items are contained within a set of list elements, screen readers are unable to indicate to the...
Read more >
<ul> and <ol> must only directly contain <li>, <script> or ...
Ensure all ordered and unordered lists (defined by ul or ol elements) contain only li content elements. Here is a list, using proper...
Read more >
ARIA: list role - Accessibility - MDN Web Docs
So, the <li> elements are not exposed to assistive technologies, but elements contained inside of those <li> elements, including nested ...
Read more >
"List items (<li>) are not contained within <ul> or <ol> parent ...
What happens if you put role=presentation on the li element? That should essentially remove it from the accessibility tree. By manually ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found