Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Ember async test helper to wait for ember-data models to be committed
// Registers a temporary test waiter that will be released once there are
// no longer any pending records of the given types in the store. "pending" records
// are ones where isEmpty, isLoading, isReloading, or isSaving are true.
import Ember from 'ember';
export default Ember.Test.registerAsyncHelper('waitForModels', function (app, typeNames) {
// The context object is necessary because unregisterWaiter will unregister
// _all_ bare function waiters if any bare function waiter is unregistered.
// This appears to be due to a defect in (or maybe
// unregisterWaiter shouldn't use
var context = Math.random();
console.log('waitForModels', typeNames.join(' | '));
var anyPending = true;
var maxIterations = 250;
var iterations = 0;
function pendingChecker() {
var i, records, pending = false;
var store = app.__container__.lookup('store:main');
for (i = 0 ; iterations < maxIterations && i < typeNames.length ; i++) {
records = store.all(typeNames[i]);
console.log(typeNames[i], records.getEach('currentState.stateName').join(" | "));
pending = records.isAny('isSaving') ||
records.isAny('isEmpty') ||
records.isAny('isLoading') ||
if (pending) { break; }
anyPending = pending;
var waiter = function () {
if (anyPending) {, pendingChecker);
return false;
} else {
console.log('done waiting for models');
Ember.Test.unregisterWaiter(context, waiter);
return true;
Ember.Test.registerWaiter(context, waiter);
return wait();

This comment has been minimized.

Copy link
Owner Author

@rsutphin rsutphin commented Dec 16, 2014

Intended for use in integration tests for projects that use ember-data with a local database, like PouchDB via ember-pouch.


waitForModels(['person', 'sandwich']);
andThen(function () {
  // verify stored data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment