2017-02-28 9 views
0

は、私は、次のラッパーコンポーネントがあると。テスト非同期ネストされたコンポーネント

は今、次のように私はアクションが書かれてます私のことを言うことができます:

export const get =() => dispatch => http('/dummy_route') 
     .spread((response, body) => dispatch(actOnThing(update, body))) 

私はそうのようなテストを書く場合は今:

Unhandled rejection Error: Error: connect ECONNREFUSED 127.0.0.1:8080 

/* global window, test, expect, beforeAll, afterAll, describe */ 

'use strict' 

import React from 'react' 
import FooDisplay from './index' 
import {mount} from 'enzyme' 
import {Provider} from 'react-redux' 
import configureStore from '../../store/configureStore' 
import nock, {uriString} from '../../config/nock' 
import _ from 'lodash' 


const env = _.cloneDeep(process.env) 
describe('the component behaves correctly when integrating with store and reducers/http',() => { 
    beforeAll(() => { 
    nock.disableNetConnect() 
    process.env.API_URL = uriString 
    }) 

    afterAll(() => { 
    process.env = _.cloneDeep(env) 
    nock.enableNetConnect() 
    nock.cleanAll() 
    }) 

    test('when deep rendering, the load event populates the input correctly',() => { 
    const store = configureStore({ 
     address: { 
     address: 'foo' 
     } 
    }) 
    const display = mount(<Provider store={store}><FooDisplay /></Provider>, 
     {attachTo: document.getElementById('root')}) 
    expect(display.find('p').find('.address').text()).toEqual('foo') 
    const button = display.find('LoadFromServerButton') 
    expect(button.text()).toEqual('fetch serverside address') 
    nock.get('/dummy_address').reply(200, {address: 'new address'}) 
    button.simulate('click') 
    }) 
}) 

これは、その結果

ちょっと考えた後で、これは、ボタンをクリックするとテストが約束を返さないという事実によるものですフードの下で発射することを約束しているので、afterAllが即時に実行され、ノックがきれいになり、実際のhttp接続がワイヤーを通過します。

このケースをテストするにはどうすればよいですか?正しい約束を返す簡単な方法がないようです...これらの更新結果のDOMへの更新をテストするにはどうすればよいですか?

+0

あなたは拒否された約束を守っていないようですが、それが達成されたときだけです。オフライン環境をシミュレートする予定ですか? nock.disableNetConnect()とそれに対応するものを削除するとどうなりますか?テストで非同期アクションが実行されている場合は、doneパラメーターをインクルードし、非同期アクションが完了したときに呼び出す必要があります。また、テストの約束を返すこともできます。 https://facebook.github.io/jest/docs/asynchronous.html – nbkhope

+0

ええ、私は理解しますが、どうすれば正しい約束を返すことができますか?ボタンをクリックすると非同期アクションがトリガされ、適切なテストがテストから返されるように強制する方法がわかりません –

+0

「更新」機能の定義はどこですか?私はthis.props.getFoo()がLoadFromServerButtonのdoUpdate関数の中でthis.props.get()のために意図されたように感じる? – nbkhope

答えて

0

あなたが言いましたように、テストから復帰するという約束はありません。

import {get} from '../../actions/actions' 
jest.mock('../../actions/actions',() => ({get: jest.fn})) 

これはあなたが約束を作成してみましょうすることができ、テスト中のオブジェクト{get: jestSpy}

でアクションモジュールを交換します:だからgetリターンノウハウがあなただけのノックを使用せずに直接getを模擬することができます約束作りますgetこれを返して、あなたのテストからこの約束を返してください:

it('',()=>{ 
    const p = new Promise.resolve('success') 
    get.mockImplementation(() => p)//let get return the resolved promise 
    //test you suff 
    return p 
}) 
関連する問題