samedi 13 février 2016

Ember.js Testing async action in controller

I've a controller with an action that invoke a method that do some async stuff and return a promise.

export default Ember.Controller.extend({

  _upload: function() {
    // return a promise
  },
  actions: {
    save: function(item) {
      this._upload(item).then(function(response) {
        // Handle success
      }, function(error) {
        // Handle error
      }
    }
  }
});

I would like to unit test the code under Handle success and Handle error. In my unit test I've mocked the _uploadMethod using

controller.set("_upload", function() {
  return new Ember.RSVP.Promise(function(resolve) {
    resolve({name: "image1"});
  });
});

And then I invoke the action and assert that the success handler has done is job

controller.send("save", "item");
assert.equal(controller.get("selected.item"), "item");

The problem is that the assertion fails because it's run before the promise is resolved and all the stuff in success handler is completed.

How can I wait the promise to resolve before the assertion is checked?




Aucun commentaire:

Enregistrer un commentaire