Async image loading in Jasmine 2.x

51 views
Skip to first unread message

Alexander

unread,
Jul 27, 2015, 11:00:10 AM7/27/15
to Jasmine
Hi,

I'm trying to load some images in my specs and need to wait with expectations until the images are fully loaded. In Jasmine 1.x this was pretty straightforward as I could waitFor img.complete. How can I accomplish this in Jasmine 2.x with a done callback?

Here is my example code where my custom matcher gets image data from a canvas object and compares it against the expected image which often fails due to not being loaded yet.

it("should be equal", function(){
   
    var canvas = $('.canvas').get(0);
    var actual = canvas.getContext('2d').getImageData(0,0,canvas.width, canvas.height);
  
    var expected = new Image();
    expected.src = 'test.png';

    expect
(actual).toImageDiffEqual(expected);
});

Cheers
Alexander

Gregg Van Hove

unread,
Jul 27, 2015, 2:51:06 PM7/27/15
to jasmi...@googlegroups.com
Take a look at the docs for [async specs](http://jasmine.github.io/edge/introduction.html#section-Asynchronous_Support). You will need to write a function that checks if the image has completed loading and call the `done` callback yourself.

Hope this helps,

-Gregg

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at http://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages