Jest Test React Router 链接导航

发布于 2025-01-12 22:51:26 字数 1133 浏览 2 评论 0原文

我想使用 Jest Enzyme 测试以下组件。

<BrowserRouter>
   <Switch>
      <Route path='/' component={Home}/>
      <Route path='/view' component={View}/>
   </Switch>
</BrowserRouter>

class Home extends React.Component {
    render {
      <Link to='/view'>
         <button onClick={() => foo}>View Data</button>
      </Link>
    }
}

该代码在应用程序中正常工作。 但是,当我使用 jest-enzyme 测试按钮单击时,组件路径或内容不会改变

it('should redirect on button click', () => {
   const component= mount(
      <MemoryRouter initialEntries={{['/', '/view']}}>
         <Route path='/' component={Home}/>
         <Route path='/view' component={View}/>
      </MemoryRouter>
   )

   expect(component.find('Home')).toHaveLength(1); //passes
   expect(component.find('LinkAnchor').props().href).toEqual('/view'); //this also passes
   component.find('button').simulate('click');
   expect(component.find('View').toHaveLength(1); //This FAILS
});

我的问题:

  1. 测试链接的唯一方法可以是检查其内部标签中的 href 吗?
  2. 我们可以测试页面导航是否正常工作吗?

I have the following component that I want to test using Jest Enzyme.

<BrowserRouter>
   <Switch>
      <Route path='/' component={Home}/>
      <Route path='/view' component={View}/>
   </Switch>
</BrowserRouter>

class Home extends React.Component {
    render {
      <Link to='/view'>
         <button onClick={() => foo}>View Data</button>
      </Link>
    }
}

This code is working in application correctly.
However, when I test button click using jest-enzyme, component path or content doesn't change

it('should redirect on button click', () => {
   const component= mount(
      <MemoryRouter initialEntries={{['/', '/view']}}>
         <Route path='/' component={Home}/>
         <Route path='/view' component={View}/>
      </MemoryRouter>
   )

   expect(component.find('Home')).toHaveLength(1); //passes
   expect(component.find('LinkAnchor').props().href).toEqual('/view'); //this also passes
   component.find('button').simulate('click');
   expect(component.find('View').toHaveLength(1); //This FAILS
});

My question:

  1. Is only way to test Link could be checking href in it's internal a tag?
  2. Can we test page navigation like it works properly for ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溇涏 2025-01-19 22:51:26

在这种情况下,您可能想要测试 MemoryRouter 的历史对象,以便在单击链接时进行更新。

但是,您遇到了这个问题,因为您用链接包装了一个按钮,并且您按下的是按钮而不是链接。

如果您的链接需要按钮样式,则应该更新 Link 组件:

return (
  <Link to='/view' className='buttonClass' onClick={() => foo()} />
);

In this case you probably want to test the history object of the MemoryRouter to be updated when you have clicked the link.

However, you are running into this problem because you're wrapping a button with a link and you are pressing the button not the link.

If you need button styling for your links, you should update the Link component instead:

return (
  <Link to='/view' className='buttonClass' onClick={() => foo()} />
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文