2017-08-09 122 views
2

如果有重复,请原谅我。如何设置与MemoryRouter和Jest匹配的路径? (不是位置或历史)

我知道MemoryRouter有initialEntries和initialIndex,所以你可以设置路径等“位置”和“历史”。然而,“匹配”没有得到更新......我需要为我的反应应用和Jest测试设置“匹配”。

当我尝试,

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

我越来越

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

我不知道是否有设置匹配的方式。提前致谢。

回答

2

你可以<Route ... />组件一样包住<Hello store={store} />组件:

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> 

那么你应该通过props得到适当的match对象。