[英]angular-jasmine: testing component that draws dom using timeout
I have the following functionality in my custom directive: 我的自定义指令具有以下功能:
link: function (scope, element) {
var editor = CKEDITOR.inline(element.find('div[contenteditable]')[0], {}
I want to test that the directive is linked and the editor is created under element
using CKEDITOR.inline
method. 我想测试该指令是否已链接,并使用
CKEDITOR.inline
方法在element
下创建了编辑器。 So I have this test: 所以我有这个测试:
it('should compile', function () {
var element = angular.element('<directive></directive>');
var compiled = $compile(element)(scope);
$('body').append(compiled);
expect(element.find('.ckeditor')).toExist();
});
The problem is that CKEDITOR
updates DOM asynchronously: 问题是
CKEDITOR
异步更新DOM:
CKEDITOR.inline = function(element) {
setTimeout(function() {
element.append('<div class=ckeditor></div>');
},0);
}
So my test fails to find the element with the class because it's executed synchronously, while the element is added inside inline
method after the test because of setTimeout
. 因此,我的测试无法通过类找到元素,因为它是同步执行的,而由于
setTimeout
,该元素在测试后添加到inline
方法中。 How do I test it? 我该如何测试?
Specs can become asynchronous : 规范可以变成异步的 :
it('should compile', function (done) {
...
setTimeout(() => {
expect(element.find('.ckeditor')).toExist();
done();
}, 10);
});
Or better, jasmine.clock
can be used: 更好的是,可以使用
jasmine.clock
:
beforeEach(function() {
jasmine.clock().install();
});
afterEach(function() {
jasmine.clock().uninstall();
});
it('should compile', function () {
...
jasmine.clock().tick(10);
expect(element.find('.ckeditor')).toExist();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.