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

Commit 4dada5c

Browse files
committed
test(): simplify the event listener tests
1 parent 970e71b commit 4dada5c

File tree

1 file changed

+64
-85
lines changed

1 file changed

+64
-85
lines changed

test/ngAnimate/animateCssSpec.js

+64-85
Original file line numberDiff line numberDiff line change
@@ -1406,122 +1406,101 @@ describe("ngAnimate $animateCss", function() {
14061406
});
14071407

14081408
describe('transitionend/animationend event listeners', function() {
1409-
var element, callLog, proxyListener, origListener, type, progress;
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', ANIMATION_PROP + ':animation 10s;');
1419+
progress = keyframeProgress;
1420+
break;
1421+
}
1422+
}
14101423

14111424
beforeEach(inject(function($rootElement, $document) {
1412-
14131425
element = jqLite('<div></div>');
14141426
$rootElement.append(element);
14151427
jqLite($document[0].body).append($rootElement);
14161428

1417-
callLog = {};
1418-
1419-
var origOn = element.on,
1420-
origOff = element.off;
1421-
1422-
proxyListener = function() {
1423-
callLog[type] = callLog[type]++ || 1;
1424-
origListener.apply(this, arguments);
1425-
};
1426-
1427-
// Overwrite the .on function to log calls to the event listeners
1428-
element.on = function() {
1429-
origListener = arguments[1];
1430-
type = arguments[0];
1431-
origOn.call(this, arguments[0], proxyListener);
1432-
};
1433-
1434-
// Make sure the .off function removes the proxyListener
1435-
element.off = function() {
1436-
origOff.call(this, arguments[0], proxyListener);
1437-
};
1438-
1429+
elementOnSpy = spyOn(element, 'on').andCallThrough();
1430+
elementOffSpy = spyOn(element, 'off').andCallThrough();
14391431
}));
14401432

14411433
they('should remove the $prop event listeners on cancel',
1442-
[TRANSITIONEND_EVENT, ANIMATIONEND_EVENT],
1443-
function(event) {inject(function($animateCss) {
1444-
1445-
switch (event) {
1446-
case TRANSITIONEND_EVENT:
1447-
progress = bind(this, transitionProgress, element, 10);
1448-
break;
1449-
case ANIMATIONEND_EVENT:
1450-
progress = bind(this, keyframeProgress, element, 10);
1451-
break;
1452-
}
1434+
[TRANSITIONEND_EVENT, ANIMATIONEND_EVENT], function(event) {
1435+
inject(function($animateCss) {
14531436

1454-
var animator = $animateCss(element, {
1455-
duration: 10,
1456-
to: { 'background': 'red' }
1457-
});
1437+
setStyles(event);
14581438

1459-
var runner = animator.start();
1460-
triggerAnimationStartFrame();
1439+
var animator = $animateCss(element, {
1440+
event: 'enter',
1441+
structural: true
1442+
});
14611443

1462-
runner.cancel();
1444+
var runner = animator.start();
1445+
triggerAnimationStartFrame();
14631446

1464-
// Trigger an end event
1465-
progress();
1447+
expect(elementOnSpy).toHaveBeenCalledOnce();
1448+
expect(elementOnSpy.mostRecentCall.args[0]).toBe(event);
14661449

1467-
expect(callLog[event]).toBeFalsy();
1468-
});
1450+
runner.cancel();
1451+
1452+
expect(elementOffSpy).toHaveBeenCalledOnce();
1453+
expect(elementOffSpy.mostRecentCall.args[0]).toBe(event);
1454+
});
14691455
});
14701456

14711457
they("should remove the $prop event listener when the animation is closed",
14721458
[TRANSITIONEND_EVENT, ANIMATIONEND_EVENT], function(event) {
1473-
inject(function($animateCss) {
1459+
inject(function($animateCss) {
14741460

1475-
switch (event) {
1476-
case TRANSITIONEND_EVENT:
1477-
ss.addRule('.ng-enter', TRANSITION_PROP + '1s linear all;' +
1478-
TRANSITION_PROP + '-duration:10s;');
1479-
1480-
progress = bind(this, transitionProgress, element, 10);
1481-
break;
1482-
case ANIMATIONEND_EVENT:
1483-
ss.addRule('.ng-enter', ANIMATION_PROP + ':animation 10s;');
1484-
progress = bind(this, keyframeProgress, element, 10);
1485-
break;
1486-
}
1461+
setStyles(event);
14871462

1488-
var animator = $animateCss(element, {
1489-
event: 'enter',
1490-
structural: true
1491-
});
1463+
var animator = $animateCss(element, {
1464+
event: 'enter',
1465+
structural: true
1466+
});
14921467

1493-
var runner = animator.start();
1494-
triggerAnimationStartFrame();
1468+
var runner = animator.start();
1469+
triggerAnimationStartFrame();
14951470

1496-
progress();
1497-
expect(element).not.toHaveClass('ng-enter ng-enter-active');
1471+
expect(elementOnSpy).toHaveBeenCalledOnce();
1472+
expect(elementOnSpy.mostRecentCall.args[0]).toBe(event);
14981473

1499-
// Trigger another end event
1500-
progress();
1474+
progress(element, 10);
15011475

1502-
expect(callLog[event]).toBe(1);
1503-
});
1476+
expect(elementOffSpy).toHaveBeenCalledOnce();
1477+
expect(elementOffSpy.mostRecentCall.args[0]).toBe(event);
1478+
});
15041479
});
15051480

1506-
it("should remove the transitionend event listener when the closing timeout occurs",
1507-
inject(function($animateCss, $document, $rootElement, $timeout) {
1481+
they("should remove the $prop event listener when the closing timeout occurs",
1482+
[TRANSITIONEND_EVENT, ANIMATIONEND_EVENT], function(event) {
1483+
inject(function($animateCss, $timeout) {
15081484

1509-
ss.addRule('.ng-enter', 'transition:10s linear all;');
1485+
setStyles(event);
15101486

1511-
var animator = $animateCss(element, {
1512-
event: 'enter',
1513-
structural: true
1514-
});
1487+
var animator = $animateCss(element, {
1488+
event: 'enter',
1489+
structural: true
1490+
});
15151491

1516-
animator.start();
1517-
triggerAnimationStartFrame();
1518-
$timeout.flush(15000);
1492+
animator.start();
1493+
triggerAnimationStartFrame();
15191494

1520-
// Force an transitionend event
1521-
transitionProgress(element, 10);
1495+
expect(elementOnSpy).toHaveBeenCalledOnce();
1496+
expect(elementOnSpy.mostRecentCall.args[0]).toBe(event);
15221497

1523-
expect(callLog[TRANSITIONEND_EVENT]).toBeFalsy();
1524-
}));
1498+
$timeout.flush(15000);
1499+
1500+
expect(elementOffSpy).toHaveBeenCalledOnce();
1501+
expect(elementOffSpy.mostRecentCall.args[0]).toBe(event);
1502+
});
1503+
});
15251504
});
15261505
});
15271506

0 commit comments

Comments
 (0)