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

Commit cc47295

Browse files
committed
refactor($$animation): use $animateRunner
1 parent 259a572 commit cc47295

File tree

4 files changed

+49
-41
lines changed

4 files changed

+49
-41
lines changed

src/ngAnimate/animateCss.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ var $AnimateCssProvider = ['$animateProvider', function($animateProvider) {
293293
var activeClasses = pendClasses(setupClasses, '-active');
294294
var hasToStyles = styles.to && Object.keys(styles.to).length > 0;
295295

296-
// there is no way we can trigger an animation since no styles or
296+
// there is no way we can trigger an animation since no styles and
297297
// no classes are being applied which would then trigger a transition
298298
if (!hasToStyles && !setupClasses) {
299299
close();

src/ngAnimate/animation.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,19 @@ var $$AnimationProvider = ['$animateProvider', function($animateProvider) {
2020
return element.data(RUNNER_STORAGE_KEY);
2121
}
2222

23-
this.$get = ['$qRaf', '$$jqLite', '$rootScope', '$injector', '$animateRunner', '$$animateOptions',
24-
function($qRaf, $$jqLite, $rootScope, $injector, $animateRunner, $$animateOptions) {
23+
this.$get = ['$$jqLite', '$rootScope', '$injector', '$animateRunner', '$$animateOptions',
24+
function($$jqLite, $rootScope, $injector, $animateRunner, $$animateOptions) {
2525

2626
var animationQueue = [];
2727

2828
return function(element, event, options) {
2929
options = $$animateOptions(element, options);
30-
var deferred = $qRaf.defer();
3130

3231
// there is no animation at the current moment, however
3332
// these runner methods will get later updated with the
3433
// methods leading into the driver's end/cancel methods
3534
// for now they just stop the animation from starting
36-
var runner = $$animateRunner(deferred.promise, {
35+
var runner = new $animateRunner({
3736
end: function() { close(); },
3837
cancel: function() { close(true); }
3938
});
@@ -252,7 +251,7 @@ var $$AnimationProvider = ['$animateProvider', function($animateProvider) {
252251
}
253252

254253
function update(element) {
255-
$$animateRunner(getRunner(element), newRunner);
254+
getRunner(element).setHost(newRunner);
256255
}
257256
}
258257

@@ -276,7 +275,7 @@ var $$AnimationProvider = ['$animateProvider', function($animateProvider) {
276275
}
277276

278277
element.removeClass(NG_ANIMATE_CLASSNAME);
279-
rejected ? deferred.reject() : deferred.resolve();
278+
runner.complete(!rejected);
280279
}
281280
};
282281
}];

test/ngAnimate/animateCssSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
ddescribe("ngAnimate $animateCss", function() {
3+
describe("ngAnimate $animateCss", function() {
44

55
beforeEach(module('ngAnimate'));
66

test/ngAnimate/animationSpec.js

+42-33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
describe('$$animation', function() {
3+
ddescribe('$$animation', function() {
44

55
beforeEach(module('ngAnimate'));
66

@@ -14,14 +14,15 @@ describe('$$animation', function() {
1414
}));
1515

1616
it("should not run an animation if there are no drivers",
17-
inject(function($$animation, $$rAF) {
17+
inject(function($$animation, $$rAF, $rootScope) {
1818

1919
element = jqLite('<div></div>');
2020
var done = false;
2121
$$animation(element, 'someEvent').then(function() {
2222
done = true;
2323
});
2424
$$rAF.flush();
25+
$rootScope.$digest();
2526
expect(done).toBe(true);
2627
}));
2728

@@ -40,6 +41,7 @@ describe('$$animation', function() {
4041
});
4142
$rootScope.$digest();
4243
$$rAF.flush();
44+
$rootScope.$digest();
4345
expect(done).toBe(true);
4446
});
4547
});
@@ -195,6 +197,7 @@ describe('$$animation', function() {
195197
// the resolve/rejection digest
196198
$rootScope.$digest();
197199
$$rAF.flush();
200+
$rootScope.$digest();
198201

199202
expect(status).toBe(event);
200203
});
@@ -207,13 +210,12 @@ describe('$$animation', function() {
207210

208211
module(function($$animationProvider, $provide) {
209212
$$animationProvider.drivers.push('actualDriver');
210-
$provide.factory('actualDriver', function($qRaf, $$animateRunner) {
213+
$provide.factory('actualDriver', function($animateRunner) {
211214
return function() {
212215
return {
213216
start: function() {
214217
log.push('start');
215-
var promise = $qRaf.when(true);
216-
return $$animateRunner(promise, {
218+
return new $animateRunner({
217219
end: function() {
218220
log.push('end');
219221
},
@@ -249,12 +251,11 @@ describe('$$animation', function() {
249251
capturedAnimation = null;
250252

251253
$$animationProvider.drivers.push('interceptorDriver');
252-
$provide.factory('interceptorDriver', function($q, $$animateRunner) {
254+
$provide.factory('interceptorDriver', function($animateRunner) {
253255
return function(details) {
254256
captureLog.push(capturedAnimation = details); //only one param is passed into the driver
255257
return function() {
256-
var runner = $q.when(true);
257-
return $$animateRunner(runner, {
258+
return new $animateRunner({
258259
end: runnerEvent('end'),
259260
cancel: runnerEvent('cancel')
260261
});
@@ -302,24 +303,34 @@ describe('$$animation', function() {
302303
});
303304
});
304305

305-
it('should patch the runner methods to the ones provided by the driver when the animation starts',
306-
inject(function($$animation, $rootScope) {
307-
308-
var runner = $$animation(element, 'someEvent');
309-
expect(runner.end).not.toEqual(noop);
310-
expect(runner.cancel).not.toEqual(noop);
311-
312-
var oldEnd = runner.end;
313-
var oldCancel = runner.cancel;
314-
315-
$rootScope.$digest();
306+
they('should update the runner methods to the ones provided by the driver when the animation starts',
307+
['end', 'cancel'], function(method) {
316308

317-
expect(oldEnd).not.toBe(runner.end);
318-
expect(oldCancel).not.toBe(runner.cancel);
309+
var spy = jasmine.createSpy();
310+
module(function($$animationProvider, $provide) {
311+
$$animationProvider.drivers.push('animalDriver');
312+
$provide.factory('animalDriver', function($animateRunner) {
313+
return function() {
314+
return function() {
315+
var data = {};
316+
data[method] = spy;
317+
return new $animateRunner(data);
318+
};
319+
};
320+
});
321+
});
322+
inject(function($$animation, $rootScope, $rootElement) {
323+
var r1 = $$animation(element, 'someEvent');
324+
r1[method]();
325+
expect(spy).not.toHaveBeenCalled();
326+
$rootScope.$digest(); // this clears the digest which cleans up the mess
319327

320-
runner.end();
321-
expect(runnerLog).toEqual(['end']);
322-
}));
328+
var r2 = $$animation(element, 'otherEvent');
329+
$rootScope.$digest();
330+
r2[method]();
331+
expect(spy).toHaveBeenCalled();
332+
});
333+
});
323334

324335
it('should not start the animation if the element is removed from the DOM before the postDigest kicks in',
325336
inject(function($$animation) {
@@ -559,8 +570,6 @@ describe('$$animation', function() {
559570
var runner2 = $$animation(toElement, 'enter');
560571

561572
expect(runner1).not.toBe(runner2);
562-
expect(runner1.end).not.toBe(runner2.end);
563-
expect(runner1.cancel).not.toBe(runner2.cancel);
564573

565574
fromAnchors[0].attr('ng-animate-ref', 'abc');
566575
toAnchors[0].attr('ng-animate-ref', 'abc');
@@ -661,15 +670,13 @@ describe('$$animation', function() {
661670
element = jqLite('<div></div>');
662671
parent = jqLite('<div></div>');
663672

664-
return function($$animateRunner, $q, $rootElement, $document) {
673+
return function($animateRunner, $q, $rootElement, $document) {
665674
jqLite($document[0].body).append($rootElement);
666675
$rootElement.append(parent);
667676

668677
mockedDriverFn = function(element, method, options, domOperation) {
669678
return function() {
670-
defered = $q.defer();
671-
runner = $$animateRunner(defered.promise);
672-
return runner;
679+
return runner = new $animateRunner();
673680
};
674681
};
675682
};
@@ -686,7 +693,7 @@ describe('$$animation', function() {
686693
expect(element).toHaveClass('temporary');
687694
expect(element).toHaveClass('fudge');
688695

689-
defered.resolve();
696+
runner.end();
690697
$rootScope.$digest();
691698

692699
expect(element).not.toHaveClass('temporary');
@@ -700,7 +707,7 @@ describe('$$animation', function() {
700707
$rootScope.$digest();
701708
expect(element).toHaveClass('ng-animate');
702709

703-
defered.resolve();
710+
runner.end();
704711
$rootScope.$digest();
705712

706713
expect(element).not.toHaveClass('ng-animate');
@@ -719,7 +726,7 @@ describe('$$animation', function() {
719726
$rootScope.$digest();
720727

721728
expect(domOperationFired).toBeFalsy();
722-
defered.resolve();
729+
runner.end();
723730
$rootScope.$digest();
724731

725732
expect(domOperationFired).toBeTruthy();
@@ -762,6 +769,7 @@ describe('$$animation', function() {
762769
$rootScope.$digest(); //runs the animation
763770
$rootScope.$digest(); //flushes the step code
764771
$$rAF.flush(); //runs the $$animation promise
772+
$rootScope.$digest(); //the runner promise
765773

766774
expect(completed).toBe(true);
767775
expect(element.css('background')).toBe('blue');
@@ -813,6 +821,7 @@ describe('$$animation', function() {
813821
$rootScope.$digest(); //runs the animation
814822
$rootScope.$digest(); //flushes the step code
815823
$$rAF.flush(); //runs the $$animation promise
824+
$rootScope.$digest(); //the runner promise
816825

817826
expect(completed).toBe(true);
818827
expect(element).toHaveClass('one');

0 commit comments

Comments
 (0)