Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 392c0ad

Browse files
Gahenchrisirhc
authored andcommitted
fix(carousel): replaced $timeout with $interval when it was wrong
I had a to tweak a bit two tests since $interval never throws an exception after flushing, hope I did it OK. Ammend: addresses @chrisirhc critics Fixes #1308 Fixes #2454 Closes #2776
1 parent faf38d2 commit 392c0ad

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

Diff for: src/carousel/carousel.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
*
88
*/
99
angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
10-
.controller('CarouselController', ['$scope', '$timeout', '$transition', function ($scope, $timeout, $transition) {
10+
.controller('CarouselController', ['$scope', '$timeout', '$interval', '$transition', function ($scope, $timeout, $interval, $transition) {
1111
var self = this,
1212
slides = self.slides = $scope.slides = [],
1313
currentIndex = -1,
14-
currentTimeout, isPlaying;
14+
currentInterval, isPlaying;
1515
self.currentSlide = null;
1616

1717
var destroyed = false;
@@ -106,22 +106,22 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
106106
function restartTimer() {
107107
resetTimer();
108108
var interval = +$scope.interval;
109-
if (!isNaN(interval) && interval>=0) {
110-
currentTimeout = $timeout(timerFn, interval);
109+
if (!isNaN(interval) && interval > 0) {
110+
currentInterval = $interval(timerFn, interval);
111111
}
112112
}
113113

114114
function resetTimer() {
115-
if (currentTimeout) {
116-
$timeout.cancel(currentTimeout);
117-
currentTimeout = null;
115+
if (currentInterval) {
116+
$interval.cancel(currentInterval);
117+
currentInterval = null;
118118
}
119119
}
120120

121121
function timerFn() {
122-
if (isPlaying) {
122+
var interval = +$scope.interval;
123+
if (isPlaying && !isNaN(interval) && interval > 0) {
123124
$scope.next();
124-
restartTimer();
125125
} else {
126126
$scope.pause();
127127
}

Diff for: src/carousel/test/carousel.spec.js

+25-21
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ describe('carousel', function() {
1414
}));
1515
beforeEach(module('template/carousel/carousel.html', 'template/carousel/slide.html'));
1616

17-
var $rootScope, $compile, $controller, $timeout;
18-
beforeEach(inject(function(_$rootScope_, _$compile_, _$controller_, _$timeout_) {
17+
var $rootScope, $compile, $controller, $interval;
18+
beforeEach(inject(function(_$rootScope_, _$compile_, _$controller_, _$interval_) {
1919
$rootScope = _$rootScope_;
2020
$compile = _$compile_;
2121
$controller = _$controller_;
22-
$timeout = _$timeout_;
22+
$interval = _$interval_;
2323
}));
2424

2525
describe('basics', function() {
@@ -148,16 +148,18 @@ describe('carousel', function() {
148148

149149
it('shouldnt go forward if interval is NaN or negative', function() {
150150
testSlideActive(0);
151+
var previousInterval = scope.interval;
151152
scope.$apply('interval = -1');
152-
//no timeout to flush, interval watch doesn't make a new one when interval is invalid
153+
$interval.flush(previousInterval);
153154
testSlideActive(0);
154155
scope.$apply('interval = 1000');
155-
$timeout.flush();
156+
$interval.flush(1000);
156157
testSlideActive(1);
157158
scope.$apply('interval = false');
159+
$interval.flush(1000);
158160
testSlideActive(1);
159161
scope.$apply('interval = 1000');
160-
$timeout.flush();
162+
$interval.flush(1000);
161163
testSlideActive(2);
162164
});
163165

@@ -182,34 +184,35 @@ describe('carousel', function() {
182184

183185
it('should be playing by default and cycle through slides', function() {
184186
testSlideActive(0);
185-
$timeout.flush();
187+
$interval.flush(scope.interval);
186188
testSlideActive(1);
187-
$timeout.flush();
189+
$interval.flush(scope.interval);
188190
testSlideActive(2);
189-
$timeout.flush();
191+
$interval.flush(scope.interval);
190192
testSlideActive(0);
191193
});
192194

193195
it('should pause and play on mouseover', function() {
194196
testSlideActive(0);
195-
$timeout.flush();
197+
$interval.flush(scope.interval);
196198
testSlideActive(1);
197199
elm.trigger('mouseenter');
198-
expect($timeout.flush).toThrow();//pause should cancel current timeout
200+
testSlideActive(1);
201+
$interval.flush(scope.interval);
199202
testSlideActive(1);
200203
elm.trigger('mouseleave');
201-
$timeout.flush();
204+
$interval.flush(scope.interval);
202205
testSlideActive(2);
203206
});
204207

205208
it('should not pause on mouseover if noPause', function() {
206209
scope.$apply('nopause = true');
207210
testSlideActive(0);
208211
elm.trigger('mouseenter');
209-
$timeout.flush();
212+
$interval.flush(scope.interval);
210213
testSlideActive(1);
211214
elm.trigger('mouseleave');
212-
$timeout.flush();
215+
$interval.flush(scope.interval);
213216
testSlideActive(2);
214217
});
215218

@@ -219,7 +222,7 @@ describe('carousel', function() {
219222
scope.$apply('slides.splice(0,1)');
220223
expect(elm.find('div.item').length).toBe(2);
221224
testSlideActive(1);
222-
$timeout.flush();
225+
$interval.flush(scope.interval);
223226
testSlideActive(0);
224227
scope.$apply('slides.splice(1,1)');
225228
expect(elm.find('div.item').length).toBe(1);
@@ -253,14 +256,15 @@ describe('carousel', function() {
253256

254257
it('issue 1414 - should not continue running timers after scope is destroyed', function() {
255258
testSlideActive(0);
256-
$timeout.flush();
259+
$interval.flush(scope.interval);
257260
testSlideActive(1);
258-
$timeout.flush();
261+
$interval.flush(scope.interval);
259262
testSlideActive(2);
260-
$timeout.flush();
263+
$interval.flush(scope.interval);
261264
testSlideActive(0);
265+
spyOn($interval, 'cancel').andCallThrough();
262266
scope.$destroy();
263-
expect($timeout.flush).toThrow('No deferred tasks to be flushed');
267+
expect($interval.cancel).toHaveBeenCalled();
264268
});
265269

266270
});
@@ -326,12 +330,12 @@ describe('carousel', function() {
326330
scope.interval = 2000;
327331
scope.$digest();
328332

329-
$timeout.flush();
333+
$interval.flush(scope.interval);
330334
expect(scope.next.calls.length).toBe(1);
331335

332336
scope.$destroy();
333337

334-
$timeout.flush(scope.interval);
338+
$interval.flush(scope.interval);
335339
expect(scope.next.calls.length).toBe(1);
336340
});
337341
});

0 commit comments

Comments
 (0)