Skip to content

Commit

Permalink
Merge pull request #103 from fatso83/change-install-signature
Browse files Browse the repository at this point in the history
Change lolex.install signature
  • Loading branch information
fatso83 committed Jul 13, 2017
2 parents 61e95e1 + 10c053f commit d0b5dae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 35 deletions.
38 changes: 18 additions & 20 deletions src/lolex-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ function fixedModulo(n, m) {

/**
* Used to grok the `now` parameter to createClock.
* @param epoch {Date|number} the system time
*/
function getEpoch(epoch) {
if (!epoch) { return 0; }
Expand Down Expand Up @@ -429,6 +430,10 @@ var keys = Object.keys || function (obj) {

exports.timers = timers;

/**
* @param now {Date|number} the system time
* @param loopLimit {number} maximum number of timers that will be run when calling runAll()
*/
function createClock(now, loopLimit) {
loopLimit = loopLimit || 1000;

Expand Down Expand Up @@ -623,32 +628,25 @@ function createClock(now, loopLimit) {
}
exports.createClock = createClock;

exports.install = function install(target, now, toFake, loopLimit) {
var i, l;

if (target instanceof Date) {
toFake = now;
now = target.getTime();
target = null;
}

if (typeof target === "number") {
toFake = now;
now = target;
target = null;
}

if (!target) {
target = global;
}
/**
* @param config {Object} optional config
* @param config.target {Object} the target to install timers in (default `window`)
* @param config.now {number|Date} a number (in milliseconds) or a Date object (default epoch)
* @param config.toFake {string[]} names of the methods that should be faked.
* @param config.loopLimit {number} the maximum number of timers that will be run when calling runAll()
*/
exports.install = function install(config) {
config = typeof config !== "undefined" ? config : {};

var clock = createClock(now, loopLimit);
var i, l;
var target = config.target || global;
var clock = createClock(config.now, config.loopLimit);

clock.uninstall = function () {
uninstall(clock, target);
};

clock.methods = toFake || [];
clock.methods = config.toFake || [];

if (clock.methods.length === 0) {
clock.methods = keys(timers);
Expand Down
30 changes: 15 additions & 15 deletions test/lolex-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("issue #59", function () {
describe("issue #73", function () {
it("should install with date object", function () {
var date = new Date("2015-09-25");
var clock = lolex.install(date);
var clock = lolex.install( { now: date });
assert.same(clock.now, 1443139200000);
clock.uninstall();
});
Expand Down Expand Up @@ -307,7 +307,7 @@ describe("lolex", function () {
describe("tick", function () {

beforeEach(function () {
this.clock = lolex.install(0);
this.clock = lolex.install({ now: 0 });
});

afterEach(function () {
Expand Down Expand Up @@ -644,7 +644,7 @@ describe("lolex", function () {
describe("next", function () {

beforeEach(function () {
this.clock = lolex.install(0);
this.clock = lolex.install({ now: 0 });
});

afterEach(function () {
Expand Down Expand Up @@ -904,7 +904,7 @@ describe("lolex", function () {
});

it("the loop limit can be set when installing a clock", function () {
this.clock = lolex.install(0, null, null, 1);
this.clock = lolex.install({ loopLimit: 1 });
var test = this;

var spies = [sinon.spy(), sinon.spy()];
Expand Down Expand Up @@ -1498,7 +1498,7 @@ describe("lolex", function () {
});

it("sets initial timestamp", function () {
this.clock = lolex.install(1400);
this.clock = lolex.install({ now: 1400 });

assert.equals(this.clock.now, 1400);
});
Expand Down Expand Up @@ -1648,7 +1648,7 @@ describe("lolex", function () {
delete global.tick;
Object.getPrototypeOf(global).tick = function () { };

this.clock = lolex.install(0, ["tick"]);
this.clock = lolex.install({ now: 0, toFake: ["tick"] });
assert.isTrue(global.hasOwnProperty("tick"));
this.clock.uninstall();

Expand All @@ -1664,7 +1664,7 @@ describe("lolex", function () {
// Directly give the global object a tick method
global.tick = NOOP;

this.clock = lolex.install(0, ["tick"]);
this.clock = lolex.install({ now: 0, toFake: ["tick"] });
assert.isTrue(global.hasOwnProperty("tick"));
this.clock.uninstall();

Expand All @@ -1673,30 +1673,30 @@ describe("lolex", function () {
});

it("fakes Date constructor", function () {
this.clock = lolex.install(0);
this.clock = lolex.install({ now: 0 });
var now = new Date();

refute.same(Date, lolex.timers.Date);
assert.equals(now.getTime(), 0);
});

it("fake Date constructor should mirror Date's properties", function () {
this.clock = lolex.install(0);
this.clock = lolex.install({ now: 0});

assert(!!Date.parse);
assert(!!Date.UTC);
});

it("decide on Date.now support at call-time when supported", function () {
global.Date.now = NOOP;
this.clock = lolex.install(0);
this.clock = lolex.install({ now: 0});

assert.equals(typeof Date.now, "function");
});

it("decide on Date.now support at call-time when unsupported", function () {
global.Date.now = undefined;
this.clock = lolex.install(0);
this.clock = lolex.install({ now: 0});

refute.defined(Date.now);
});
Expand All @@ -1710,29 +1710,29 @@ describe("lolex", function () {
});

it("uninstalls Date constructor", function () {
this.clock = lolex.install(0);
this.clock = lolex.install({ now: 0});
this.clock.uninstall();

assert.same(GlobalDate, lolex.timers.Date);
});

it("fakes provided methods", function () {
this.clock = lolex.install(0, ["setTimeout", "Date", "setImmediate"]);
this.clock = lolex.install({ now: 0, toFake: ["setTimeout", "Date", "setImmediate"] });

refute.same(setTimeout, lolex.timers.setTimeout);
refute.same(Date, lolex.timers.Date);
});

it("resets faked methods", function () {
this.clock = lolex.install(0, ["setTimeout", "Date", "setImmediate"]);
this.clock = lolex.install({ now: 0, toFake: ["setTimeout", "Date", "setImmediate"] });
this.clock.uninstall();

assert.same(setTimeout, lolex.timers.setTimeout);
assert.same(Date, lolex.timers.Date);
});

it("does not fake methods not provided", function () {
this.clock = lolex.install(0, ["setTimeout", "Date", "setImmediate"]);
this.clock = lolex.install({ now: 0, toFake: ["setTimeout", "Date", "setImmediate"] });

assert.same(clearTimeout, lolex.timers.clearTimeout);
assert.same(setInterval, lolex.timers.setInterval);
Expand Down

0 comments on commit d0b5dae

Please sign in to comment.