简体   繁体   English

使用测试库测试弹出组件以进行反应

[英]Testing popover components with testing-library for react

I'm using I have a button, which when hovered over, displays a Popover menu.我正在使用我有一个按钮,将鼠标悬停在该按钮上时,会显示一个Popover菜单。 I'm trying to test the existence of some of the components of the Popover menu, but it seems like the Popover menu is not being rendered properly:我正在尝试测试Popover菜单的某些组件是否存在,但似乎Popover菜单未正确呈现:

// Displays market menu popover
const marketMenu: JSX.Element = (
  <Menu>
    <Tabs>
      <Tab id={APPLE} title="Apple" />
      <Tab id={ORANGE} title="Orange" />
      <Tab id={BANANA} title="Banana" />
    </Tabs>
  </Menu>
);
return (
  <Popover
    className="market-selector-popover"
    popoverClassName="market-selector-popover"
    content={marketMenu}
  >
    <AnchorButton
      className="market-selector-btn"
      rightIcon="chevron-down"
      text="Fruits"
    />
  </Popover>
);



it("Selects Spot tab and displays it", () => {
    const { container, getByRole } = renderWithProviders(
      <MarketSelector />
    );

    fireEvent.mouseEnter(getByRole("button"));

    const { getByText } = within(
      container.querySelector(".market-selector-popover")
    );
    expect(getByText("Banana")).toBeInTheDocument();
  });

Late reply but I just ran into a similar issue and I fixed it by using findByText .回复晚了,但我刚刚遇到了类似的问题,我使用findByText修复了它。

So in your case I would change the code to:因此,在您的情况下,我会将代码更改为:

it("Selects Spot tab and displays it", async () => {
    const { container, getByText } = renderWithProviders(
      <MarketSelector />
    );

    // Using the text to select like a user would    
    fireEvent.mouseEnter(getByText("Fruits"));
    expect(await findByText("Banana")).toBeInTheDocument();
  });

Using userEvent from @testing-library/user-event and screen使用来自 @testing-library/user-event 和screenuserEvent

it("Selects Spot tab and displays it", async () => {
   renderWithProviders(<MarketSelector />); 
   userEvent.hover(screen.getByText("Fruits"));
   expect(await screen.findByText("Banana")).toBeInTheDocument();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有 react-native 测试库的样式化组件主题 - Styled-components theme with react-native testing-library 在反应测试库中使用 DOM 元素? - Working with DOM elements in react testing-library? @testing-library/react 测试表单 onSubmit - @testing-library/react test form onSubmit 找不到模块'@testing-library/react' - Cannot find module '@testing-library/react' 使用 @testing-library/react 测试 antd 下拉列表 - Test antd dropdown with @testing-library/react testing-library/react 中 toJSON 的等价物是什么? - What is the equivalent of toJSON in testing-library/react? 未输入反应测试库中的 waitFor - waitFor in react testing-library not entered 在 React / testing-library/react 中呈现所有组件后,如何从表中获取元素? - How to get the elements from a table after all components are rendered in React / testing-library/react? 对于异步挂钩,@testing-library/react 中的 function waitForNextUpdate (@testing-library/react-hooks) 有什么替代方案? - What is the alternative the function waitForNextUpdate (@testing-library/react-hooks) in @testing-library/react for async hooks? 使用@testing-library/react 测试材料 ui slider - Testing a material ui slider with @testing-library/react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM