Merge branch 'mock-date' of https://github.com/jalopez/jasmine into jalopez-mock-date
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
getJasmineRequireObj().Clock = function() {
|
||||
function Clock(global, delayedFunctionScheduler) {
|
||||
function Clock(global, delayedFunctionScheduler, date) {
|
||||
var self = this,
|
||||
realTimingFunctions = {
|
||||
setTimeout: global.setTimeout,
|
||||
@@ -16,15 +16,24 @@ getJasmineRequireObj().Clock = function() {
|
||||
installed = false,
|
||||
timer;
|
||||
|
||||
self.install = function() {
|
||||
|
||||
self.install = function(mockDate) {
|
||||
replace(global, fakeTimingFunctions);
|
||||
timer = fakeTimingFunctions;
|
||||
installed = true;
|
||||
|
||||
if (date && mockDate) {
|
||||
date.install(mockDate);
|
||||
}
|
||||
};
|
||||
|
||||
self.uninstall = function() {
|
||||
delayedFunctionScheduler.reset();
|
||||
if (date) {
|
||||
date.uninstall();
|
||||
}
|
||||
replace(global, realTimingFunctions);
|
||||
|
||||
timer = realTimingFunctions;
|
||||
installed = false;
|
||||
};
|
||||
@@ -59,6 +68,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.clock().install()');
|
||||
|
||||
@@ -11,7 +11,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 = {};
|
||||
|
||||
|
||||
62
src/core/MockDate.js
Normal file
62
src/core/MockDate.js
Normal file
@@ -0,0 +1,62 @@
|
||||
getJasmineRequireObj().MockDate = function() {
|
||||
function MockDate(global) {
|
||||
var self = this;
|
||||
var currentTime = 0;
|
||||
|
||||
if (!global || !global.Date) {
|
||||
self.install = function() {};
|
||||
self.tick = function() {};
|
||||
self.uninstall = function() {};
|
||||
return self;
|
||||
}
|
||||
|
||||
var GlobalDate = global.Date;
|
||||
|
||||
self.install = function(mockDate) {
|
||||
if (mockDate instanceof Date) {
|
||||
currentTime = mockDate.getTime();
|
||||
} else {
|
||||
currentTime = GlobalDate.now();
|
||||
}
|
||||
|
||||
global.Date = FakeDate;
|
||||
};
|
||||
|
||||
self.tick = function(millis) {
|
||||
millis = millis || 0;
|
||||
currentTime = currentTime + millis;
|
||||
};
|
||||
|
||||
self.uninstall = function() {
|
||||
currentTime = 0;
|
||||
global.Date = GlobalDate;
|
||||
};
|
||||
|
||||
createDateProperties();
|
||||
|
||||
return self;
|
||||
|
||||
function FakeDate() {
|
||||
if (arguments.length === 0) {
|
||||
return new GlobalDate(currentTime);
|
||||
} else {
|
||||
return new GlobalDate(arguments[0], arguments[1], arguments[2],
|
||||
arguments[3], arguments[4], arguments[5], arguments[6]);
|
||||
}
|
||||
}
|
||||
|
||||
function createDateProperties() {
|
||||
|
||||
FakeDate.now = function() {
|
||||
return currentTime;
|
||||
};
|
||||
|
||||
FakeDate.toSource = GlobalDate.toSource;
|
||||
FakeDate.toString = GlobalDate.toString;
|
||||
FakeDate.parse = GlobalDate.parse;
|
||||
FakeDate.UTC = GlobalDate.UTC;
|
||||
}
|
||||
}
|
||||
|
||||
return MockDate;
|
||||
};
|
||||
@@ -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$);
|
||||
|
||||
Reference in New Issue
Block a user