我有加载页面:
const Loading: React.FunctionComponent = () => {
return (
<div className="w-screen h-screen overflow-hidden">
<LoadingBackground />
</div>
);
};
如何测试LoadingBackground
是否已渲染?
import { render } from "@testing-library/react";
import Loading from "./Loading";
import LoadingBackground from "../../components/LoadingBackground";
test("Loading", () => {
const parent = render(<Loading />);
expect(parent).toContainElement(loadingBackground); <-- Check if LoadingBackground is rendered or not?
});
1条答案
按热度按时间0md85ypi1#
你需要在
LoadingBackground
中添加一些可以用来标识它的东西,如果你没有任何唯一的文本,你可以把data-testid={'loadingBackground'}
作为一个属性添加到你的元素中,并执行如下操作: