test-library to verify aria-expanded value as false
Asked Answered
D

1

5

I have one component render a flyout as following HTML code once flyout is opened. I would like to write the test using test-library to verify the aria-expanded value is false after clicking the flyoutItem. is there any better way for me to verify the aria-expanded?

// component
    <body>
      <div>
        <div >
          <button aria-expanded="true" class="btn">
            Flyout button
          </button>
          <div class="flyoutContainer>
            <ul class="flyoutItem" role="listbox" >
              <li aria-selected="false" class="item" role="option" >
                <div class="option" >
                    <span >Data is here</span>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </body>

// test

render(<flyout />);

const item = screen.getByText(/Data is here/, {selector: 'span'});
fireEvent.click(item);

const flyoutButton = screen.getByRole('button', {name: 'Flyout button'});
expect(flyoutButton).toHaveClass('aria-expanded').;
expect(flyoutButton).toHaveAttribute('aria-expanded', 'false')
Dizen answered 21/9, 2021 at 1:8 Comment(1)
Unless you are dynamically adding a custom class named aria-expanded and you want to test that is being added correctly, the first assertion (...toHaveClass) wouldn't be necessary. Other than that, I don't see the problem. Are you getting an error?Encincture
D
6

You could use getByRole() option expanded: false like this

expect(
    screen.getByRole("button", {
        name: "Flyout button",
        expanded: false,
    })
).toBeInTheDocument();
Dexamyl answered 19/5, 2022 at 11:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.