[英]how to write test case for nested jsx returning function using jest and react testing library
export default function App() {
let arr = [{name:"a"},{name:"b"},{name:"c"},{name:"d"},{name:"e"},{name:"f"}]
let **renderFn** = () => {
return arr.map(a=>{
return <div>{a.name}</div>
})
}
return (
<div className="App">
<div>{renderFn()}</div>
</div>
);
}
How to test test nested function that returns a complete jsx and that function is invoked in the jsx itself.如何测试测试嵌套的 function,它返回一个完整的 jsx 并且 function 在 jsx 本身中被调用。 I am unable to write test我无法编写测试
Do not use a nested function, instead move your function outside of the function App.不要使用嵌套的 function,而是将您的 function 移到 function App 之外。 Declare it as a "First-class citizen", as a function that you export.将其声明为“一等公民”,即您导出的 function。
Dependency injection what ever your function needs should be passed as arguments.依赖注入无论你的 function 需要什么,都应该作为 arguments 传递。
Now you can test your function as a stand alone unit.现在您可以将 function 作为独立单元进行测试。
export function List({ list }) {
return list.map(a => {
return <div>{a.name}</div>
})
}
export default function App() {
let arr = [{ name: "a" }, { name: "b" }, { name: "c" }, { name: "d" }, { name: "e" }, { name: "f" }]
return (
<div className="App">
<div><List list={arr} /></div>
</div>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.