您如何在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下按照要求进行操作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。