Reputation: 1329
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
Upvotes: 3
Views: 1922
Reputation: 1329
So it seems the scrollTop
positions being outputted to the console
were triggered by the fixtures being reset. The scroll event wasn't being trigger at all by the window.scroll
function.
I managed to get the scroll event to trigger by animating $("html, body").scrollTop()
using JQuery animate.
Upvotes: 1