如何使用React测试库测试React Bootstrap NavDropdown

cu6pst1q  于 2022-10-22  发布在  React
关注(0)|答案(1)|浏览(158)

我正在尝试使用test-id属性测试React Bootstrap NavDropdown元素,但React测试库找不到数据test属性。
我的导航.tsx

  1. import Nav from 'react-bootstrap/Nav';
  2. import Navbar from 'react-bootstrap/Navbar';
  3. import NavDropdown from 'react-bootstrap/NavDropdown';
  4. import LOGO from './logo.svg';
  5. const MyNav = (): JSX.Element => (
  6. <Navbar collapseOnSelect expand="lg" bg="dark" variant="dark">
  7. <Navbar.Brand href="/">
  8. <img src={LOGO} width="30" height="30" alt="React Bootstrap logo" />
  9. BBS
  10. </Navbar.Brand>
  11. <Navbar.Toggle aria-controls="responsive-navbar-nav" />
  12. <Navbar.Collapse id="responsive-navbar-nav">
  13. <Nav className="mr-auto">
  14. <NavDropdown
  15. title="English"
  16. id="navbarScrollingDropdown"
  17. data-testid="en-boards"
  18. >
  19. <NavDropdown.Item href="#en-news" data-testid="en-news">
  20. News
  21. </NavDropdown.Item>
  22. <NavDropdown.Item href="#en-sensitive-may">
  23. Politics
  24. </NavDropdown.Item>
  25. </NavDropdown>
  26. <NavDropdown title="日本語" id="navbarScrollingDropdown">
  27. <NavDropdown.Item href="#ja-news">ニュース</NavDropdown.Item>
  28. <NavDropdown.Item href="#ja-sensitive-may">政治</NavDropdown.Item>
  29. </NavDropdown>
  30. </Nav>
  31. </Navbar.Collapse>
  32. </Navbar>
  33. );
  34. export default MyNav;

我的导航测试.tsx

  1. import React from 'react';
  2. import { render, screen, within } from '@testing-library/react';
  3. import userEvent from '@testing-library/user-event';
  4. import MyNav from './MyNav';
  5. test('renders nav', async () => {
  6. const user = userEvent.setup();
  7. render(<MyNav />);
  8. const navBarBrandEl = screen.getByText('BBS');
  9. const navDropDownElEn = screen.getByText('English');
  10. const navDropDownElJa = screen.getByText('日本語');
  11. expect(navBarBrandEl).toBeInTheDocument();
  12. expect(navDropDownElEn).toBeInTheDocument();
  13. expect(navDropDownElJa).toBeInTheDocument();
  14. const enBoards = screen.getByTestId('en-boards');
  15. await user.click(enBoards);
  16. const enNews = within(enBoards).getByTestId('en-news');
  17. expect(enNews).toBeInTheDocument();
  18. });

测试结果为

  1. TestingLibraryElementError: Unable to find an element by: [data-testid="en-news"]
  2. Ignored nodes: comments, script, style
  3. <div
  4. class="nav-item dropdown"
  5. data-testid="en-boards"
  6. >
  7. <a
  8. aria-expanded="false"
  9. class="dropdown-toggle nav-link"
  10. href="#"
  11. id="navbarScrollingDropdown"
  12. role="button"
  13. tabindex="0"
  14. >
  15. English
  16. </a>
  17. </div>
  18. 18 | const enBoards = screen.getByTestId('en-boards');
  19. 19 | await user.click(enBoards);
  20. > 20 | const enNews = within(enBoards).getByTestId('en-news');
  21. | ^
  22. 21 |
  23. 22 | expect(enNews).toBeInTheDocument();
  24. 23 | });
  25. at Object.getElementError (node_modules/@testing-library/dom/dist/config.js:40:19)
  26. at node_modules/@testing-library/dom/dist/query-helpers.js:90:38
  27. at node_modules/@testing-library/dom/dist/query-helpers.js:62:17
  28. at getByTestId (node_modules/@testing-library/dom/dist/query-helpers.js:111:19)
  29. at Object.<anonymous> (src/MyNav.test.tsx:20:35)

环境

  • macOS 12.5
  • 类型脚本4.8.4
  • React18.2.0
  • React引导2.5.0
  • 测试库/React13.4.0

我尝试过这个issue的解决方案,但没有成功。谢谢你阅读。有人能解决吗?

ctzwtxfj

ctzwtxfj1#

您应该单击enBoards内部的按钮,而不是enBoard本身。
这是更正后的片段。

  1. import React from 'react';
  2. import { render, screen, within } from '@testing-library/react';
  3. import userEvent from '@testing-library/user-event';
  4. import MyNav from './MyNav';
  5. test('renders nav', async () => {
  6. render(<MyNav />);
  7. const navBarBrandEl = screen.getByText('BBS');
  8. const navDropDownElEn = screen.getByText('English');
  9. const navDropDownElJa = screen.getByText('日本語');
  10. expect(navBarBrandEl).toBeInTheDocument();
  11. expect(navDropDownElEn).toBeInTheDocument();
  12. expect(navDropDownElJa).toBeInTheDocument();
  13. const enBoards = screen.getByTestId('en-boards');
  14. const enBoardsButton = within(enBoards).getByRole('button');
  15. await userEvent.click(enBoardsButton);
  16. const enNews = within(enBoards).getByTestId('en-news');
  17. expect(enNews).toBeInTheDocument();
  18. });
展开查看全部

相关问题