Created
December 16, 2014 16:17
-
-
Save rsutphin/73fdad14a24884eee336 to your computer and use it in GitHub Desktop.
Ember async test helper to wait for ember-data models to be committed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 Ember.compare (or maybe | |
// unregisterWaiter shouldn't use Ember.compare). | |
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'); | |
iterations++; | |
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') || | |
records.isAny('isReloading'); | |
if (pending) { break; } | |
} | |
anyPending = pending; | |
} | |
var waiter = function () { | |
if (anyPending) { | |
Ember.run.next(null, pendingChecker); | |
return false; | |
} else { | |
console.log('done waiting for models'); | |
Ember.Test.unregisterWaiter(context, waiter); | |
return true; | |
} | |
}; | |
Ember.Test.registerWaiter(context, waiter); | |
return wait(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Intended for use in integration tests for projects that use ember-data with a local database, like PouchDB via ember-pouch.
Sketch: