jasmine.clock().tick() does not work with $timeout and debounce, but works fine with setTimeout
The clock in Jasmine will only work if you're testing setInterval()
or setTimeout()
functions directly as it just mocks those functions specifically to run synchronously. I believe there's a pull request for Jasmine to mock the Date object, which would allow for testing functions like _.debounce()
without mocking it, but I don't remember if that was merged in or not.
To test _.debounce()
you'll have to mock it to run synchronously, preferably as a spy or you can just override the function. Here's what I've found to work for me:
spyOn(_, 'debounce').and.callFake(function (func) {
return function () {
func.apply(this, arguments);
};
});
Now calls to _.debounce()
will run synchronously and tests should complete successfully. Of course you'll still have to use $timeout.flush()
.
I updated your plunker with these changes: http://plnkr.co/edit/KXmwcf1faUNf8nlqPeyd
The debounce function from lodash
uses the Date
object. You mock the Date
object using jasmine
like this:
jasmine.clock().install();
jasmine.clock().mockDate();
jasmine.clock().tick(1000); // trigger the debounce
Source: https://jasmine.github.io/2.9/introduction.html#section-Mocking_the_Date