From 5e52bb298dec44e33a06a1c4084d2f8c82fca3cb Mon Sep 17 00:00:00 2001 From: Joshua Minzner Date: Mon, 20 Jan 2014 17:41:04 -0500 Subject: [PATCH] fix($interval): call fn even if invokeApply is false Becasue calling the interval function relies on a promise notify handler, the interval function would not get called without a invokeApply being true. With this update, if invokeApply is false, the function is called normally. Closes #5902 --- src/ng/interval.js | 6 +++++- src/ngMock/angular-mocks.js | 6 +++++- test/ng/intervalSpec.js | 18 ++++++++++++++++++ test/ngMock/angular-mocksSpec.js | 14 ++++++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/ng/interval.js b/src/ng/interval.js index bde4e25e0363..bce95f9af9d1 100644 --- a/src/ng/interval.js +++ b/src/ng/interval.js @@ -143,7 +143,11 @@ function $IntervalProvider() { promise.then(null, null, fn); promise.$$intervalId = setInterval(function tick() { - deferred.notify(iteration++); + if (!skipApply) { + deferred.notify(iteration++); + } else { + fn(iteration++); + } if (count > 0 && iteration >= count) { deferred.resolve(iteration); diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index c2c9d7a2be97..c6e509554640 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -472,7 +472,11 @@ angular.mock.$IntervalProvider = function() { promise.$$intervalId = nextRepeatId; function tick() { - deferred.notify(iteration++); + if (!skipApply) { + deferred.notify(iteration++); + } else { + fn(iteration++); + } if (count > 0 && iteration >= count) { var fnIndex; diff --git a/test/ng/intervalSpec.js b/test/ng/intervalSpec.js index 6999f75098d6..e890a046ea1d 100644 --- a/test/ng/intervalSpec.js +++ b/test/ng/intervalSpec.js @@ -97,6 +97,24 @@ describe('$interval', function() { })); + it('should still call the interval fn if invokeApply is false', + inject(function($interval, $rootScope, $window) { + var spy = jasmine.createSpy('invokeApply = false'); + + $interval(spy, 100, 0, false); + expect(spy).not.toHaveBeenCalled(); + + $window.flush(100); + expect(spy).toHaveBeenCalled(); + + $window.flush(100); + expect(spy.callCount).toBe(2); + + $window.flush(100); + expect(spy.callCount).toBe(3); + })); + + it('should allow you to specify the delay time', inject(function($interval, $window) { var counter = 0; $interval(function() { counter++; }, 123); diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index fb602adc2ace..08ea192d0ebb 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -367,6 +367,20 @@ describe('ngMock', function() { expect(counterA).toEqual(4); expect(counterB).toEqual(1); })); + + it('should work if invokeApply is set to false', inject(function($interval) { + var counter = 0; + $interval(function() { counter++; }, 100, 0, false); + + $interval.flush(100); + expect(counter).toBe(1); + + $interval.flush(100); + expect(counter).toBe(2); + + $interval.flush(100); + expect(counter).toBe(3); + })); });