2017-01-08 8 views
11

酵素+モカ+チャイを使用して、私の反応還元プロジェクトをテストしています。酵素は、コンポーネントの動作をテストするために浅いものを提供します。しかし、私はルータをテストする方法を見つけることができませんでした。私はNurseAuthorizationコンポーネントを参照してください。このルートnurse/authorizationをテストしたい反応ルータを酵素でテストするには

<Router history={browserHistory}> 
    ... 
     <Route path="nurse/authorization" component{NurseAuthorization}/> 
    ... 
    </Route> 

:私は反応し、ルータを以下のように使用しています。どのようにreactjsプロジェクトでそれをテストするには?

EDIT1

私は、ルータのフレームワークとしてreact-routerを使用しています。

答えて

7

ルーターをコンポーネント内にラップしてテストすることができます。

Routes.jsx

​​

index.js

import Routes from './Routes.jsx'; 
... 

ReactDOM.render(<Routes />, document.getElementById('root')); 

次に、あなたがあなたのRoutesコンポーネントをレンダリング浅くする必要があります、そしてあなたがチェックするためにオブジェクトマップを作成することができますパスと関連するコンポーネントとの間の対応。

Routes.test.js

import { shallow } from 'enzyme'; 
import { Route } from 'react-router'; 
import Routes from './Routes.jsx'; 
import NurseAuthorization from './NurseAuthorization.jsx'; 

it('renders correct routes',() => { 
    const wrapper = shallow(<Routes />); 
    const pathMap = wrapper.find(Route).reduce((pathMap, route) => { 
    const routeProps = route.props(); 
    pathMap[routeProps.path] = routeProps.component; 
    return pathMap; 
    }, {}); 
    // { 'nurse/authorization' : NurseAuthorization, ... } 

    expect(pathMap['nurse/authorization']).toBe(NurseAuthorization); 
}); 
+0

私はあなたの指示に従いましたが、 'const wrapper = shallow();という行に以下のエラーが表示されました。 - オブジェクトはコンストラクタではありません( 'Component(publicProps、publicContext、updateQueue)'を評価しています) –

+0

私は 'react-router'を使用しています –

+0

@ZhaoYiルートはリアクションコンポーネントでなければなりません(Routes.jsx参照) – Freez

0

私は私のパスは、ダイナミックルータの別のファイルに定義されていたので、私はまた私がルートとしてレンダリングしていたすべてのルートが私のパスに定義されていることをテストしてい.js定数:

it('Routes should only have paths declared in src/routing/paths.js',() => { 
    const isDeclaredInPaths = (element, index, array) => { 
    return pathsDefined.indexOf(array[index]) >= 0; 
    } 
    expect(routesDefined.every(isDeclaredInPaths)).to.be.true; 
}); 
関連する問題