diff --git a/src/core/Clock.js b/src/core/Clock.js index 188a45e3..96304ed2 100644 --- a/src/core/Clock.js +++ b/src/core/Clock.js @@ -1,5 +1,5 @@ getJasmineRequireObj().Clock = function() { - function Clock(global, delayedFunctionScheduler) { + function Clock(global, delayedFunctionScheduler, date) { var self = this, realTimingFunctions = { setTimeout: global.setTimeout, @@ -15,15 +15,34 @@ getJasmineRequireObj().Clock = function() { }, installed = false; - self.install = function() { + if (date) { + var realDate = { + Date: global.Date + }, + fakeDate = { + Date: date.Date + }; + } + + self.install = function(mockDate) { replace(global, fakeTimingFunctions); timer = fakeTimingFunctions; installed = true; + + if (date && mockDate) { + date.install(mockDate); + replace(global, fakeDate); + } }; self.uninstall = function() { delayedFunctionScheduler.reset(); + if (date) { + date.reset(); + replace(global, realDate); + } replace(global, realTimingFunctions); + timer = realTimingFunctions; installed = false; }; @@ -58,6 +77,9 @@ getJasmineRequireObj().Clock = function() { self.tick = function(millis) { if (installed) { + if (date) { + date.tick(millis); + } delayedFunctionScheduler.tick(millis); } else { throw new Error("Mock clock is not installed, use jasmine.getEnv().clock.install()"); diff --git a/src/core/Env.js b/src/core/Env.js index 5c1fa813..58de0609 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -9,7 +9,7 @@ getJasmineRequireObj().Env = function(j$) { var realSetTimeout = j$.getGlobal().setTimeout; var realClearTimeout = j$.getGlobal().clearTimeout; - this.clock = new j$.Clock(global, new j$.DelayedFunctionScheduler()); + this.clock = new j$.Clock(global, new j$.DelayedFunctionScheduler(), new j$.MockDate(global)); var runnableLookupTable = {}; diff --git a/src/core/MockDate.js b/src/core/MockDate.js new file mode 100644 index 00000000..db17e006 --- /dev/null +++ b/src/core/MockDate.js @@ -0,0 +1,59 @@ +getJasmineRequireObj().MockDate = function() { + function MockDate(global) { + var self = this; + var currentTime = 0; + + if (!global || !global.Date) { + self.install = function() {}; + self.tick = function() {}; + self.reset = function() {}; + return self; + } + + self.install = function(mockDate) { + if (mockDate instanceof Date) { + currentTime = mockDate.getTime(); + } else { + currentTime = global.Date.now(); + } + }; + + self.tick = function(millis) { + millis = millis || 0; + currentTime = currentTime + millis; + }; + + self.reset = function() { + currentTime = 0; + }; + + self.Date = FakeDate; + + createDateProperties(); + + return self; + + function FakeDate() { + if (arguments.length === 0) { + return new global.Date(currentTime); + } else { + return global.Date.apply(this, arguments); + } + } + + function createDateProperties() { + FakeDate.prototype = global.Date.prototype; + + FakeDate.now = function() { + return currentTime; + } + + FakeDate.toSource = global.Date.toSource; + FakeDate.toString = global.Date.toString; + FakeDate.parse = global.Date.parse; + FakeDate.UTC = global.Date.UTC; + } + } + + return MockDate; +}; diff --git a/src/core/requireCore.js b/src/core/requireCore.js index 15b949ea..b33f7296 100644 --- a/src/core/requireCore.js +++ b/src/core/requireCore.js @@ -14,6 +14,7 @@ getJasmineRequireObj().core = function(jRequire) { j$.util = jRequire.util(); j$.Any = jRequire.Any(); j$.CallTracker = jRequire.CallTracker(); + j$.MockDate = jRequire.MockDate(); j$.Clock = jRequire.Clock(); j$.DelayedFunctionScheduler = jRequire.DelayedFunctionScheduler(); j$.Env = jRequire.Env(j$);