Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit d81ff88

Browse files
teritecaitp
authored andcommitted
fix(ngMock): call $interval callbacks even when invokeApply is false
Make ngMock.$interval behave like ng.$interval Closes #10032
1 parent eca14d9 commit d81ff88

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/ngMock/angular-mocks.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -454,17 +454,17 @@ angular.mock.$LogProvider = function() {
454454
* @returns {promise} A promise which will be notified on each iteration.
455455
*/
456456
angular.mock.$IntervalProvider = function() {
457-
this.$get = ['$rootScope', '$q',
458-
function($rootScope, $q) {
457+
this.$get = ['$browser', '$rootScope', '$q', '$$q',
458+
function($browser, $rootScope, $q, $$q) {
459459
var repeatFns = [],
460460
nextRepeatId = 0,
461461
now = 0;
462462

463463
var $interval = function(fn, delay, count, invokeApply) {
464-
var deferred = $q.defer(),
465-
promise = deferred.promise,
466-
iteration = 0,
467-
skipApply = (angular.isDefined(invokeApply) && !invokeApply);
464+
var iteration = 0,
465+
skipApply = (angular.isDefined(invokeApply) && !invokeApply),
466+
deferred = (skipApply ? $$q : $q).defer(),
467+
promise = deferred.promise;
468468

469469
count = (angular.isDefined(count)) ? count : 0;
470470
promise.then(null, null, fn);
@@ -487,7 +487,11 @@ angular.mock.$IntervalProvider = function() {
487487
}
488488
}
489489

490-
if (!skipApply) $rootScope.$apply();
490+
if (skipApply) {
491+
$browser.defer.flush();
492+
} else {
493+
$rootScope.$apply();
494+
}
491495
}
492496

493497
repeatFns.push({

test/ngMock/angular-mocksSpec.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,15 @@ describe('ngMock', function() {
321321
inject(function($interval, $rootScope) {
322322
var applySpy = spyOn($rootScope, '$apply').andCallThrough();
323323

324-
$interval(noop, 1000, 0, false);
324+
var counter = 0;
325+
$interval(function increment() { counter++; }, 1000, 0, false);
326+
325327
expect(applySpy).not.toHaveBeenCalled();
328+
expect(counter).toBe(0);
326329

327330
$interval.flush(2000);
328331
expect(applySpy).not.toHaveBeenCalled();
332+
expect(counter).toBe(2);
329333
}));
330334

331335

0 commit comments

Comments
 (0)