Skip to content

Commit d39460f

Browse files
committed
refactor(ngMock/$interval): more closely follow actual $interval's internal implementation
1 parent fbbaf89 commit d39460f

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/ngMock/angular-mocks.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -552,13 +552,15 @@ angular.mock.$IntervalProvider = function() {
552552
promise = deferred.promise;
553553

554554
count = (angular.isDefined(count)) ? count : 0;
555-
promise.then(null, function() {}, (!hasParams) ? fn : function() {
556-
fn.apply(null, args);
557-
});
558555

559556
promise.$$intervalId = nextRepeatId;
560557

561558
function tick() {
559+
if (skipApply) {
560+
$browser.defer(callback);
561+
} else {
562+
$rootScope.$evalAsync(callback);
563+
}
562564
deferred.notify(iteration++);
563565

564566
if (count > 0 && iteration >= count) {
@@ -581,6 +583,14 @@ angular.mock.$IntervalProvider = function() {
581583
}
582584
}
583585

586+
function callback() {
587+
if (!hasParams) {
588+
fn(iteration);
589+
} else {
590+
fn.apply(null, args);
591+
}
592+
}
593+
584594
repeatFns.push({
585595
nextTime: (now + (delay || 0)),
586596
delay: delay || 1,

test/ngMock/angular-mocksSpec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -323,16 +323,16 @@ describe('ngMock', function() {
323323

324324
it('should NOT call $apply if invokeApply is set to false',
325325
inject(function($interval, $rootScope) {
326-
var applySpy = spyOn($rootScope, '$apply').and.callThrough();
326+
var digestSpy = spyOn($rootScope, '$digest').and.callThrough();
327327

328328
var counter = 0;
329329
$interval(function increment() { counter++; }, 1000, 0, false);
330330

331-
expect(applySpy).not.toHaveBeenCalled();
331+
expect(digestSpy).not.toHaveBeenCalled();
332332
expect(counter).toBe(0);
333333

334334
$interval.flush(2000);
335-
expect(applySpy).not.toHaveBeenCalled();
335+
expect(digestSpy).not.toHaveBeenCalled();
336336
expect(counter).toBe(2);
337337
}));
338338

0 commit comments

Comments
 (0)