首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Jasmin-2测试异步DOM/CSS更改--从waitsFor迁移到done()

使用Jasmin-2测试异步DOM/CSS更改--从waitsFor迁移到done()
EN

Stack Overflow用户
提问于 2014-02-20 15:05:02
回答 1查看 1.4K关注 0票数 3

我目前正在更新我的弹出/工具提示库的测试套件,从Jasminin1.3升级到Jasminine2。

该库主要以异步方式运行,因为它广泛使用CSS转换。例如,

  1. .show()一个工具提示,
  2. 它启动CSS转换,
  3. 等待他们完成
  4. 如果有必要,在工具提示布局和完全可见后重新定位。

正因为如此,我的规范中有很大一部分目前采用以下形式:

代码语言:javascript
复制
it("should update the CSS classes after repositioning the tooltip due to lack of space", function() {
  var popoverIsVisible = false;

  // instantiate a popover with specific settings
  runs(function() {
    instance = new Popover(domElement, {position: "left center", alternativePositions: ["right center"]});

    instance.addCallback('afterShow', function() { popoverIsVisible = true; });

    instance.show();
  });

 // the afterShow callback is run after the CSS transitions 
 // have run and the tooltip is visible
  waitsFor(function() {
    return popoverIsVisible;
  }, "The afterShow callback should have triggered", 100);

  runs(function() {
    expect(instance.popover().hasClass('horizontal-right')).toBeTruthy();
  });
});

在我看来,为了过渡到Jasmine 2的done()回调样式,我必须将每个规范包装在它自己的describe块中,并将库设置部分移动到beforeEach中。

代码语言:javascript
复制
describe("...", function() {
  beforeEach(function(done) {
    var instance = new Popover(domElement, {position: "left center", alternativePositions: ["right center"]});

    instance.addCallback('afterShow', function() { done(); });

    instance.show();
  });

  it("should update the CSS classes when repositioning the tooltip due to lack of space", function() {
    expect(instance.popover().hasClass('horizontal-right')).toBeTruthy();
  });
});

使用一个单独的describe块和每个规范的beforeEach对我来说都是错误的,就好像我在对付茉莉花一样。我是不是遗漏了什么?有更好的方法来构造我的规格吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-24 04:11:07

如果您不希望为每个单独的describe/beforeEach设置单独的it,那么您还应该能够在异步函数的回调中得到您的期望,并在此之后为it调用done。类似于:

代码语言:javascript
复制
describe("...", function() {
  it("should update the CSS classes when repositioning the tooltip due to lack of space", function(done) {
    var instance = new Popover(domElement, {position: "left center", alternativePositions: ["right center"]});

    instance.addCallback('afterShow', function() {
      expect(instance.popover().hasClass('horizontal-right')).toBeTruthy();
      done();
    });

    instance.show();
  });
});

由于it是异步的,如果没有及时调用done回调,茉莉花就会失败,因此,如果从未调用来自异步函数的回调,那么您的测试就会适当地失败。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21911635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档