“react + react-route"的单元测试;堆 [英] Unit testing on "react + react-route" stack

查看:35
本文介绍了“react + react-route"的单元测试;堆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了许多关于如何通过 react-router 组件渲染路由的建议,但我仍然无法使其工作.我尝试使用 github 代码库搜索找到它,但仍然没有运气.在这一点上,我需要的只是一个工作示例.

I've read many recommendations of how it's possible to render routed via react-router components, but I still can't to make it work. I tried to find it using github codebase search, still no luck. And at this point all I need is one working example.

这是我的样板项目,但也许这并不重要.我只想看一些 react-route 单元测试工作示例.

Here is my boilerplate project, but maybe it's not important. I just want to see some react-route unit-testing working example.

推荐答案

(v4 中可能是新的)这样做的方法是将您正在测试的组件包装在 react 提供的 MemoryRouter 中-路由器.

The (possbily new in v4) way of doing this is to wrap the component you're testing in the MemoryRouter provided by react-router.

import {MemoryRouter} from 'react-router-dom';
import {render} from 'react-dom';

render(<MemoryRouter>
  <YourComponent>
</MemoryRouter>, node, () => {});

这篇关于“react + react-route"的单元测试;堆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆