Skip to content

Commit

Permalink
fix(test): fix angular#1069, FakeDate should handle constructor param…
Browse files Browse the repository at this point in the history
…eter
  • Loading branch information
JiaLiPassion committed Apr 5, 2018
1 parent a86bddb commit 3e44830
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 33 deletions.
22 changes: 11 additions & 11 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,11 @@
}
return OriginalDate.now.apply(this, arguments);
}

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

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

class Scheduler {
// Next scheduler id.
public nextId: number = 1;
Expand Down
11 changes: 10 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 @@ -159,7 +164,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
101 changes: 80 additions & 21 deletions test/zone-spec/fake-async-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,32 @@ describe('FakeAsyncTestZoneSpec', () => {
expect(d instanceof Date).toBe(true);
});
});

it('should new Date with parameter correctly', () => {
fakeAsyncTestZone.run(() => {
const d: Date = new Date(0);
expect(d.getFullYear()).toBeLessThan(1971);
const d1: Date = new Date('December 17, 1995 03:24:00');
expect(d1.getFullYear()).toEqual(1995);
const d2: Date = new Date(1995, 11, 17, 3, 24, 0);
expect(d2.getFullYear()).toEqual(1995);
});
});

it('should get Date.UTC() correctly', () => {
fakeAsyncTestZone.run(() => {
const utcDate = new Date(Date.UTC(96, 11, 1, 0, 0, 0));
expect(utcDate.getFullYear()).toBe(1996);
});
});

it('should call Date.parse() correctly', () => {
fakeAsyncTestZone.run(() => {
const unixTimeZero = Date.parse('01 Jan 1970 00:00:00 GMT');
expect(unixTimeZero).toBe(0);
});
});

});

describe(
Expand All @@ -932,31 +958,31 @@ describe('FakeAsyncTestZoneSpec', () => {
});

it('should check date type correctly', fakeAsync(() => {
const d: any = new Date();
expect(d instanceof Date).toBe(true);
}));
const d: any = new Date();
expect(d instanceof Date).toBe(true);
}));

it('should mock date correctly', fakeAsync(() => {
const baseTime = new Date(2013, 9, 23);
jasmine.clock().mockDate(baseTime);
const start = Date.now();
expect(start).toBe(baseTime.getTime());
jasmine.clock().tick(100);
const end = Date.now();
expect(end - start).toBe(100);
expect(end).toBe(baseTime.getTime() + 100);
}));
const baseTime = new Date(2013, 9, 23);
jasmine.clock().mockDate(baseTime);
const start = Date.now();
expect(start).toBe(baseTime.getTime());
jasmine.clock().tick(100);
const end = Date.now();
expect(end - start).toBe(100);
expect(end).toBe(baseTime.getTime() + 100);
}));

it('should handle new Date correctly', fakeAsync(() => {
const baseTime = new Date(2013, 9, 23);
jasmine.clock().mockDate(baseTime);
const start = new Date();
expect(start.getTime()).toBe(baseTime.getTime());
jasmine.clock().tick(100);
const end = new Date();
expect(end.getTime() - start.getTime()).toBe(100);
expect(end.getTime()).toBe(baseTime.getTime() + 100);
}));
const baseTime = new Date(2013, 9, 23);
jasmine.clock().mockDate(baseTime);
const start = new Date();
expect(start.getTime()).toBe(baseTime.getTime());
jasmine.clock().tick(100);
const end = new Date();
expect(end.getTime() - start.getTime()).toBe(100);
expect(end.getTime()).toBe(baseTime.getTime() + 100);
}));
}));

describe('fakeAsyncTest should patch jasmine.clock', ifEnvSupports(supportClock, () => {
Expand Down Expand Up @@ -1427,6 +1453,39 @@ const {fakeAsync, tick, discardPeriodicTasks, flush, flushMicrotasks} = fakeAsyn
expect(zoneInTest1).toBe(zoneInBeforeEach);
}));
});

describe('fakeAsync should work with Date', () => {
it('should get date diff correctly', fakeAsync(() => {
const start = Date.now();
tick(100);
const end = Date.now();
expect(end - start).toBe(100);
}));

it('should check date type correctly', fakeAsync(() => {
const d: any = new Date();
expect(d instanceof Date).toBe(true);
}));

it('should new Date with parameter correctly', fakeAsync(() => {
const d: Date = new Date(0);
expect(d.getFullYear()).toBeLessThan(1971);
const d1: Date = new Date('December 17, 1995 03:24:00');
expect(d1.getFullYear()).toEqual(1995);
const d2: Date = new Date(1995, 11, 17, 3, 24, 0);
expect(d2.getFullYear()).toEqual(1995);
}));

it('should get Date.UTC() correctly', fakeAsync(() => {
const utcDate = new Date(Date.UTC(96, 11, 1, 0, 0, 0));
expect(utcDate.getFullYear()).toBe(1996);
}));

it('should call Date.parse() correctly', fakeAsync(() => {
const unixTimeZero = Date.parse('01 Jan 1970 00:00:00 GMT');
expect(unixTimeZero).toBe(0);
}));
});
});

describe('ProxyZone', () => {
Expand Down

0 comments on commit 3e44830

Please sign in to comment.