Allow mocked Date constructor to be called with a subset of full params

Fix #643 #624
This commit is contained in:
slackersoft
2014-07-31 13:36:31 -07:00
parent ee09301d8d
commit f2346d357f
3 changed files with 65 additions and 15 deletions

View File

@@ -1352,11 +1352,26 @@ getJasmineRequireObj().MockDate = function() {
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]);
switch(arguments.length) {
case 0:
return new GlobalDate(currentTime);
case 1:
return new GlobalDate(arguments[0]);
case 2:
return new GlobalDate(arguments[0], arguments[1]);
case 3:
return new GlobalDate(arguments[0], arguments[1], arguments[2]);
case 4:
return new GlobalDate(arguments[0], arguments[1], arguments[2], arguments[3]);
case 5:
return new GlobalDate(arguments[0], arguments[1], arguments[2], arguments[3],
arguments[4]);
case 6:
return new GlobalDate(arguments[0], arguments[1], arguments[2], arguments[3],
arguments[4], arguments[5]);
case 7:
return new GlobalDate(arguments[0], arguments[1], arguments[2], arguments[3],
arguments[4], arguments[5], arguments[6]);
}
}

View File

@@ -157,15 +157,35 @@ describe("FakeDate", function() {
expect(fakeGlobal.Date.now()).toEqual(1000);
});
it("allows to create a Date in a different time than the mocked time", function() {
it("allows creation of a Date in a different time than the mocked time", function() {
var fakeGlobal = { Date: Date },
mockDate = new j$.MockDate(fakeGlobal),
baseDate = new Date(2013, 9, 23, 0, 0, 0, 0);
mockDate = new j$.MockDate(fakeGlobal);
mockDate.install(baseDate);
mockDate.install();
var otherDate = new fakeGlobal.Date(2013, 9, 23, 0, 0, 1, 0);
expect(otherDate.getTime()).not.toEqual(baseDate.getTime());
expect(otherDate.getTime()).toEqual(new Date(2013, 9, 23, 0, 0, 1, 0).getTime());
});
it("allows creation of a Date that isn't fully specified", function() {
var fakeGlobal = { Date: Date },
mockDate = new j$.MockDate(fakeGlobal);
mockDate.install();
var otherDate = new fakeGlobal.Date(2013, 9, 23);
expect(otherDate.getTime()).toEqual(new Date(2013, 9, 23).getTime());
});
it('allows creation of a Date with millis', function() {
var fakeGlobal = { Date: Date },
mockDate = new j$.MockDate(fakeGlobal),
now = new Date(2014, 3, 15).getTime();
mockDate.install();
var otherDate = new fakeGlobal.Date(now);
expect(otherDate.getTime()).toEqual(now);
});
it("copies all Date properties to the mocked date", function() {

View File

@@ -37,11 +37,26 @@ getJasmineRequireObj().MockDate = function() {
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]);
switch(arguments.length) {
case 0:
return new GlobalDate(currentTime);
case 1:
return new GlobalDate(arguments[0]);
case 2:
return new GlobalDate(arguments[0], arguments[1]);
case 3:
return new GlobalDate(arguments[0], arguments[1], arguments[2]);
case 4:
return new GlobalDate(arguments[0], arguments[1], arguments[2], arguments[3]);
case 5:
return new GlobalDate(arguments[0], arguments[1], arguments[2], arguments[3],
arguments[4]);
case 6:
return new GlobalDate(arguments[0], arguments[1], arguments[2], arguments[3],
arguments[4], arguments[5]);
case 7:
return new GlobalDate(arguments[0], arguments[1], arguments[2], arguments[3],
arguments[4], arguments[5], arguments[6]);
}
}