From 32ad2926116e5f52ed04d788220bd389f8d0a4ac Mon Sep 17 00:00:00 2001 From: Matias Niemelä Date: Tue, 3 Sep 2013 13:29:37 -0400 Subject: refactor($animate): use CSS3 transition/animation events instead of $timeouts to track ongoing animations Closes #3629 Closes #3874 --- test/ngAnimate/animateSpec.js | 117 ++++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 55 deletions(-) (limited to 'test/ngAnimate/animateSpec.js') diff --git a/test/ngAnimate/animateSpec.js b/test/ngAnimate/animateSpec.js index 661cc91e..8b88776f 100644 --- a/test/ngAnimate/animateSpec.js +++ b/test/ngAnimate/animateSpec.js @@ -138,7 +138,7 @@ describe("ngAnimate", function() { if($sniffer.transitions) { expect(child.hasClass('ng-enter')).toBe(true); expect(child.hasClass('ng-enter-active')).toBe(true); - $timeout.flush(); + child.triggerHandler('transitionend', { originalEvent: { elapsedTime: 1 } }); } expect(element.contents().length).toBe(1); @@ -154,7 +154,7 @@ describe("ngAnimate", function() { if($sniffer.transitions) { expect(child.hasClass('ng-leave')).toBe(true); expect(child.hasClass('ng-leave-active')).toBe(true); - $timeout.flush(); + child.triggerHandler('transitionend', { originalEvent: { elapsedTime: 1 } }); } expect(element.contents().length).toBe(0); @@ -186,7 +186,7 @@ describe("ngAnimate", function() { if($sniffer.transitions) { expect(child.hasClass('ng-hide-remove')).toBe(true); expect(child.hasClass('ng-hide-remove-active')).toBe(true); - $timeout.flush(); + child.triggerHandler('transitionend', { originalEvent: { elapsedTime: 1 } }); } expect(child.hasClass('ng-hide-remove')).toBe(false); expect(child.hasClass('ng-hide-remove-active')).toBe(false); @@ -202,7 +202,7 @@ describe("ngAnimate", function() { if($sniffer.transitions) { expect(child.hasClass('ng-hide-add')).toBe(true); expect(child.hasClass('ng-hide-add-active')).toBe(true); - $timeout.flush(); + child.triggerHandler('transitionend', { originalEvent: { elapsedTime: 1 } }); } expect(child).toBeHidden(); })); @@ -221,7 +221,7 @@ describe("ngAnimate", function() { expect(child.attr('class')).toContain('ng-enter'); expect(child.attr('class')).toContain('ng-enter-active'); - $timeout.flushNext(1000); + child.triggerHandler('transitionend', { originalEvent: { elapsedTime: 1 } }); //move element.append(after); @@ -230,26 +230,26 @@ describe("ngAnimate", function() { expect(child.attr('class')).toContain('ng-move'); expect(child.attr('class')).toContain('ng-move-active'); - $timeout.flushNext(1000); + child.triggerHandler('transitionend', { originalEvent: { elapsedTime: 1 } }); //hide $animate.addClass(child, 'ng-hide'); expect(child.attr('class')).toContain('ng-hide-add'); expect(child.attr('class')).toContain('ng-hide-add-active'); - $timeout.flushNext(1000); + child.triggerHandler('transitionend', { originalEvent: { elapsedTime: 1 } }); //show $animate.removeClass(child, 'ng-hide'); expect(child.attr('class')).toContain('ng-hide-remove'); expect(child.attr('class')).toContain('ng-hide-remove-active'); - $timeout.flushNext(1000); + child.triggerHandler('transitionend', { originalEvent: { elapsedTime: 1 } }); //leave $animate.leave(child); $rootScope.$digest(); expect(child.attr('class')).toContain('ng-leave'); expect(child.attr('class')).toContain('ng-leave-active'); - $timeout.flushNext(1000); + child.triggerHandler('transitionend', { originalEvent: { elapsedTime: 1 } }); })); it("should not run if animations are disabled", @@ -292,11 +292,10 @@ describe("ngAnimate", function() { if($sniffer.transitions) { expect(element.children().length).toBe(1); //still animating - $timeout.flushNext(1000); - $timeout.flushNext(1000); + child.triggerHandler('transitionend', { originalEvent: { elapsedTime: 1 } }); } - $timeout.flushNext(2000); - $timeout.flushNext(2000); + $timeout.flush(2000); + $timeout.flush(2000); expect(child).toBeShown(); expect(element.children().length).toBe(0); @@ -310,9 +309,9 @@ describe("ngAnimate", function() { child.addClass('custom-delay ng-hide'); $animate.removeClass(child, 'ng-hide'); if($sniffer.transitions) { - $timeout.flushNext(1000); + child.triggerHandler('transitionend', { originalEvent: { elapsedTime: 1 } }); } - $timeout.flushNext(2000); + $timeout.flush(2000); $animate.addClass(child, 'ng-hide'); @@ -352,8 +351,8 @@ describe("ngAnimate", function() { inject(function($animate, $rootScope, $compile, $sniffer, $timeout) { $animate.addClass(element, 'custom-delay custom-long-delay'); - $timeout.flushNext(2000); - $timeout.flushNext(20000); + $timeout.flush(2000); + $timeout.flush(20000); expect(element.hasClass('custom-delay')).toBe(true); expect(element.hasClass('custom-long-delay')).toBe(true); })); @@ -374,10 +373,10 @@ describe("ngAnimate", function() { $animate.removeClass(element, 'ng-hide'); if($sniffer.transitions) { - $timeout.flushNext(1000); + element.triggerHandler('transitionend', { originalEvent: { elapsedTime: 1 } }); } - $timeout.flushNext(2000); - $timeout.flushNext(20000); + $timeout.flush(2000); + $timeout.flush(20000); expect(element.hasClass('custom-delay')).toBe(true); expect(element.hasClass('custom-delay-add')).toBe(false); @@ -417,7 +416,7 @@ describe("ngAnimate", function() { $animate.removeClass(element, 'ng-hide'); if ($sniffer.animations) { - $timeout.flushNext(4000); + element.triggerHandler('animationend', { originalEvent: { elapsedTime: 4 } }); } expect(element).toBeShown(); })); @@ -440,7 +439,7 @@ describe("ngAnimate", function() { $animate.removeClass(element, 'ng-hide'); if ($sniffer.animations) { - $timeout.flushNext(6000); + element.triggerHandler('animationend', { originalEvent: { elapsedTime: 6 } }); } expect(element).toBeShown(); })); @@ -463,12 +462,12 @@ describe("ngAnimate", function() { $animate.removeClass(element, 'ng-hide'); if ($sniffer.animations) { - $timeout.flushNext(2000); + element.triggerHandler('animationend', { originalEvent: { elapsedTime: 2 } }); } expect(element).toBeShown(); })); - it("should consider the animation delay is provided", + it("should not consider the animation delay is provided", inject(function($animate, $rootScope, $compile, $sniffer, $timeout) { var style = 'animation-duration: 2s;' + @@ -488,7 +487,7 @@ describe("ngAnimate", function() { $animate.removeClass(element, 'ng-hide'); if ($sniffer.transitions) { - $timeout.flushNext(20000); + element.triggerHandler('animationend', { originalEvent: { elapsedTime: 10 } }); } expect(element).toBeShown(); })); @@ -534,7 +533,7 @@ describe("ngAnimate", function() { if($sniffer.animations) { //cleanup some pending animations expect(element.hasClass('ng-hide-add')).toBe(true); expect(element.hasClass('ng-hide-add-active')).toBe(true); - $timeout.flushNext(2000); + element.triggerHandler('animationend', { originalEvent: { elapsedTime: 2 } }); } expect(element.hasClass('ng-hide-remove-active')).toBe(false); @@ -567,7 +566,7 @@ describe("ngAnimate", function() { $animate.removeClass(element, 'ng-hide'); if ($sniffer.transitions) { - $timeout.flushNext(1000); + element.triggerHandler('transitionend', { originalEvent: { elapsedTime: 1 } }); } expect(element).toBeShown(); })); @@ -588,7 +587,9 @@ describe("ngAnimate", function() { $animate.removeClass(element, 'ng-hide'); if ($sniffer.transitions) { expect(element).toBeHidden(); - $timeout.flushNext(2000); + element.triggerHandler('transitionend', { originalEvent: { elapsedTime: 1 } }); + element.triggerHandler('transitionend', { originalEvent: { elapsedTime: 1 } }); + element.triggerHandler('transitionend', { originalEvent: { elapsedTime: 2 } }); } expect(element).toBeShown(); })); @@ -610,7 +611,7 @@ describe("ngAnimate", function() { element.addClass('ng-hide'); $animate.removeClass(element, 'ng-hide'); - $timeout.flushNext(0); + $timeout.flush(0); expect(element).toBeShown(); $animate.enabled(true); @@ -619,12 +620,14 @@ describe("ngAnimate", function() { $animate.removeClass(element, 'ng-hide'); if ($sniffer.transitions) { - $timeout.flushNext(3000); + element.triggerHandler('transitionend', { originalEvent: { elapsedTime: 0 } }); + element.triggerHandler('transitionend', { originalEvent: { elapsedTime: 1 } }); + element.triggerHandler('transitionend', { originalEvent: { elapsedTime: 1 } }); } expect(element).toBeShown(); })); - it("should select the highest duration and delay", + it("should animate for the highest duration", inject(function($animate, $rootScope, $compile, $sniffer, $timeout) { var style = 'transition:1s linear all 2s;' + vendorPrefix + 'transition:1s linear all 2s;' + @@ -641,7 +644,7 @@ describe("ngAnimate", function() { $animate.removeClass(element, 'ng-hide'); if ($sniffer.transitions) { - $timeout.flushNext(11000); + element.triggerHandler('animationend', { originalEvent: { elapsedTime: 10 } }); } expect(element).toBeShown(); })); @@ -661,7 +664,7 @@ describe("ngAnimate", function() { if($sniffer.transitions) { expect(element.hasClass('ng-hide-remove')).toBe(true); expect(element.hasClass('ng-hide-remove-active')).toBe(true); - $timeout.flushNext(1000); + element.triggerHandler('transitionend', { originalEvent: { elapsedTime: 1 } }); } expect(element.hasClass('ng-hide-remove')).toBe(false); expect(element.hasClass('ng-hide-remove-active')).toBe(false); @@ -678,37 +681,37 @@ describe("ngAnimate", function() { describe('animation evaluation', function () { it('should re-evaluate the CSS classes for an animation each time', - inject(function($animate, $rootScope, $sniffer, $rootElement, $timeout) { + inject(function($animate, $rootScope, $sniffer, $rootElement, $timeout, $compile) { - ss.addRule('.ng-enter', 'transition:22s linear all;' + - vendorPrefix + 'transition:22s linear all'); - ss.addRule('.ng-enter', 'transition:22s linear all;' + + ss.addRule('.abc', 'transition:22s linear all;' + vendorPrefix + 'transition:22s linear all'); + ss.addRule('.xyz', 'transition:11s linear all;' + + vendorPrefix + 'transition:11s linear all'); - var parent = jqLite('