Jestjs 如何在react测试库中测试返回jsx的函数?
如何使用jest/react测试库测试组件是否返回特定的JSX 我必须安装等,但很难找到有关“反应测试库”的信息 以下是几个例子Jestjs 如何在react测试库中测试返回jsx的函数?,jestjs,react-testing-library,Jestjs,React Testing Library,如何使用jest/react测试库测试组件是否返回特定的JSX 我必须安装等,但很难找到有关“反应测试库”的信息 以下是几个例子 const className = props.className; if (!props.action) { return props.children; } const { id, type, target } = props.action; if (type === "EXTERNAL") { return (
const className = props.className;
if (!props.action) {
return props.children;
}
const { id, type, target } = props.action;
if (type === "EXTERNAL") {
return (
<a className={className} href={target}>
{props.children}
</a>
);
}
if (["page"].includes(type.toLowerCase())) {
return (
<Link className={className} key={id} to={"/" + target}>
{props.children}
</Link>
);
}
if (type.toLowerCase() === "play") {
return props.children;
} ...
const className=props.className;
如果(!道具动作){
返回道具。儿童;
}
const{id,type,target}=props.action;
如果(类型=“外部”){
返回(
);
}
if([“page”].includes(type.toLowerCase())){
返回(
{props.children}
);
}
如果(键入.toLowerCase()=“播放”){
返回道具。儿童;
} ...
正如您所看到的,每个返回都是不同的jsx。理想情况下,您应该查找面向用户的DOM元素,如文本、标签等。但是,对于这种组件,您可能需要返回到使用测试ID。您可以将
data test id=“SomeComponent”
添加到要返回的各种元素中,然后使用getByTestId
(或queryByTestId
等)查找这些元素。理想情况下,您应该查找面向用户的DOM元素,如文本、标签等。但是,对于此类组件,您可能需要退回到使用测试ID。您可以将data test id=“SomeComponent”
添加到要返回的各种元素中,然后使用getByTestId
(或queryByTestId
等)查找这些元素