I have a mocha + chai test suite set up for unit testing in my react app. This has been working great, however I am trying to cover one small bit of new code and am not sure how to best approach this. I have a small function that calls jquery and a jquery plugin to print a page. The code looks like so :
printAsset() {
const iframeDocument = $('#previewForPrint').contents().find('body');
$.print(iframeDocument, {
globalStyles: false,
mediaPrint: true,
stylesheet: null,
noPrintSelector: "scrispt",
iframe: true,
append: null,
prepend: null,
manuallyCopyFormValues: true
});
}
This functionality works fine, however I cannot seem to find a way to correctly unit test this. A few important things to note are that the jquery and jquery.print are not installed in the package.json, they are bower components that are loaded on the index.html. So when running my unit test i don't believe i have access to jquery and the print jquery addon. Here is my test :
it('fires print when the print button is clicked', () => {
const previewCAS = TestUtils.renderIntoDocument(<PrintAsset />);
const button = TestUtils.scryRenderedDOMComponentsWithTag(PrintAsset, 'button');
TestUtils.Simulate.click(button[0]);
// breaks here
// expect(Print).to.have.been.called();
});
This breaks with : ReferenceError: $ is not defined . So, I understand this, jquery is not imported uptop because its global with bower, however I am unsure how I am suppose to test this otherwise with the current suite. Is there a better way to do this or alternative approaches? Any advice would be greatly appreciated, thanks for reading!
Aucun commentaire:
Enregistrer un commentaire