Skip to content

Instantly share code, notes, and snippets.

@lsmith
Last active December 24, 2015 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsmith/6799794 to your computer and use it in GitHub Desktop.
Save lsmith/6799794 to your computer and use it in GitHub Desktop.
YUITest.TestCase.prototype = {
// ...
wait: function (segment, timeout) {
var delay = (typeof segment === 'number') ? segment :
(typeof timeout === 'number') ? timeout :
YUITest.MAX_WAIT;
throw new YUITest.Wait((typeof segment === 'function' ? segment : YUITest._waitTimeout), delay);
},
/**
Delays the current test until _condition_ returns a truthy value. If _condition_ fails to return a
truthy value before _timeout_ milliseconds have passed, the test fails. Default _timeout_ is 10s.
_condition_ will be executed every _increment_ milliseconds (default 100).
@method waitFor
@param {Function} condition Function executed to indicate whether to execute _segment_
@param {Function} segment Function to check the success or failure of this test
@param {Number} [timeout=10000] Maximum number of milliseconds to wait for _condition_ to return true
@param {Number} [increment=100] Milliseconds to wait before checking _condition_
**/
waitFor: function (condition, segment, timeout, increment) {
var self = this,
endTime;
if ((typeof condition !== 'function') || (typeof segment !== 'function')) {
self.fail('waitFor() called with invalid parameters. condition and segment must be functions.');
}
if (typeof timeout !== 'number') {
timeout = YUITest.MAX_WAIT;
}
endTime = (+new Date()) + timeout;
if (typeof increment !== 'number') {
increment = 100;
}
self.wait(function () {
if (condition()) {
segment();
} else {
var now = (+new Date());
if (now > endTime) {
YUITest._waitTimeout();
} else {
self.waitFor(condition, segment, (endTime - now), increment);
}
}, increment);
},
//...
};
/**
Calls `YUITest.Assert.fail()` with a message indicating `wait()` was called, but
`resume()` was never called.
@method _waitTimeout
@static
@protected
**/
YUITest._waitTimeout = function () {
YUITest.Assert.fail("Timeout: wait() called but resume() never called.");
};
YUITest.MAX_WAIT = 10000;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment