reactjs - 如何测试与Router连接的React组件?
问题描述
我有这样一个组件
const ButtonWithRouter = ({ to, history, ...restProps }) => (
<Button
onClick={() => history.push(to)}
{...restProps}
/>
);
export default withRouter(ButtonWithRouter)
如您所见,它使用 react-router-dom 并与Router 连接。我的测试看起来像这样
test('render', () => {
const component = renderer.create(
<Router>
<ButtonWithRouter to='/anywhere'>Anywhere</ButtonWithRouter>
</Router>
);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
我认为在我的测试中导入和使用真正的路由器是不正确的。如何正确执行?
解决方案
推荐阅读
- json - 使用子查询创建 JSON 文件
- asp.net-core - 服务器端 Blazor 中的会话超时重定向
- python-3.x - 使用 __getattr__ 和 __slots__ 进行组合时避免 Pycharm __dict__ 查找
- python-3.x - 函数名称未在 Python 类中定义
- python - Markdown 中的可折叠表格
- html - 角 [ngIf]、[ngIfElse] 和 [ngIfElseIf]
- node.js - 部署应用程序时 Heroku 控制台中的标准输入
- r - 如何将 collums 转换为行
- drupal - Drupal jsonapi 跨包模块使用
- slurm - 取消在某个日期之前提交或 JOBID 低于给定整数的作业