Skip to content
This repository has been archived by the owner on Apr 5, 2023. It is now read-only.

Commit

Permalink
Allow mocked Date constructor to be called with a subset of full params
Browse files Browse the repository at this point in the history
  • Loading branch information
slackersoft committed Jul 31, 2014
1 parent ee09301 commit f2346d3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
25 changes: 20 additions & 5 deletions lib/jasmine-core/jasmine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

Expand Down
30 changes: 25 additions & 5 deletions spec/core/MockDateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
25 changes: 20 additions & 5 deletions src/core/MockDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

Expand Down

0 comments on commit f2346d3

Please sign in to comment.