gpt4 book ai didi

javascript - 如何设置与 MemoryRouter 和 Jest 的匹配路径? (不是位置或历史)

转载 作者:可可西里 更新时间:2023-11-01 02:36:06 29 4
gpt4 key购买 nike

如有重复请见谅

我知道 MemoryRouter 有 initialEntries 和 initialIndex,所以你可以为“location”和“history”设置路径等。但是“匹配”没有得到更新...我需要为我的 React 应用程序和 Jest 测试设置“匹配”。

当我尝试时,

<MemoryRouter initialEntries={['/hello']} initialIndex={0}>
<Hello store={store} />
</MemoryRouter>

我得到了

match: { path: '/', url: '/', params: {} ... },
location: { path: '/hello', pathname: '/', ... },
history: { ..., location: { path: '/hello', pathname: '/', ... }}

不知道有没有办法设置匹配。提前致谢。

最佳答案

你可以包装你的 <Hello store={store} />组件 <Route ... />组件如:

import React from 'react';
import { MemoryRouter, Route } from 'react-router-dom';

// later in tests

<MemoryRouter initialEntries={['/home']}>
<Route component={props => <HomeComponent {...props} />} path="/home" />
</MemoryRouter>

然后您应该可以访问适当的 match对象 props .

关于javascript - 如何设置与 MemoryRouter 和 Jest 的匹配路径? (不是位置或历史),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45591812/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com