如何检查我的元素是否已集中在单元测试中 [英] How do I check if my element has been focussed in a unit test
本文介绍了如何检查我的元素是否已集中在单元测试中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下指令来自动聚焦一个字段:
I have the following directive to autofocus a field:
.directive('ngAutofocus', function ($timeout) {
return {
restrict: 'A',
link: function (scope, elm) {
$timeout(function () {
elm[0].focus();
});
}
};
}
我将如何对其进行单元测试?我尝试了几种类似以下选择器的方法,但它们都返回错误或错误:
How would I unit test this? I tried several things like the following selector but they all return errors or false:
console.log($(elm[0]).is(':focus'));
我的单元测试是这样设置的:
My unit test is set up like this:
elm = angular.element('<input type="text" name="textfield1" ng-autofocus>');
$scope.$digest();
$compile(elm)($scope);
推荐答案
我想通了,其实很明显;
I figured it out, and it was pretty obvious actually;
it('should set the focus on timeout', function () {
spyOn(elm[0],'focus');
$timeout.flush();
expect(elm[0].focus).toHaveBeenCalled();
})
我的问题有两方面:
- 我没有调用超时刷新函数,因此没有发生超时,并且
- 我试图查看元素的 focus 属性,而仅查看 focus() 函数的调用更像是单元测试.focus 属性真正属于 e2e 测试领域.
这篇关于如何检查我的元素是否已集中在单元测试中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文