mbreton
mbreton

Reputation: 684

Ember Integration Testing, How to use wait to build a custom Helper?

I don't understand how to use the wait helper provided by the Ember.Test package to build our own helpers. The few that I understood, is that we must return systematically "wait(...)" at the end of our helpers. But how report that my asynchronous operation is done ? We can pass a parameter to the wait method, so I thought that I could pass it a promise, and resolve the promise when my asynchronous operation is done ... But it doesn't work :D

To illustrate my idea I did a JSBIN with an example : http://jsbin.com/OxeniTO/30/

App = Em.Application.create({
  rootElement:"#ember-app"
});

App.setupForTesting();
App.injectTestHelpers();
App.advanceReadiness();


test('Wait 1000ms and fail',function(){
  visit('/').then(function(){
    var promise = $.Deferred();

    setTimeout(function(){
      ok(false, "Assertion failed");
      promise.resolve();
    }, 1000);

    ok(true, "Assert passed");

    return wait(promise);
  });
});

In this case, the Ember.Test.Adapter for Qunit doesn't wait the promise resolution.

Upvotes: 2

Views: 1866

Answers (1)

intuitivepixel
intuitivepixel

Reputation: 23322

Try to wrap your resolving promise in Ember.run.later instead of using setTimeout:

...
Ember.run.later(function(){
  ok(false, "Assertion failed");
  promise.resolve();
}, 1000);
...

Demo

Hope it helps.

Upvotes: 3

Related Questions