@@ -12,6 +12,16 @@ describe("ngAnimate $animateCss", function() {
12
12
: expect ( className ) . not . toMatch ( regex ) ;
13
13
}
14
14
15
+ function keyframeProgress ( element , duration , delay ) {
16
+ browserTrigger ( element , 'animationend' ,
17
+ { timeStamp : Date . now ( ) + ( ( delay || 1 ) * 1000 ) , elapsedTime : duration } ) ;
18
+ }
19
+
20
+ function transitionProgress ( element , duration , delay ) {
21
+ browserTrigger ( element , 'transitionend' ,
22
+ { timeStamp : Date . now ( ) + ( ( delay || 1 ) * 1000 ) , elapsedTime : duration } ) ;
23
+ }
24
+
15
25
var fakeStyle = {
16
26
color : 'blue'
17
27
} ;
@@ -355,16 +365,6 @@ describe("ngAnimate $animateCss", function() {
355
365
assert . toHaveClass ( 'ng-enter-active' ) ;
356
366
}
357
367
358
- function keyframeProgress ( element , duration , delay ) {
359
- browserTrigger ( element , 'animationend' ,
360
- { timeStamp : Date . now ( ) + ( ( delay || 1 ) * 1000 ) , elapsedTime : duration } ) ;
361
- }
362
-
363
- function transitionProgress ( element , duration , delay ) {
364
- browserTrigger ( element , 'transitionend' ,
365
- { timeStamp : Date . now ( ) + ( ( delay || 1 ) * 1000 ) , elapsedTime : duration } ) ;
366
- }
367
-
368
368
beforeEach ( inject ( function ( $rootElement , $document ) {
369
369
element = jqLite ( '<div></div>' ) ;
370
370
$rootElement . append ( element ) ;
@@ -1404,6 +1404,138 @@ describe("ngAnimate $animateCss", function() {
1404
1404
expect ( count . stagger ) . toBe ( 2 ) ;
1405
1405
} ) ) ;
1406
1406
} ) ;
1407
+
1408
+ describe ( 'transitionend/animationend event listeners' , function ( ) {
1409
+ var element , elementOnSpy , elementOffSpy , progress ;
1410
+
1411
+ function setStyles ( event ) {
1412
+ switch ( event ) {
1413
+ case TRANSITIONEND_EVENT :
1414
+ ss . addRule ( '.ng-enter' , 'transition: 10s linear all;' ) ;
1415
+ progress = transitionProgress ;
1416
+ break ;
1417
+ case ANIMATIONEND_EVENT :
1418
+ ss . addRule ( '.ng-enter' , '-webkit-animation: animation 10s;' +
1419
+ 'animation: animation 10s;' ) ;
1420
+ progress = keyframeProgress ;
1421
+ break ;
1422
+ }
1423
+ }
1424
+
1425
+ beforeEach ( inject ( function ( $rootElement , $document ) {
1426
+ element = jqLite ( '<div></div>' ) ;
1427
+ $rootElement . append ( element ) ;
1428
+ jqLite ( $document [ 0 ] . body ) . append ( $rootElement ) ;
1429
+
1430
+ elementOnSpy = spyOn ( element , 'on' ) . andCallThrough ( ) ;
1431
+ elementOffSpy = spyOn ( element , 'off' ) . andCallThrough ( ) ;
1432
+ } ) ) ;
1433
+
1434
+ they ( 'should remove the $prop event listeners on cancel' ,
1435
+ [ TRANSITIONEND_EVENT , ANIMATIONEND_EVENT ] , function ( event ) {
1436
+ inject ( function ( $animateCss ) {
1437
+
1438
+ setStyles ( event ) ;
1439
+
1440
+ var animator = $animateCss ( element , {
1441
+ event : 'enter' ,
1442
+ structural : true
1443
+ } ) ;
1444
+
1445
+ var runner = animator . start ( ) ;
1446
+ triggerAnimationStartFrame ( ) ;
1447
+
1448
+ expect ( elementOnSpy ) . toHaveBeenCalledOnce ( ) ;
1449
+ expect ( elementOnSpy . mostRecentCall . args [ 0 ] ) . toBe ( event ) ;
1450
+
1451
+ runner . cancel ( ) ;
1452
+
1453
+ expect ( elementOffSpy ) . toHaveBeenCalledOnce ( ) ;
1454
+ expect ( elementOffSpy . mostRecentCall . args [ 0 ] ) . toBe ( event ) ;
1455
+ } ) ;
1456
+ } ) ;
1457
+
1458
+ they ( "should remove the $prop event listener when the animation is closed" ,
1459
+ [ TRANSITIONEND_EVENT , ANIMATIONEND_EVENT ] , function ( event ) {
1460
+ inject ( function ( $animateCss ) {
1461
+
1462
+ setStyles ( event ) ;
1463
+
1464
+ var animator = $animateCss ( element , {
1465
+ event : 'enter' ,
1466
+ structural : true
1467
+ } ) ;
1468
+
1469
+ var runner = animator . start ( ) ;
1470
+ triggerAnimationStartFrame ( ) ;
1471
+
1472
+ expect ( elementOnSpy ) . toHaveBeenCalledOnce ( ) ;
1473
+ expect ( elementOnSpy . mostRecentCall . args [ 0 ] ) . toBe ( event ) ;
1474
+
1475
+ progress ( element , 10 ) ;
1476
+
1477
+ expect ( elementOffSpy ) . toHaveBeenCalledOnce ( ) ;
1478
+ expect ( elementOffSpy . mostRecentCall . args [ 0 ] ) . toBe ( event ) ;
1479
+ } ) ;
1480
+ } ) ;
1481
+
1482
+ they ( "should remove the $prop event listener when the closing timeout occurs" ,
1483
+ [ TRANSITIONEND_EVENT , ANIMATIONEND_EVENT ] , function ( event ) {
1484
+ inject ( function ( $animateCss , $timeout ) {
1485
+
1486
+ setStyles ( event ) ;
1487
+
1488
+ var animator = $animateCss ( element , {
1489
+ event : 'enter' ,
1490
+ structural : true
1491
+ } ) ;
1492
+
1493
+ animator . start ( ) ;
1494
+ triggerAnimationStartFrame ( ) ;
1495
+
1496
+ expect ( elementOnSpy ) . toHaveBeenCalledOnce ( ) ;
1497
+ expect ( elementOnSpy . mostRecentCall . args [ 0 ] ) . toBe ( event ) ;
1498
+
1499
+ $timeout . flush ( 15000 ) ;
1500
+
1501
+ expect ( elementOffSpy ) . toHaveBeenCalledOnce ( ) ;
1502
+ expect ( elementOffSpy . mostRecentCall . args [ 0 ] ) . toBe ( event ) ;
1503
+ } ) ;
1504
+ } ) ;
1505
+
1506
+ they ( "should not add or remove $prop event listeners when no animation styles are detected" ,
1507
+ [ TRANSITIONEND_EVENT , ANIMATIONEND_EVENT ] , function ( event ) {
1508
+ inject ( function ( $animateCss , $timeout ) {
1509
+
1510
+ progress = event === TRANSITIONEND_EVENT ? transitionProgress : keyframeProgress ;
1511
+
1512
+ // Make sure other event listeners are not affected
1513
+ var otherEndSpy = jasmine . createSpy ( 'otherEndSpy' ) ;
1514
+ element . on ( event , otherEndSpy ) ;
1515
+
1516
+ expect ( elementOnSpy ) . toHaveBeenCalledOnce ( ) ;
1517
+ elementOnSpy . reset ( ) ;
1518
+
1519
+ var animator = $animateCss ( element , {
1520
+ event : 'enter' ,
1521
+ structural : true
1522
+ } ) ;
1523
+
1524
+ expect ( animator . $$willAnimate ) . toBeFalsy ( ) ;
1525
+
1526
+ // This will close the animation because no styles have been detected
1527
+ var runner = animator . start ( ) ;
1528
+ triggerAnimationStartFrame ( ) ;
1529
+
1530
+ expect ( elementOnSpy ) . not . toHaveBeenCalled ( ) ;
1531
+ expect ( elementOffSpy ) . not . toHaveBeenCalled ( ) ;
1532
+
1533
+ progress ( element , 10 ) ;
1534
+ expect ( otherEndSpy ) . toHaveBeenCalledOnce ( ) ;
1535
+ } ) ;
1536
+ } ) ;
1537
+
1538
+ } ) ;
1407
1539
} ) ;
1408
1540
1409
1541
it ( 'should avoid applying the same cache to an element a follow-up animation is run on the same element' ,
0 commit comments