Does Ember have a hint to find out when there are no active async operations?

Protractor provides a great API wrapper in selenium-webdriver tests, but one way it's done is by using a hook that Angular provides the ability to complete all pending async / polling operations. This includes HTTP calls, intervals, etc.

I'm new to Ember, but I suspect the equivalent needs to be aware of route changes, http calls, and one or more aspects of the startup loop. I have confirmed that such a hook is likely to open the door to using Protractor with Ember applications, which is ultimately my goal.

So, is there a way to detect when Ember has entered this "calm" state?

+3


source to share


1 answer


This is how Ember does it (in test mode only, and Test is available in the Ember namespace)



function wait(app, value) {
  return Test.promise(function(resolve) {
    // If this is the first async promise, kick off the async test
    if (++countAsync === 1) {
      Test.adapter.asyncStart();
    }

    // Every 10ms, poll for the async thing to have finished
    var watcher = setInterval(function() {
      // 1. If the router is loading, keep polling
      var routerIsLoading = !!app.__container__.lookup('router:main').router.activeTransition;
      if (routerIsLoading) { return; }

      // 2. If there are pending Ajax requests, keep polling
      if (Test.pendingAjaxRequests) { return; }

      // 3. If there are scheduled timers or we are inside of a run loop, keep polling
      if (run.hasScheduledTimers() || run.currentRunLoop) { return; }
      if (Test.waiters && Test.waiters.any(function(waiter) {
        var context = waiter[0];
        var callback = waiter[1];
        return !callback.call(context);
      })) { return; }
      // Stop polling
      clearInterval(watcher);

      // If this is the last async promise, end the async test
      if (--countAsync === 0) {
        Test.adapter.asyncEnd();
      }

      // Synchronously resolve the promise
      run(null, resolve, value);
    }, 10);
  });

}

      

+1


source







All Articles