简体   繁体   English

角茉莉花:使用超时绘制dom的测试组件

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM