From f2346d357f05f2edaf998fbbacfd7b8f65373128 Mon Sep 17 00:00:00 2001 From: slackersoft Date: Thu, 31 Jul 2014 13:36:31 -0700 Subject: [PATCH] Allow mocked Date constructor to be called with a subset of full params Fix #643 #624 --- lib/jasmine-core/jasmine.js | 25 ++++++++++++++++++++----- spec/core/MockDateSpec.js | 30 +++++++++++++++++++++++++----- src/core/MockDate.js | 25 ++++++++++++++++++++----- 3 files changed, 65 insertions(+), 15 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index c943db1a..95b98208 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -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]); } } diff --git a/spec/core/MockDateSpec.js b/spec/core/MockDateSpec.js index 72aa357b..250fe457 100644 --- a/spec/core/MockDateSpec.js +++ b/spec/core/MockDateSpec.js @@ -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() { diff --git a/src/core/MockDate.js b/src/core/MockDate.js index 9741033e..db347bbe 100644 --- a/src/core/MockDate.js +++ b/src/core/MockDate.js @@ -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]); } }