加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

测试一个AngularJS指令

发布时间:2020-12-17 08:19:03 所属栏目:安全 来源:网络整理
导读:您如何在AngularJS指令中测试焦点?我期望以下工作: describe('focus test',function(){ it('should focus element',function(){ var element = $('input type="text" /'); // Append to body because otherwise it can't be foccused element.appendTo(doc
您如何在AngularJS指令中测试焦点?我期望以下工作:
describe('focus test',function(){
    it('should focus element',function(){
        var element = $('<input type="text" />');
        // Append to body because otherwise it can't be foccused
        element.appendTo(document.body);
        element.focus();
        expect(element.is(':focus')).toBe(true);
    });
});

但是,这仅适用于IE,它在Firefox和Chrome中失败

更新:
@S McCrohan的解决方案。使用这个我创建了一个’toHaveFocus’匹配器:

beforeEach(function(){
    this.addMatchers({
        toHaveFocus: function(){
            this.message = function(){
                return 'Expected '' + angular.mock.dump(this.actual) + '' to have focus';
            };

            return document.activeElement === this.actual[0];
        }
    });
});

其用法如下:

expect(myElement).toHaveFocus();

请注意,对于与焦点相关的测试,已编译的元素必须附加到DOM,可以这样做:

myElement.appendTo(document.body);
尝试’document.activeElement’而不是’:focus’。我没有通过业务测试它,但$(‘document.activeElement’)在标准jQuery下按照要求进行操作。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读