jeudi 28 avril 2016

How to stub a javascript library like html2canvas

I have to test an ember app that uses html2canvas library, but the test is failing and I believe its because of the promise. I have already wrapped the asynchronous part like this:

/**
 * This method render DOM object (webpage) and generated canvas object using html2canvas library.
 * After getting canvas object it set image property.
 *
 * @method 'captureScreen'
 * @param {DOM}
 * @return {Ember.RSVP.Promise}
 */
captureScreen: function (dom) {
    return new Ember.RSVP.Promise((resolve) => {
        html2canvas(dom).then((canvas) => {
            resolve(canvas);
        });
    });
}

So now I am trying to stub the html2canvas lib itself like this:

stubHtml2Canvas = function () {
    _stubHtml2Canvas = sinon.stub(html2canvas).returns(new Ember.RSVP.Promise(function (resolve) {
        resolve(document.createElement("CANVAS"));
    }));
}

but this throws out following err:

beforeEach failed on should show overlay when clicking on the insight capture button: Attempted to wrap undefined property undefined as function

I had also tried

sinon.createStubInstance(html2canvas).returns(new Ember.RSVP.Promise(function (resolve) {
        resolve(document.createElement("CANVAS"));
    }));

But that didn't work either.




Aucun commentaire:

Enregistrer un commentaire