How do I fake-time a jQuery animation using Sinon in a Jasmine unit test?

好久不见. 提交于 2019-12-05 00:00:40

You can use jQuery off to turn off jQuery effects:

jQuery.fx.off = true

This won't solve your problem of the inital 7 second wait for your event to fire but it does mean you can test that the DOM has been changed as you expected.

I've had the same problem. I believe it happens because jQuery's animation takes advantage of requestAnimationFrame() if it's available instead relying of the setTimeout().

I've worked around this issue by setting all browser-specific requestAnimationFrame functions on the window object to null:

window.webkitRequestAnimationFrame = null;
window.mozRequestAnimationFrame = null;
window.oRequestAnimationFrame = null;

Make sure this code executes before jQuery is loaded.

I think it is possible with SinonJs as it showed here: http://sinonjs.org/qunit

test("should animate element over 500ms", function () {
   var el = jQuery("<div></div>");
   el.appendTo(document.body);

   el.animate({ height: "200px", width: "200px" });
   this.clock.tick(510);

   equals("200px", el.css("height"));
   equals("200px", el.css("width"));
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!