[英]How to test a className with the Jest and React testing library
[英]How to test conditionally rendered string using React testing library and jest
嗨,我是 React 測試庫的新手,需要一些幫助。 我想使用 React 測試庫測試以下代碼,以根據 boolean 的可見性道具有條件地確定組件上的文本是“活動”還是“非活動”:
const SomeComponent = ({visibility}) => {
return (
<Badge>
{visibility ? 'Active' : 'Inactive'}
<Badge />
);
};
任何幫助將不勝感激! 謝謝!
這是您可以為組件編寫的測試示例:
import { render } from "@testing-library/react";
import SomeComponent from "./SomeComponent";
it("displays only active when visibility is true", () => {
const { queryByText } = render(<SomeComponent visibility={true} />);
expect(queryByText("Active")).toBeInTheDocument();
expect(queryByText("Inactive")).not.toBeInTheDocument();
});
it("displays only inactive when visibility is false", () => {
const { queryByText } = render(<SomeComponent visibility={false} />);
expect(queryByText("Active")).not.toBeInTheDocument();
expect(queryByText("Inactive")).toBeInTheDocument();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.