Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix(test): fix #1069, FakeDate should handle constructor parameter (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion authored and mhevery committed Apr 6, 2018
1 parent a86bddb commit b3fdd7e
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 36 deletions.
45 changes: 26 additions & 19 deletions lib/jasmine/jasmine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@
return originalJasmineFn.apply(this, arguments);
};
});
if (enableClockPatch) {
const originalClockFn: Function = ((jasmine as any)[symbol('clock')] = jasmine['clock']);
(jasmine as any)['clock'] = function() {
const clock = originalClockFn.apply(this, arguments);

// need to patch jasmine.clock().mockDate and jasmine.clock().tick() so
// they can work properly in FakeAsyncTest
const originalClockFn: Function = ((jasmine as any)[symbol('clock')] = jasmine['clock']);
(jasmine as any)['clock'] = function() {
const clock = originalClockFn.apply(this, arguments);
if (!clock[symbol('patched')]) {
clock[symbol('patched')] = symbol('patched');
const originalTick = (clock[symbol('tick')] = clock.tick);
clock.tick = function() {
const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
Expand All @@ -83,28 +87,31 @@
clock.mockDate = function() {
const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
if (fakeAsyncZoneSpec) {
const dateTime = arguments[0];
const dateTime = arguments.length > 0 ? arguments[0] : new Date();
return fakeAsyncZoneSpec.setCurrentRealTime.apply(
fakeAsyncZoneSpec,
dateTime && typeof dateTime.getTime === 'function' ? [dateTime.getTime()] :
arguments);
}
return originalMockDate.apply(this, arguments);
};
['install', 'uninstall'].forEach(methodName => {
const originalClockFn: Function = (clock[symbol(methodName)] = clock[methodName]);
clock[methodName] = function() {
const FakeAsyncTestZoneSpec = (Zone as any)['FakeAsyncTestZoneSpec'];
if (FakeAsyncTestZoneSpec) {
(jasmine as any)[symbol('clockInstalled')] = 'install' === methodName;
return;
}
return originalClockFn.apply(this, arguments);
};
});
return clock;
};
}
// for auto go into fakeAsync feature, we need the flag to enable it
if (enableClockPatch) {
['install', 'uninstall'].forEach(methodName => {
const originalClockFn: Function = (clock[symbol(methodName)] = clock[methodName]);
clock[methodName] = function() {
const FakeAsyncTestZoneSpec = (Zone as any)['FakeAsyncTestZoneSpec'];
if (FakeAsyncTestZoneSpec) {
(jasmine as any)[symbol('clockInstalled')] = 'install' === methodName;
return;
}
return originalClockFn.apply(this, arguments);
};
});
}
}
return clock;
};

/**
* Gets a function wrapping the body of a Jasmine `describe` block to execute in a
Expand Down
48 changes: 36 additions & 12 deletions lib/zone-spec/fake-async-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
const OriginalDate = global.Date;
class FakeDate {
constructor() {
const d = new OriginalDate();
d.setTime(global.Date.now());
return d;
}

static UTC() {
return OriginalDate.UTC();
if (arguments.length === 0) {
const d = new OriginalDate();
d.setTime(FakeDate.now());
return d;
} else {
const args = Array.prototype.slice.call(arguments);
return new OriginalDate(...args);
}
}

static now() {
Expand All @@ -48,12 +49,19 @@
}
return OriginalDate.now.apply(this, arguments);
}

static parse() {
return OriginalDate.parse();
}
}

(FakeDate as any).UTC = OriginalDate.UTC;
(FakeDate as any).parse = OriginalDate.parse;

// keep a reference for zone patched timer function
const timers = {
setTimeout: global.setTimeout,
setInterval: global.setInterval,
clearTimeout: global.clearTimeout,
clearInterval: global.clearInterval
};

class Scheduler {
// Next scheduler id.
public nextId: number = 1;
Expand All @@ -63,7 +71,7 @@
// Current simulated time in millis.
private _currentTime: number = 0;
// Current real time in millis.
private _currentRealTime: number = Date.now();
private _currentRealTime: number = OriginalDate.now();

constructor() {}

Expand Down Expand Up @@ -341,6 +349,11 @@
}
global['Date'] = FakeDate;
FakeDate.prototype = OriginalDate.prototype;

// try check and reset timers
// because jasmine.clock().install() may
// have replaced the global timer
FakeAsyncTestZoneSpec.checkTimerPatch();
}

static resetDate() {
Expand All @@ -349,6 +362,17 @@
}
}

static checkTimerPatch() {
if (global.setTimeout !== timers.setTimeout) {
global.setTimeout = timers.setTimeout;
global.clearTimeout = timers.clearTimeout;
}
if (global.setInterval !== timers.setInterval) {
global.setInterval = timers.setInterval;
global.clearInterval = timers.clearInterval;
}
}

lockDatePatch() {
this.patchDateLocked = true;
FakeAsyncTestZoneSpec.patchDate();
Expand Down
16 changes: 15 additions & 1 deletion test/test-env-setup-mocha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ declare const global: any;
throw new Error(`Expected ${expected} to be greater than ${actual}`);
}
},
toBeLessThan: function(actual: number) {
if (expected >= actual) {
throw new Error(`Expected ${expected} to be lesser than ${actual}`);
}
},
toBeDefined: function() {
if (!expected) {
throw new Error(`Expected ${expected} to be defined`);
Expand Down Expand Up @@ -109,6 +114,11 @@ declare const global: any;
throw new Error(`Expected ${expected} to be truthy`);
}
},
toBeFalsy: function(actual: any) {
if (!!actual) {
throw new Error(`Expected ${actual} to be falsy`);
}
},
toContain: function(actual: any) {
if (expected.indexOf(actual) === -1) {
throw new Error(`Expected ${expected} to contain ${actual}`);
Expand Down Expand Up @@ -159,7 +169,11 @@ declare const global: any;
if (expected > actual) {
throw new Error(`Expected ${expected} not to be greater than ${actual}`);
}

},
toBeLessThan: function(actual: number) {
if (expected < actual) {
throw new Error(`Expected ${expected} not to be lesser than ${actual}`);
}
},
toHaveBeenCalledWith: function(params: any[]) {
if (!eq(expected.callArgs, params)) {
Expand Down
Loading

0 comments on commit b3fdd7e

Please sign in to comment.