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

rya*_*yan 12 javascript reactjs jestjs react-router

如果有重复,请原谅我.

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

当我尝试时,

<MemoryRouter initialEntries={['/hello']} initialIndex={0}>
      <Hello store={store} />
</MemoryRouter>
Run Code Online (Sandbox Code Playgroud)

我正进入(状态

match: { path: '/', url: '/', params: {} ... },
location: { path: '/hello', pathname: '/', ... },
history: { ..., location: { path: '/hello', pathname: '/', ... }}
Run Code Online (Sandbox Code Playgroud)

我想知道是否有办法设置匹配.提前致谢.

hin*_*nok 19

您可以<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>
Run Code Online (Sandbox Code Playgroud)

然后你应该有权访问适当的match对象props.