gpt4 book ai didi

angularjs - 如何检查我的元素是否已集中在单元测试中

转载 作者:行者123 更新时间:2023-12-03 13:23:55 24 4
gpt4 key购买 nike

我有以下指令来自动聚焦字段:

.directive('ngAutofocus', function ($timeout) {
return {
restrict: 'A',
link: function (scope, elm) {
$timeout(function () {
elm[0].focus();
});
}
};
}


我将如何对此进行单元测试?我尝试了以下选择器之类的几种方法,但是它们都返回错误或false:

console.log($(elm[0]).is(':focus'));


我的单元测试设置如下:

elm = angular.element('<input type="text"  name="textfield1" ng-autofocus>');
$scope.$digest();
$compile(elm)($scope);

最佳答案

我想通了,实际上这很明显。

it('should set the focus on timeout', function () {
spyOn(elm[0],'focus');
$timeout.flush();
expect(elm[0].focus).toHaveBeenCalled();
})


我的问题有两个:


我没有调用超时刷新功能,所以没有发生超时,并且
我试图查看元素的focus属性,而仅关注focus()函数的调用更像是单元测试。 focus属性确实属于e2e测试领域。

关于angularjs - 如何检查我的元素是否已集中在单元测试中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22330098/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com