【问题标题】:Ember acceptance test andThen() doesn't wait until scroll events are completeEmber 验收测试 andThen() 不会等到滚动事件完成
【发布时间】:2016-12-20 20:02:35
【问题描述】:

我需要一个 emberJs 验收测试,以便能够滚动到页面中的某个点,然后才断言有关页面的内容。

这两个功能,

Ember.$('body').scrollTop(1000);

window.scroll(0,1000);

当后面跟着一个

andThen(()=>{
    console.log('body', Ember.$('body').scrollTop());
}) 

打印出 body scrollTop 位置在 108。我需要它在 1000。

到目前为止,我可以达到 body scrollTop 为 1000 的唯一方法是使用此回调:

Ember.$('body').animate({scrollTop: 1000}, () => {
  console.log('body', Ember.$('body').scrollTop());
  staticWiz.assertHasGoToTopButton(true);
});

这里的问题是,在这个回调发生的时候,没有任何测试内容存在。我不能断言任何事情。

如果我尝试在此回调中使用 assert.async() 和 done(),它会在 body scrollTop() 为 108 时过早触发:

const done = assert.async();
Ember.$('body').animate({scrollTop: 1000}, () => {
    console.log('body', Ember.$('body').scrollTop());
    staticWiz.assertHasGoToTopButton(true);
    done();
});

如果我设置循环超时作为检查滚动位置的一种方式,它只会永远停留在 108 的相同位置。

const done = assert.async();
window.scroll(0, 1000);
const checkScroll = () => {
  console.log('body', Ember.$('body').scrollTop());
  if (Ember.$('body').scrollTop() === 1000) {
    staticWiz.assertHasGoToTopButton(true);
    done();
    return;
  }
  setTimeout(checkScroll, 1000);
};
checkScroll();

所以。有任何想法吗?以前有没有人为他们工作过,在这种情况下,您不能只是有任何程度的滚动,而是需要通过 Emberjs 验收测试的特定数字?

更新:同事刚刚意识到测试页面正文正在被测试滚动,而不是测试页面内的嵌套应用正文。我们应用程序中的“body”滚动观察器不会在测试页面的应用程序窗口内拾取滚动。不知道从那里去哪里。

【问题讨论】:

    标签: jquery testing ember.js qunit event-loop


    【解决方案1】:

    完成这项工作的一种方法是使用 Ember 中的低级 waiter 功能。

    见:http://emberjs.com/api/classes/Ember.Test.html#method_registerWaiter

    这基本上是你将如何使用它:

    function finishedScrolling() {
      return Ember.$('body').scrollTop() === 1000;
    }
    
    Ember.Test.registerWaiter(finishedScrolling);
    
    Ember.$('body').animate({scrollTop: 1000});
    
    andThen(function() {
      Ember.Test.unregisterWaiter(finishedScrolling);
      staticWiz.assertHasGoToTopButton(true);
    });
    

    在这里查看它的工作原理:https://ember-twiddle.com/1407bbadbb13365181f91201de6ba46c?openFiles=tests.acceptance.my-acceptance-test.js%2C

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      相关资源
      最近更新 更多