我想测试工具提示标题是否等于特定文本。这是我的工具提示,我想为其编写测试:
<Tooltip
title={
this.props.connection ? "Connected" : "Disconnected (Try again)"
}>
<Badge status="default" data-testid="connection-sign" />
</Tooltip>
这是我开玩笑的测试:
test("Show error title in tooltip", async () => {
baseDom = render(cardComponent);
fireEvent.mouseMove(await baseDom.findByTestId("connection-sign")); //To hover element and show tooltip
expect(
baseDom.getByTitle(
"Disconnected (Try again)"
)
).toBeInTheDocument();
});
但是这个测试失败了,无法找到具有此标题的元素。如何测试我的工具提示是否包含“已断开连接(重试)”?
您的测试中有多个错误。
render
// this is wrong, passing component type
baseDom = render(cardComponent);
// this is right, passing component instance created with JSX
baseDom = render(<cardComponent />);
使用
mouseMove
代替 mouseOver
事件
按标题搜索元素并传递文本,而不是按文本搜索
// wrong, because, the prop on the Tooltip is called 'title'
// but it is actually text as 'getByTitle' looks for HTML
// title attribute
baseDom.getByTitle("Disconnected (Try again)");
// right, because you are actually looking for text not
// HTML title attribute (but wrong see (4))
baseDom.getByText("Disconnected (Try again)");
// wrong, because, Tooltip is added dynamically to the DOM
baseDom.getByText("Disconnected (Try again)");
// right
await baseDom.findByText("Disconnected (Try again)");
总而言之,修正所有错误后,测试应该如下所示:
import React from "react";
import { render, fireEvent } from "@testing-library/react";
import App from "./App";
test("Show error title in tooltip", async () => {
const baseDom = render(<cardComponent />);
fireEvent.mouseOver(baseDom.getByTestId("connection-sign"));
expect(
await baseDom.findByText("Disconnected (Try again)")
).toBeInTheDocument();
});
我尝试了很多方法但没有成功,所以我尝试了 鼠标输入而不是mouseOver或mouseMove,它对我有用。 这是测试工具提示内容的解决方案,如下所示:
import { render, cleanup, waitFor, fireEvent, screen } from '@testing-library/react';
// Timeline component should render correctly with tool-tip
test('renders TimeLine component with mouse over(tool-tip)', async () => {
const { getByTestId, getByText, getByRole } = render(
<TimeLine
items={timeLineItems()}
currentItem={currentTimeLineItem()}
onTimeLineItemChange={() => {}}
/>
);
const courseTitle = "Course collection-3";
fireEvent.mouseEnter(getByRole('button'));
await waitFor(() => getByText(courseTitle));
expect(screen.getByText(courseTitle)).toBeInTheDocument();
});
除了接受的答案之外,重要的是要确保您是否为
getPopupContainer
工具提示设置了 Antd
属性,弹出窗口可能对反应测试库不可见,就像我的情况一样,因为 DOM 容器集可能会测试组件时,尤其是单元测试时,在主体中不可用。例如
就我而言,我有
<Popover
getPopupContainer={() => document.getElementById('post--component-drawer')}
content={<h1>Hello world</h1>}>
<span data-testid="symbol-input--color-input">Click me</span>
</Popover>
div
post--component-drawer
不适用于该单元测试。所以我必须模拟 Popover
以确保我将 prop getPopupContainer
覆盖为 null,以便弹出窗口可见
所以在我的测试文件的开头,我嘲笑了
Popover
jest.mock('antd', () => {
const antd = jest.requireActual('antd');
/** We need to mock Popover in order to override getPopupContainer to null. getPopContainer
* sets the DOM container and if this prop is set, the popup div may not be available in the body
*/
const Popover = (props) => {
return <antd.Popover {...props} getPopupContainer={null} />;
};
return {
__esModule: true,
...antd,
Popover,
};
});
test('popver works', async () => {
render(<MyComponent/>);
fireEvent.mouseOver(screen.getByTestId('symbol-input--color-input'));
await waitFor(() => {
expect(screen.getByRole('heading', {level: 1})).toBeInTheDocument();
});
});
我发现这是最新的方式。您必须在测试中执行 async(),然后等待 findByRole,因为它不是即时的!
render(<LogoBar />);
fireEvent.mouseEnter(screen.getByLabelText('DaLabel'));
await screen.findByRole(/tooltip/);
expect(screen.getByRole(/tooltip/)).toBeInTheDocument();
我在类似的代码和 React 测试库中苦苦挣扎,使用外部组件,对我来说,诀窍是使用
fireEvent.focus(...component...)
相反。 希望它可以帮助一些人!
这是对 Akshay Pal 解决方案的轻微修改:
import { render, cleanup, waitFor, fireEvent, screen } from '@testing-library/react';
// Timeline component should render correctly with tool-tip
test('renders TimeLine component with mouse over(tool-tip)', async () => {
render(
<TimeLine
items={timeLineItems()}
currentItem={currentTimeLineItem()}
onTimeLineItemChange={() => {}}
/>
);
const courseTitle = "Course collection-3";
fireEvent.mouseEnter(screen.getByRole('button'));
expect(await screen.getByText(courseTitle)).toBeTruthy();
});