From 04f1da2f18cae75d2037ea0a34ae6e53323f8e24 Mon Sep 17 00:00:00 2001
From: sreeramu <sreeramu@gmail.com>
Date: Mon, 6 Jul 2015 23:19:24 +0800
Subject: [PATCH] Fix($animate): ensure that class-based animations are
 properly applied when cancelled

Instead of merging existing animation option to new animation options we can merge in reverse and utilize old animation runner.
---
 src/ngAnimate/animateQueue.js |  5 +++--
 test/ngAnimate/animateSpec.js | 18 ++++++++++++++++++
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/src/ngAnimate/animateQueue.js b/src/ngAnimate/animateQueue.js
index 374377de8b6e..afe45bbb3f26 100644
--- a/src/ngAnimate/animateQueue.js
+++ b/src/ngAnimate/animateQueue.js
@@ -330,8 +330,9 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
             // 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);
+            // this will merge the new animation options into existing animation options
+            mergeAnimationOptions(element, existingAnimation.options, newAnimation.options);
+            return existingAnimation.runner;
           }
         } else {
           // a joined animation means that this animation will take over the existing one
diff --git a/test/ngAnimate/animateSpec.js b/test/ngAnimate/animateSpec.js
index 61810a891819..c1fe37adb366 100644
--- a/test/ngAnimate/animateSpec.js
+++ b/test/ngAnimate/animateSpec.js
@@ -1206,6 +1206,24 @@ describe("animations", function() {
         expect(r1).toBe(r2);
         expect(r2).toBe(r3);
       }));
+
+      it('should not skip or miss the animations when animations are executed sequential',
+        inject(function($animate, $rootScope, $$rAF, $rootElement) {
+
+        element = jqLite('<div></div>');
+
+        $rootElement.append(element);
+
+        $animate.addClass(element, 'rclass');
+        $animate.removeClass(element, 'rclass');
+        $animate.addClass(element, 'rclass');
+        $animate.removeClass(element, 'rclass');
+
+        $rootScope.$digest();
+        $$rAF.flush();
+
+        expect(element).not.toHaveClass('rclass');
+      }));
     });
   });