3

I'm trying to test a widget i've created that does some stuff with scrolling. I'm using Jasmine and jasmine-jquery for testing but I'm getting some odd results.

I want to test that my widget successfully triggers an event when the page scrolls in a specific way. I've simplified things here in case it's my widget causing the problem, so I'm just trying to test that the scroll event has been triggered on the window.

it("should trigger scrollEvent on the window", function(){
    loadFixtures('lorem-ipsum.html'); //gives some height to the page
    window.scrollTo(0,0);
    console.log($(window).scrollTop()); //Should return 0

    $(window).bind("scroll", function (e) {
        console.log("Now scrolling");
    });
    window.scroll(0,100);
    console.log($(window).scrollTop()); //Should return 100
    window.scroll(0,50);
    console.log($(window).scrollTop()); //Should return 50

    expect('scroll').toHaveBeenTriggeredOn(window);
});

In the console I get the following

Testing started at 16:32 ...
0
100
50
    Expected event scroll to have been triggered on [object Object]
    ()@C:/Projects/jquery.scrollevents/tests/specs/development.js:45


'Now scrolling'
    Expected event scroll to have been triggered on [object Object]
    Error: Expected event scroll to have been triggered on [object Object]
        at null.<anonymous> (C:/Projects/jquery.scrollevents/tests/specs/development.js:45:34)

0
100
50'Now scrolling'
Process finished with exit code 0

I'm not sure what to make of this as the order of events is odd. It runs twice? The scroll event is only triggered after the expect?

I've debugged it in the browser and you can see the same thing happening. Test failure first then browser events triggering.

I've looked at Jasmine's asynchronous features but I'm not sure what I'm waiting for?

Any help would be greatly appreciated. Thanks

4

1 回答 1

1

因此,scrollTop输出到 的位置似乎console是由重置的灯具触发的。window.scroll该函数根本没有触发滚动事件。

我设法通过$("html, body").scrollTop()使用 JQuery animate 制作动画来触发滚动事件。

于 2013-10-12T17:02:01.350 回答