2017-04-01 71 views
1

我正在用chai + karma +酶測試反應組件。測試反應組件(context.router - undefind)

mount(
<Provider store={configureStore(mockContext, null)}> 
    <Component {...props} /> 
</Provider> 
) 

我在這個組件中使用context.router.location。我得到這個未定義的錯誤。我嘗試通過與第二個參數掛載像這樣的上下文:

mount(
<Provider store={configureStore(mockContext, null)}> 
    <Component {...props} /> 
</Provider>,  
    { 
     context: { 
     router: { 
      location: { pathname: '' } 
     } 
     }, 
     childContextTypes: { 
     router: React.PropTypes.object 
     } 
    }) 

如何在我的組件中傳遞上下文?

"react-router": "^3.0.2", 
"enzyme": "^2.3.0", 

p.s.打字稿:)

回答

0

您需要通過childContextTypes作爲一個單獨的對象,而不是作爲背景的孩子一樣

mount(
    <Provider store={configureStore(mockContext, null)}> 
     <Component {...props} /> 
    </Provider>,  
    { 
     context: { 
     router: { 
      location: { pathname: '' } 
     } 
     }, 
     childContextTypes: { 
      router: React.PropTypes.object 
     } 

    }) 
+0

遺憾。我的例子是壞的:(我像你一樣做,我有一個路由器 - 未定義。 也許我可以以某種方式不同地做到這一點? – Daxik