dimanche 14 février 2016

How do you assert a run loop was used in Ember.js?

I have this code inside an Ember service:

return new Promise((resolve, reject) => {
  semaphore.take(() => {
    this.get('limiter').removeTokens(1, () => {
      semaphore.leave();

      this.get('requestSender').sendRequest(url).then(data => {
        run(null, resolve, cache.put(url, data, cacheTime));
      }).catch(function() {
        run(null, reject, arguments);
      });
    });
  });
});

I'm going through the limiter callback, so I need a run loop (to my understanding). I would like to assert in a unit test that a run loop was used. I can assert the promise resolves the correct value from the cache, but I don't know how to verify a run loop was used. In other words, I could remove the run call and all my tests would still pass, which is not desirable.

Things I've thought of that I don't want to resort to:

  • I could use Ember.run instead of run and use stubbing and restoring, but I like to deconstruct globals at the top of the file (const { run } = Ember;), so I would not like to sacrifice that just for a test.
  • I could wrap the run loop call in another Ember service so I can stub it, but that seems like overkill.

Basically I'm wondering if there are any events that fire when a run loop is entered that I can listen to in a test. It's OK if involves Ember privates.




Aucun commentaire:

Enregistrer un commentaire