在灰烬验收测试中测试“加载”状态

时间:2017-02-23 16:15:59

标签: ember.js ember-cli

在我的验收测试中,我想确认在异步操作期间正确显示和隐藏“加载指示符”。假设我有一个看起来像这样的动作:

myAction() {
    this.set('isLoading', true);
    someAsyncMethod().then(answer => {
        this.set('isLoading', false);
    });
}

一个看起来像这样的模板:

<button class="my-button" {{action "myAction"}}>
  {{#if isLoading}}
    <i class="loader"></i>
  {{/if}}
  Click me !
</button>

最后,测试:

test('My super action', function(assert) {
    visit('/my-route');
    andThen(() => {
        click('.my-button');
        // Here, since the click() helper is async, the action isn't 
        // called, so the loader isn't shown yet.
    });
    andThen(() => {
      // Here, the load is finished, so the loader is removed
    });
});

我的问题是:我在哪里可以断言显示加载程序?

1 个答案:

答案 0 :(得分:0)

test('My super action', function(assert) {
  visit('/my-route');
  andThen(() => {
      click('.my-button');
      // Here, since the click() helper is async, the action isn't 
      // called, so the loader isn't shown yet.
      Ember.run.next(function () {
        //here is fine, as we are in the next run loop the promise returned in the action has started loading
      });
  });
  andThen(() => {
    // Here, the load is finished, so the loader is removed
  });
});