Skip to content

Instantly share code, notes, and snippets.

@jareware
Created July 15, 2011 12:51
Show Gist options
  • Save jareware/1084619 to your computer and use it in GitHub Desktop.
Save jareware/1084619 to your computer and use it in GitHub Desktop.
Waiting for a specific DOM event in Jasmine
/**
* Pauses the tests execution until the named event is notified on the given
* element, or a timeout is reached.
*
* @param event
* @param onElement
* @param timeout
*/
var waitsForEvent = function(event, onElement, timeout) {
timeout = timeout || 10000;
var eventFired = null;
waitsFor(function() {
if (eventFired === null) {
onElement.addEventListener(event, function() {
eventFired = true;
}, false);
eventFired = false;
}
return eventFired;
}, 'event "' + event + '" to fire on given element', timeout);
};
// Usage example:
// waitsForEvent('load', iframe);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment