Skip to content

Unable to test NavDropdown with Jest because it doesn't render menu items in the DOM #5106

@joshuarize

Description

@joshuarize

Describe the bug

If you have a NavDropdown menu, the DOM doesn't render the menu items on first load (NavDropdown.Item) so they aren't available to test. Here is what my menu looks likes:

// Navbar.jsx
<div className="d-flex flex-row">
  <NavDropdown
    title="Dropdown"
    id="nav-dropdown"
    data-testid="profile-navigation"
    onSelect={handleDropdownSelect}
  >
    <div className="d-flex">
      <div className="d-flex flex-column">
        <small className="text-primary font-weight-bold">Member</small>
        <small>Tim Robbins</small>
        <small className="text-muted">@timrobbins</small>
      </div>
    </div>
    <NavDropdown.Item eventKey="home">Home</NavDropdown.Item>
    <NavDropdown.Item eventKey="users">Users</NavDropdown.Item>
  </NavDropdown>
</div>

To Reproduce

Here is what my test looks like:

it('should open dropdown navigation when user clicks on profile image', async () => {
    const { getByTestId, findByText } = render(<Navbar />);
    const profileDropdown = getByTestId('profile-navigation');
    userEvent.click(profileDropdown);
    const menuItem = findByText('Member');
    expect(menuItem).toBeInTheDocument();
  });

Expected behavior

The test should pass if the user clicks on the proper DOM element to render the components in the DOM. If there is no way to make the DOM render with a user click, then there should be a way to pre-render the component with a prop.

Environment (please complete the following information)

MacOS 2018
React-Bootstrap v1.0.0
Jest

Additional context

Is there any way to pre-render the component in the DOM on first page load. I can see this also causing accessibility issues if that doesn't happen.

I don't mean using the show prop either. I think it should exist in the DOM but be hidden on first page load.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions