2017-08-20 9 views
1

モジュール用のJEST/Enzymeテストケースを作成しています。Jest-Mockネスト関数

私のコンポーネントでテストしている機能の1つは、xmlファイルを保存することです。今度は私がテストケースを書いたとき、それはsaveContentToXMLを呼び出し、今度はfileDownloadを呼び出し

const saveContentToXML =() => { 
    if(this.props.message && this.props.message.details){ 
     fileDownload(this.props.message.details, this.props.message.title); 
    } 
} 

「反応し、ファイルのダウンロード」からライブラリ関数fileDownloadを呼び出します。これは例外となります。

TypeError: window.URL.createObjectURL is not a function 

私のテストケースは、私はこの関数をテストするにはどうすればよい

test('Save Content as XML Test',() =>{ 
    const component = shallow(<Message details={details} />); 
    component.instance().saveContentToXML(); 
}); 

のように見えますか?

+0

多分これはあなたを助けることができます:https://stackoverflow.com/questions/45325081/mocking-dependency-of-module-function-under-test-in-javascript – croraf

答えて

1

あなたが反応し、ファイルのダウンロードを模擬し、モックの機能上の冗談のドキュメントを参照するには良いリソースです

// Default mock will just make fileDownload a jest mock function 
jest.mock('react-file-download') 

import fileDownload from 'react-file-download' 

test('Save Content as XML Test',() =>{ 
    const component = shallow(<Message details={details} />); 
    component.instance().saveContentToXML(); 
    expect(fileDownload).toHaveBeenCalledWith('details', 'title'); 
    fileDownload.mockClear() // resets the mock function so the call is not used in assertions for other tests 
}); 

と呼ばれていることを主張すべきである:https://facebook.github.io/jest/docs/mock-functions.html