I have code that is something like this:
function doThing() {
if (invalidInput) {
console.error('Invalid input.');
return;
}
$timeout(function() {
MyService.doThing();
}, 1000);
}
I want to test that MyService.doThing
isn't called when invalid input is passed in.
If I call doThing(invalidInput)
without doing $timeout.flush()
, MyService.doThing
wouldn't be called, regardless of whether I have lines 2-5. So to really test whether MyService.doThing
is called when invalid input is passed in, I need to call $timeout.flush
.
The problem is that it throws an error if I try to flush when there's nothing to flush. Error: No deferred tasks to be flushed
.
How can I handle this scenario? I'd like to do something like, $timeout.flushIfFlushable()
.