2017-08-09 68 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對象。