Skip to content

Commit

Permalink
Allow waitsFor() arguments to be specified in arbitrary order.
Browse files Browse the repository at this point in the history
Default waitsFor() timeout is now specified in env.defaultTimeoutInterval; defaults to 5 seconds.
Deprecate waits() block in favor of waitsFor().
  • Loading branch information
xian committed Aug 26, 2010
1 parent 5514931 commit e7a5760
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 11 deletions.
57 changes: 57 additions & 0 deletions spec/suites/SpecRunningSpec.js
Expand Up @@ -259,6 +259,63 @@ describe("jasmine spec running", function () {
});

describe("waitsFor", function() {
var latchFunction = function() {
return true;
};
var spec;

function makeWaitsForSpec() {
var args = jasmine.util.argsToArray(arguments);
env.describe('suite', function() {
spec = env.it('spec', function() {
this.waitsFor.apply(this, args);
});
});
env.execute();
}

it("should accept args (latchFunction, timeoutMessage, timeout)", function() {
makeWaitsForSpec(latchFunction, "message", 123);
var block = spec.queue.blocks[1];
expect(block.latchFunction).toBe(latchFunction);
expect(block.timeout).toEqual(123);
expect(block.message).toEqual('message');
});

it("should accept args (latchFunction, timeout)", function() {
makeWaitsForSpec(latchFunction, 123);
var block = spec.queue.blocks[1];
expect(block.latchFunction).toBe(latchFunction);
expect(block.timeout).toEqual(123);
expect(block.message).toEqual(null);
});

it("should accept args (latchFunction, timeoutMessage)", function() {
env.defaultTimeoutInterval = 4321;
makeWaitsForSpec(latchFunction, "message");
var block = spec.queue.blocks[1];
expect(block.latchFunction).toBe(latchFunction);
expect(block.timeout).toEqual(4321);
expect(block.message).toEqual('message');
});

it("should accept args (latchFunction)", function() {
env.defaultTimeoutInterval = 4321;
makeWaitsForSpec(latchFunction);
var block = spec.queue.blocks[1];
expect(block.latchFunction).toBe(latchFunction);
expect(block.timeout).toEqual(4321);
expect(block.message).toEqual(null);
});

it("should accept deprecated args order (timeout, latchFunction, timeoutMessage)", function() {
makeWaitsForSpec(123, latchFunction, "message");
var block = spec.queue.blocks[1];
expect(block.latchFunction).toBe(latchFunction);
expect(block.timeout).toEqual(123);
expect(block.message).toEqual('message');
});

it("testWaitsFor", function() {
var doneWaiting = false;
var runsBlockExecuted = false;
Expand Down
1 change: 1 addition & 0 deletions src/Env.js
Expand Up @@ -11,6 +11,7 @@ jasmine.Env = function() {
this.reporter = new jasmine.MultiReporter();

this.updateInterval = jasmine.DEFAULT_UPDATE_INTERVAL;
this.defaultTimeoutInterval = jasmine.DEFAULT_TIMEOUT_INTERVAL;
this.lastUpdate = 0;
this.specFilter = function() {
return true;
Expand Down
36 changes: 34 additions & 2 deletions src/Spec.js
Expand Up @@ -73,14 +73,46 @@ jasmine.Spec.prototype.expect = function(actual) {
return positive;
};

/**
* Waits a fixed time period before moving to the next block.
*
* @deprecated Use waitsFor() instead
* @param {Number} timeout milliseconds to wait
*/
jasmine.Spec.prototype.waits = function(timeout) {
var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this);
this.addToQueue(waitsFunc);
return this;
};

jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessage) {
var waitsForFunc = new jasmine.WaitsForBlock(this.env, timeout, latchFunction, timeoutMessage, this);
/**
* Waits for the latchFunction to return true before proceeding to the next block.
*
* @param {Function} latchFunction
* @param {String} optional_timeoutMessage
* @param {Number} optional_timeout
*/
jasmine.Spec.prototype.waitsFor = function(latchFunction, optional_timeoutMessage, optional_timeout) {
var latchFunction_ = null;
var optional_timeoutMessage_ = null;
var optional_timeout_ = null;

for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i];
switch (typeof arg) {
case 'function':
latchFunction_ = arg;
break;
case 'string':
optional_timeoutMessage_ = arg;
break;
case 'number':
optional_timeout_ = arg;
break;
}
}

var waitsForFunc = new jasmine.WaitsForBlock(this.env, optional_timeout_, latchFunction_, optional_timeoutMessage_, this);
this.addToQueue(waitsForFunc);
return this;
};
Expand Down
2 changes: 1 addition & 1 deletion src/WaitsForBlock.js
Expand Up @@ -10,7 +10,7 @@
* @param {jasmine.Spec} spec The Jasmine spec.
*/
jasmine.WaitsForBlock = function(env, timeout, latchFunction, message, spec) {
this.timeout = timeout;
this.timeout = timeout || env.defaultTimeoutInterval;
this.latchFunction = latchFunction;
this.message = message;
this.totalTimeSpentWaitingForLatch = 0;
Expand Down
23 changes: 15 additions & 8 deletions src/base.js
Expand Up @@ -21,11 +21,16 @@ jasmine.unimplementedMethod_ = function() {
jasmine.undefined = jasmine.___undefined___;

/**
* Default interval for event loop yields. Small values here may result in slow test running. Zero means no updates until all tests have completed.
* Default interval in milliseconds for event loop yields (e.g. to allow network activity or to refresh the screen with the HTML-based runner). Small values here may result in slow test running. Zero means no updates until all tests have completed.
*
*/
jasmine.DEFAULT_UPDATE_INTERVAL = 250;

/**
* Default timeout interval in milliseconds for waitsFor() blocks.
*/
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;

jasmine.getGlobal = function() {
function getGlobal() {
return this;
Expand Down Expand Up @@ -490,22 +495,24 @@ var runs = function(func) {
};

/**
* Waits for a timeout before moving to the next runs()-defined block.
* @param {Number} timeout
* Waits a fixed time period before moving to the next block.
*
* @deprecated Use waitsFor() instead
* @param {Number} timeout milliseconds to wait
*/
var waits = function(timeout) {
jasmine.getEnv().currentSpec.waits(timeout);
};

/**
* Waits for the latchFunction to return true before proceeding to the next runs()-defined block.
* Waits for the latchFunction to return true before proceeding to the next block.
*
* @param {Number} timeout
* @param {Function} latchFunction
* @param {String} message
* @param {String} optional_timeoutMessage
* @param {Number} optional_timeout
*/
var waitsFor = function(timeout, latchFunction, message) {
jasmine.getEnv().currentSpec.waitsFor(timeout, latchFunction, message);
var waitsFor = function(latchFunction, optional_timeoutMessage, optional_timeout) {
jasmine.getEnv().currentSpec.waitsFor.apply(jasmine.getEnv().currentSpec, arguments);
};

/**
Expand Down

0 comments on commit e7a5760

Please sign in to comment.