reactjs - Jest - Mock Nested Function -
i writing jest/enzyme test case module.
one of function testing in component saving xml file. in turn calls library function filedownload 'react-file-download'
const savecontenttoxml = () => { if(this.props.message && this.props.message.details){ filedownload(this.props.message.details, this.props.message.title); } }
when wrote test case, calls savecontenttoxml , in turn calls filedownload. results in exception.
typeerror: window.url.createobjecturl not function
my test case looks like
test('save content xml test', () =>{ const component = shallow(<message details={details} />); component.instance().savecontenttoxml(); });
how test function?
you should mock react-file-download , assert being called
// default mock make filedownload jest mock function jest.mock('react-file-download') import filedownload 'react-file-download' test('save content xml test', () =>{ const component = shallow(<message details={details} />); component.instance().savecontenttoxml(); expect(filedownload).tohavebeencalledwith('details', 'title'); filedownload.mockclear() // resets mock function call not used in assertions other tests });
the jest documentation on mock functions resource refer to: https://facebook.github.io/jest/docs/mock-functions.html
Comments
Post a Comment