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

fix(ngAnimate): ensure that repeated structural calls during pre-digest function #11895

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/ngAnimate/animateQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
structural: isStructural,
element: element,
event: event,
close: close,
options: options,
runner: runner
};
Expand All @@ -311,8 +312,17 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
var cancelAnimationFlag = isAllowed('cancel', element, newAnimation, existingAnimation);
if (cancelAnimationFlag) {
if (existingAnimation.state === RUNNING_STATE) {
// this will end the animation right away and it is safe
// to do so since the animation is already running and the
// runner callback code will run in async
existingAnimation.runner.end();
} else if (existingAnimation.structural) {
// this means that the animation is queued into a digest, but
// hasn't started yet. Therefore it is safe to run the close
// method which will call the runner methods in async.
existingAnimation.close();
} else {
// this will merge the existing animation options into this new follow-up animation
mergeAnimationOptions(element, newAnimation.options, existingAnimation.options);
}
} else {
Expand Down
28 changes: 25 additions & 3 deletions test/ngAnimate/animateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,20 @@ describe("animations", function() {
expect(enterComplete).toBe(true);
}));

it('should cancel the previous structural animation if a follow-up structural animation takes over before the postDigest',
inject(function($animate, $$rAF) {

var enterComplete = false;
$animate.enter(element, parent).done(function() {
enterComplete = true;
});

expect(enterComplete).toBe(false);
$animate.leave(element);
$$rAF.flush();
expect(enterComplete).toBe(true);
}));

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use spies:

      it('should cancel the previous structural animation if a follow-up structural animation takes over before the postDigest',
        inject(function($animate, $$rAF) {

        var enterDone = jasmine.createSpy('enter animation done');
        $animate.enter(element, parent).done(enterDone);
        expect(enterDone).not.toHaveBeenCalled();

        $animate.leave(element);
        $$rAF.flush();
        expect(enterDone).toHaveBeenCalled();
      }));

it('should skip the class-based animation entirely if there is an active structural animation',
inject(function($animate, $rootScope) {

Expand Down Expand Up @@ -1152,12 +1166,20 @@ describe("animations", function() {
it('class-based animations, however it should also cancel former structural animations in the process',
inject(function($animate, $rootScope) {

element.addClass('green');
element.addClass('green lime');

$animate.enter(element, parent);
$animate.addClass(element, 'red');
$animate.removeClass(element, 'green');

$animate.leave(element);
$animate.addClass(element, 'pink');
$animate.removeClass(element, 'lime');

expect(element).toHaveClass('red');
expect(element).not.toHaveClass('green');
expect(element).not.toHaveClass('pink');
expect(element).toHaveClass('lime');

$rootScope.$digest();

Expand All @@ -1168,8 +1190,8 @@ describe("animations", function() {
expect(element.parent()[0]).toEqual(parent[0]);

options = capturedAnimation[2];
expect(options.addClass).toEqual('red');
expect(options.removeClass).toEqual('green');
expect(options.addClass).toEqual('pink');
expect(options.removeClass).toEqual('lime');
}));

it('should retain the instance to the very first runner object when multiple element-level animations are issued',
Expand Down