Angular.js单元测试不调用$animate.enter回调
Angular.js unit test does not call $animate.enter callback
我编写了一个指令,该指令将有条件地添加一个包装器元素,该元素是我根据Angular的ngIf指令建模的。该指令在生产中运行时效果很好,但在尝试添加单元测试时,$animate.enter函数从不调用我的回调函数。这会导致我的所有单元测试在假设包装器不在那里时失败。
我使用Angular.js 1.2.16版本,并加载ngMock和ngAnimate进行单元测试。该代码会触发ngAnimate enter函数,但之后它从不触发回调。
您可以在此处查看代码,只需取消对appSpec.js脚本标记的注释,该指令就不再有效。
现在有人知道如何在单元测试中触发$animate.enter来调用我的回调函数吗?
addWrapperIf.js
angular.module('myModule', ['ngAnimate'])
.directive('addWrapperIf', ['$animate', function($animate) {
return {
transclude: 'element',
priority: 1000,
restrict: 'A',
compile: function (element, attr, transclude) {
return function ($scope, $element, $attr) {
var childElement, childScope;
$scope.$watch($attr.addWrapperIf, function addWrapperIfWatchAction(value) {
if (childElement) {
$animate.leave(childElement);
childElement = undefined;
}
if (childScope) {
childScope.$destroy();
childScope = undefined;
}
// add the wrapper
if (value) {
childScope = $scope.$new();
transclude(childScope, function (clone) {
childElement = clone
$animate.enter(clone, $element.parent(), $element);
});
}
// remove the wrapper
else {
childScope = $scope.$new();
transclude(childScope, function (clone) {
$animate.enter(clone, $element.parent(), $element, function() {
childElement = clone.contents();
clone.replaceWith(clone.contents());
});
});
}
});
}
}
};
}]);
addWrapperInfoSpec.js
var expect = chai.expect;
describe('addWrapperIf', function () {
var $template;
var $compile;
var $scope;
beforeEach(window.module('myModule'));
beforeEach(inject(function(_$compile_, $rootScope){
$compile = _$compile_;
$scope = $rootScope.$new();
}));
function compileDirective(template) {
$template = $compile(template)($scope)[0];
$scope.$apply();
}
it('should output the correct values with default options', function() {
compileDirective('<div add-wrapper-if="false"><span>child</span></div>');
console.log($template); // <div add-wrapper-if="false"><span>child</span></div>
});
});
所以我想好了你必须做什么。我深入研究了代码,发现在ngAnimate内部,它将回调函数推送到$$asyncCallback
。$$asyncCallback
有一个flush
函数,它将调用推到它上面的任何函数。要让$animate.enter
触发回调,你必须将$$asyncCallback
注入到单元测试中,然后调用$$asyncCallback.flush()
。这将运行您的回调函数。
你可以在这个Plunker中看到这一点。
相关文章:
- 可以'使用jQuery animate时无法看到动画
- 如何使用jquery animate来回移动多个元素
- 当我在form_for中的text_field_tag中点击Enter时,如何禁止发出POST请求
- jQuery animate()函数没有't设置动画
- JavaScript:在调用函数的文本输入上按enter键
- JQuery.animate();不以固定高度工作
- $animate addClass在angular 1.3中不起作用
- JS异常:animate不是一个函数
- 关于使用Animate.css向和项添加和删除动画类的问题
- 有没有什么方法可以停止Jquery中的animate(也可以停止完整的回调函数)
- D3js如何在.enter上下文中在同一级别附加两个子级
- 有没有一种方法可以用步骤递归调用jQuery animate()
- 当阻止Enter键提交AJAX表单时,关注下一个输入是't工作
- jquery animate无法正常工作
- Can't get the tail to wag (animate) in three.js?
- 添加animate.css类,然后使用remove移除元素
- 如何编写脚本并在网页上按Enter键
- jquery Animate Number reanimate on screen enter
- Angular.js单元测试不调用$animate.enter回调
- 如何以多种方式让Angular的ng-switch animate(enter/leave)