2017-02-23 87 views
1

在我的驗收測試中,我想確認在異步操作期間「加載指示器」是否正確顯示和隱藏。比方說,我有一個看起來像這樣的動作:在Ember驗收測試中測試「加載」狀態

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 
    }); 
}); 

我的問題是:我可以在哪裏做斷言,顯示加載程序?

回答

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 
    }); 
});