Add specs for mock date
This commit is contained in:
@@ -0,0 +1,139 @@
|
|||||||
|
describe("FakeDate", function() {
|
||||||
|
it("does not fail if no global date is found", function() {
|
||||||
|
var fakeGlobal = {},
|
||||||
|
mockDate = new j$.MockDate(fakeGlobal);
|
||||||
|
|
||||||
|
mockDate.install();
|
||||||
|
|
||||||
|
fakeGlobal.Date = jasmine.createSpy("Date");
|
||||||
|
|
||||||
|
mockDate.tick(0);
|
||||||
|
|
||||||
|
expect(fakeGlobal.Date).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not replace global Date if it is not installed", function() {
|
||||||
|
var fakeDate = jasmine.createSpy("global Date"),
|
||||||
|
fakeGlobal = { Date: fakeDate },
|
||||||
|
mockDate = new j$.MockDate(fakeGlobal);
|
||||||
|
|
||||||
|
fakeDate.now = function(){};
|
||||||
|
|
||||||
|
expect(fakeDate).toEqual(fakeGlobal.Date);
|
||||||
|
mockDate.install();
|
||||||
|
|
||||||
|
expect(fakeDate).not.toEqual(fakeGlobal.Date);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("replaces the global Date on uninstall", function() {
|
||||||
|
var fakeDate = jasmine.createSpy("global Date"),
|
||||||
|
fakeGlobal = { Date: fakeDate },
|
||||||
|
mockDate = new j$.MockDate(fakeGlobal);
|
||||||
|
|
||||||
|
fakeDate.now = function(){};
|
||||||
|
|
||||||
|
mockDate.install();
|
||||||
|
mockDate.uninstall();
|
||||||
|
|
||||||
|
expect(fakeDate).toEqual(fakeGlobal.Date);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
it("takes the current time as the base when installing without parameters", function() {
|
||||||
|
var fakeDate = jasmine.createSpy("global Date"),
|
||||||
|
fakeGlobal = { Date: fakeDate },
|
||||||
|
mockDate = new j$.MockDate(fakeGlobal);
|
||||||
|
|
||||||
|
fakeGlobal.Date.prototype.getTime = function() {
|
||||||
|
return 1000;
|
||||||
|
};
|
||||||
|
fakeDate.now = function(){ return 1000; };
|
||||||
|
|
||||||
|
mockDate.install();
|
||||||
|
|
||||||
|
expect(new fakeGlobal.Date().getTime()).toEqual(1000);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("can accept a date as time base when installing", function() {
|
||||||
|
var fakeGlobal = { Date: Date },
|
||||||
|
mockDate = new j$.MockDate(fakeGlobal),
|
||||||
|
baseDate = new Date(2013, 9, 23);
|
||||||
|
|
||||||
|
mockDate.install(baseDate);
|
||||||
|
|
||||||
|
expect(new fakeGlobal.Date().getTime()).toEqual(baseDate.getTime());
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it("fakes current time when using Date.now()", function() {
|
||||||
|
var fakeGlobal = { Date: Date },
|
||||||
|
mockDate = new j$.MockDate(fakeGlobal),
|
||||||
|
baseDate = new Date(2013, 9, 23);
|
||||||
|
|
||||||
|
mockDate.install(baseDate);
|
||||||
|
|
||||||
|
expect(fakeGlobal.Date.now()).toEqual(baseDate.getTime());
|
||||||
|
});
|
||||||
|
|
||||||
|
it("makes time passes using tick", function() {
|
||||||
|
var fakeDate = jasmine.createSpy("global Date"),
|
||||||
|
fakeGlobal = { Date: fakeDate },
|
||||||
|
mockDate = new j$.MockDate(fakeGlobal);
|
||||||
|
|
||||||
|
fakeDate.now = function(){ return 1000; };
|
||||||
|
|
||||||
|
mockDate.install();
|
||||||
|
|
||||||
|
mockDate.tick(100);
|
||||||
|
|
||||||
|
expect(fakeGlobal.Date.now()).toEqual(1100);
|
||||||
|
|
||||||
|
mockDate.tick(1000);
|
||||||
|
|
||||||
|
expect(fakeGlobal.Date.now()).toEqual(2100);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("allows to increase 0 milliseconds using tick", function() {
|
||||||
|
var fakeDate = jasmine.createSpy("global Date"),
|
||||||
|
fakeGlobal = { Date: fakeDate },
|
||||||
|
mockDate = new j$.MockDate(fakeGlobal);
|
||||||
|
|
||||||
|
fakeDate.now = function(){ return 1000; };
|
||||||
|
|
||||||
|
mockDate.install();
|
||||||
|
|
||||||
|
mockDate.tick(0);
|
||||||
|
expect(fakeGlobal.Date.now()).toEqual(1000);
|
||||||
|
|
||||||
|
mockDate.tick();
|
||||||
|
expect(fakeGlobal.Date.now()).toEqual(1000);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("allows to create a Date in a different time than now", function() {
|
||||||
|
var fakeGlobal = { Date: Date },
|
||||||
|
mockDate = new j$.MockDate(fakeGlobal),
|
||||||
|
baseDate = new Date(2013, 9, 23, 0, 0, 0, 0);
|
||||||
|
|
||||||
|
mockDate.install(baseDate);
|
||||||
|
|
||||||
|
var otherDate = new fakeGlobal.Date(2013, 9, 23, 0, 0, 1, 0);
|
||||||
|
|
||||||
|
mockDate.tick(1000);
|
||||||
|
|
||||||
|
expect(fakeGlobal.Date.now()).toEqual(otherDate.getTime());
|
||||||
|
});
|
||||||
|
|
||||||
|
it("copies all Date properties to the mocked date", function() {
|
||||||
|
var fakeGlobal = { Date: Date },
|
||||||
|
mockDate = new j$.MockDate(fakeGlobal),
|
||||||
|
baseDate = new Date(2013, 9, 23, 0, 0, 0, 0);
|
||||||
|
|
||||||
|
mockDate.install(baseDate);
|
||||||
|
|
||||||
|
var otherDate = new fakeGlobal.Date();
|
||||||
|
|
||||||
|
expect(otherDate).toEqual(jasmine.any(Date));
|
||||||
|
|
||||||
|
expect(fakeGlobal.Date.UTC(2013, 9, 23)).toEqual(Date.UTC(2013, 9, 23));
|
||||||
|
});
|
||||||
|
});
|
||||||
+1
-11
@@ -15,14 +15,6 @@ getJasmineRequireObj().Clock = function() {
|
|||||||
},
|
},
|
||||||
installed = false;
|
installed = false;
|
||||||
|
|
||||||
if (date) {
|
|
||||||
var realDate = {
|
|
||||||
Date: global.Date
|
|
||||||
},
|
|
||||||
fakeDate = {
|
|
||||||
Date: date.Date
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
self.install = function(mockDate) {
|
self.install = function(mockDate) {
|
||||||
replace(global, fakeTimingFunctions);
|
replace(global, fakeTimingFunctions);
|
||||||
@@ -31,15 +23,13 @@ getJasmineRequireObj().Clock = function() {
|
|||||||
|
|
||||||
if (date && mockDate) {
|
if (date && mockDate) {
|
||||||
date.install(mockDate);
|
date.install(mockDate);
|
||||||
replace(global, fakeDate);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
self.uninstall = function() {
|
self.uninstall = function() {
|
||||||
delayedFunctionScheduler.reset();
|
delayedFunctionScheduler.reset();
|
||||||
if (date) {
|
if (date) {
|
||||||
date.reset();
|
date.uninstall();
|
||||||
replace(global, realDate);
|
|
||||||
}
|
}
|
||||||
replace(global, realTimingFunctions);
|
replace(global, realTimingFunctions);
|
||||||
|
|
||||||
|
|||||||
+17
-14
@@ -6,16 +6,20 @@ getJasmineRequireObj().MockDate = function() {
|
|||||||
if (!global || !global.Date) {
|
if (!global || !global.Date) {
|
||||||
self.install = function() {};
|
self.install = function() {};
|
||||||
self.tick = function() {};
|
self.tick = function() {};
|
||||||
self.reset = function() {};
|
self.uninstall = function() {};
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var GlobalDate = global.Date;
|
||||||
|
|
||||||
self.install = function(mockDate) {
|
self.install = function(mockDate) {
|
||||||
if (mockDate instanceof Date) {
|
if (mockDate instanceof Date) {
|
||||||
currentTime = mockDate.getTime();
|
currentTime = mockDate.getTime();
|
||||||
} else {
|
} else {
|
||||||
currentTime = global.Date.now();
|
currentTime = GlobalDate.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
global.Date = FakeDate;
|
||||||
};
|
};
|
||||||
|
|
||||||
self.tick = function(millis) {
|
self.tick = function(millis) {
|
||||||
@@ -23,35 +27,34 @@ getJasmineRequireObj().MockDate = function() {
|
|||||||
currentTime = currentTime + millis;
|
currentTime = currentTime + millis;
|
||||||
};
|
};
|
||||||
|
|
||||||
self.reset = function() {
|
self.uninstall = function() {
|
||||||
currentTime = 0;
|
currentTime = 0;
|
||||||
|
global.Date = GlobalDate;
|
||||||
};
|
};
|
||||||
|
|
||||||
self.Date = FakeDate;
|
|
||||||
|
|
||||||
createDateProperties();
|
createDateProperties();
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
|
|
||||||
function FakeDate() {
|
function FakeDate() {
|
||||||
if (arguments.length === 0) {
|
if (arguments.length === 0) {
|
||||||
return new global.Date(currentTime);
|
return new GlobalDate(currentTime);
|
||||||
} else {
|
} else {
|
||||||
return global.Date.apply(this, arguments);
|
return new GlobalDate(arguments[0], arguments[1], arguments[2],
|
||||||
|
arguments[3], arguments[4], arguments[5], arguments[6]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createDateProperties() {
|
function createDateProperties() {
|
||||||
FakeDate.prototype = global.Date.prototype;
|
|
||||||
|
|
||||||
FakeDate.now = function() {
|
FakeDate.now = function() {
|
||||||
return currentTime;
|
return currentTime;
|
||||||
}
|
};
|
||||||
|
|
||||||
FakeDate.toSource = global.Date.toSource;
|
FakeDate.toSource = GlobalDate.toSource;
|
||||||
FakeDate.toString = global.Date.toString;
|
FakeDate.toString = GlobalDate.toString;
|
||||||
FakeDate.parse = global.Date.parse;
|
FakeDate.parse = GlobalDate.parse;
|
||||||
FakeDate.UTC = global.Date.UTC;
|
FakeDate.UTC = GlobalDate.UTC;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user