diff --git a/spec/suites/SpecRunningSpec.js b/spec/suites/SpecRunningSpec.js index 9f5d725..289d946 100644 --- a/spec/suites/SpecRunningSpec.js +++ b/spec/suites/SpecRunningSpec.js @@ -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; diff --git a/src/Env.js b/src/Env.js index e62bdff..66b5df3 100644 --- a/src/Env.js +++ b/src/Env.js @@ -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; diff --git a/src/Spec.js b/src/Spec.js index 98828bb..b24f859 100644 --- a/src/Spec.js +++ b/src/Spec.js @@ -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; }; diff --git a/src/WaitsForBlock.js b/src/WaitsForBlock.js index b413b38..793ca3e 100644 --- a/src/WaitsForBlock.js +++ b/src/WaitsForBlock.js @@ -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; diff --git a/src/base.js b/src/base.js index 60db77e..485ff84 100755 --- a/src/base.js +++ b/src/base.js @@ -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; @@ -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); }; /**