首页 > 解决方案 > 通过使用 Jest 在 Router 中将函数作为 props 传递来测试函数

问题描述

我是新来为反应中的功能组件编写测试用例。我有一个下面的功能组件,我需要为 checkCreated() 函数编写一个测试用例,它在其中调用一个 useState 函数。

function TestComponent() {
 const [Icon, setIcon] = useState(false);
 const checkCreated = status => {
   setIcon(status);
   };

<Router>
  <Route exact path="/test" component={test} />            
  <Route exact path="/sample1" render={props => ( <Sample1 {...props} checkCreated ={ checkCreated } /> )} />
  <Route exact path="/sample2" render={props => ( <Sample2 {...props} checkCreated ={ checkCreated } /> )} />
  />
  </Router>
export default TestComponent;

有人可以帮助我编写测试用例来覆盖使用路由器的 checkCreated()。

标签: reactjsjestjsuse-state

解决方案


推荐阅读