Maa*_*ten 18 unit-testing jasmine angularjs
我有以下指令来自动对焦字段:
.directive('ngAutofocus', function ($timeout) {
return {
restrict: 'A',
link: function (scope, elm) {
$timeout(function () {
elm[0].focus();
});
}
};
}
Run Code Online (Sandbox Code Playgroud)
我该如何对此进行单元测试?我尝试了几个像下面的选择器,但它们都返回错误或错误:
console.log($(elm[0]).is(':focus'));
Run Code Online (Sandbox Code Playgroud)
我的单元测试设置如下:
elm = angular.element('<input type="text" name="textfield1" ng-autofocus>');
$scope.$digest();
$compile(elm)($scope);
Run Code Online (Sandbox Code Playgroud)
Maa*_*ten 34
我明白了,实际上很明显;
it('should set the focus on timeout', function () {
spyOn(elm[0],'focus');
$timeout.flush();
expect(elm[0].focus).toHaveBeenCalled();
})
Run Code Online (Sandbox Code Playgroud)
我的问题有两方面:
您可以用来document.activeElement检查焦点。唯一的缺点是需要将 HTML 添加到文档正文中才能正常工作。
https://developer.mozilla.org/en-US/docs/Web/API/Document/activeElement
| 归档时间: |
|
| 查看次数: |
17793 次 |
| 最近记录: |