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

Commit 38e6d52

Browse files
committed
fix(test): fix #1069, FakeDate should handle constructor parameter
1 parent a86bddb commit 38e6d52

File tree

3 files changed

+100
-27
lines changed

3 files changed

+100
-27
lines changed

lib/zone-spec/fake-async-test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,18 @@
3232
const OriginalDate = global.Date;
3333
class FakeDate {
3434
constructor() {
35-
const d = new OriginalDate();
36-
d.setTime(global.Date.now());
37-
return d;
35+
const args = Array.prototype.slice.call(arguments);
36+
if (args.length === 0) {
37+
const d = new OriginalDate(args);
38+
d.setTime(global.Date.now());
39+
return d;
40+
} else {
41+
return new OriginalDate(...args);
42+
}
3843
}
3944

4045
static UTC() {
41-
return OriginalDate.UTC();
46+
return OriginalDate.UTC.apply(this, arguments);
4247
}
4348

4449
static now() {
@@ -50,7 +55,7 @@
5055
}
5156

5257
static parse() {
53-
return OriginalDate.parse();
58+
return OriginalDate.parse.apply(this, arguments);
5459
}
5560
}
5661

test/test-env-setup-mocha.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ declare const global: any;
8181
throw new Error(`Expected ${expected} to be greater than ${actual}`);
8282
}
8383
},
84+
toBeLessThan: function(actual: number) {
85+
if (expected >= actual) {
86+
throw new Error(`Expected ${expected} to be lesser than ${actual}`);
87+
}
88+
},
8489
toBeDefined: function() {
8590
if (!expected) {
8691
throw new Error(`Expected ${expected} to be defined`);
@@ -159,7 +164,11 @@ declare const global: any;
159164
if (expected > actual) {
160165
throw new Error(`Expected ${expected} not to be greater than ${actual}`);
161166
}
162-
167+
},
168+
toBeLessThan: function(actual: number) {
169+
if (expected < actual) {
170+
throw new Error(`Expected ${expected} not to be lesser than ${actual}`);
171+
}
163172
},
164173
toHaveBeenCalledWith: function(params: any[]) {
165174
if (!eq(expected.callArgs, params)) {

test/zone-spec/fake-async-test.spec.ts

Lines changed: 80 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,32 @@ describe('FakeAsyncTestZoneSpec', () => {
913913
expect(d instanceof Date).toBe(true);
914914
});
915915
});
916+
917+
it('should new Date with parameter correctly', () => {
918+
fakeAsyncTestZone.run(() => {
919+
const d: Date = new Date(0);
920+
expect(d.getFullYear()).toBeLessThan(1971);
921+
const d1: Date = new Date('December 17, 1995 03:24:00');
922+
expect(d1.getFullYear()).toEqual(1995);
923+
const d2: Date = new Date(1995, 11, 17, 3, 24, 0);
924+
expect(d2.getFullYear()).toEqual(1995);
925+
});
926+
});
927+
928+
it('should get Date.UTC() correctly', () => {
929+
fakeAsyncTestZone.run(() => {
930+
const utcDate = new Date(Date.UTC(96, 11, 1, 0, 0, 0));
931+
expect(utcDate.getFullYear()).toBe(1996);
932+
});
933+
});
934+
935+
it('should call Date.parse() correctly', () => {
936+
fakeAsyncTestZone.run(() => {
937+
const unixTimeZero = Date.parse('01 Jan 1970 00:00:00 GMT');
938+
expect(unixTimeZero).toBe(0);
939+
});
940+
});
941+
916942
});
917943

918944
describe(
@@ -932,31 +958,31 @@ describe('FakeAsyncTestZoneSpec', () => {
932958
});
933959

934960
it('should check date type correctly', fakeAsync(() => {
935-
const d: any = new Date();
936-
expect(d instanceof Date).toBe(true);
937-
}));
961+
const d: any = new Date();
962+
expect(d instanceof Date).toBe(true);
963+
}));
938964

939965
it('should mock date correctly', fakeAsync(() => {
940-
const baseTime = new Date(2013, 9, 23);
941-
jasmine.clock().mockDate(baseTime);
942-
const start = Date.now();
943-
expect(start).toBe(baseTime.getTime());
944-
jasmine.clock().tick(100);
945-
const end = Date.now();
946-
expect(end - start).toBe(100);
947-
expect(end).toBe(baseTime.getTime() + 100);
948-
}));
966+
const baseTime = new Date(2013, 9, 23);
967+
jasmine.clock().mockDate(baseTime);
968+
const start = Date.now();
969+
expect(start).toBe(baseTime.getTime());
970+
jasmine.clock().tick(100);
971+
const end = Date.now();
972+
expect(end - start).toBe(100);
973+
expect(end).toBe(baseTime.getTime() + 100);
974+
}));
949975

950976
it('should handle new Date correctly', fakeAsync(() => {
951-
const baseTime = new Date(2013, 9, 23);
952-
jasmine.clock().mockDate(baseTime);
953-
const start = new Date();
954-
expect(start.getTime()).toBe(baseTime.getTime());
955-
jasmine.clock().tick(100);
956-
const end = new Date();
957-
expect(end.getTime() - start.getTime()).toBe(100);
958-
expect(end.getTime()).toBe(baseTime.getTime() + 100);
959-
}));
977+
const baseTime = new Date(2013, 9, 23);
978+
jasmine.clock().mockDate(baseTime);
979+
const start = new Date();
980+
expect(start.getTime()).toBe(baseTime.getTime());
981+
jasmine.clock().tick(100);
982+
const end = new Date();
983+
expect(end.getTime() - start.getTime()).toBe(100);
984+
expect(end.getTime()).toBe(baseTime.getTime() + 100);
985+
}));
960986
}));
961987

962988
describe('fakeAsyncTest should patch jasmine.clock', ifEnvSupports(supportClock, () => {
@@ -1427,6 +1453,39 @@ const {fakeAsync, tick, discardPeriodicTasks, flush, flushMicrotasks} = fakeAsyn
14271453
expect(zoneInTest1).toBe(zoneInBeforeEach);
14281454
}));
14291455
});
1456+
1457+
describe('fakeAsync should work with Date', () => {
1458+
it('should get date diff correctly', fakeAsync(() => {
1459+
const start = Date.now();
1460+
tick(100);
1461+
const end = Date.now();
1462+
expect(end - start).toBe(100);
1463+
}));
1464+
1465+
it('should check date type correctly', fakeAsync(() => {
1466+
const d: any = new Date();
1467+
expect(d instanceof Date).toBe(true);
1468+
}));
1469+
1470+
it('should new Date with parameter correctly', fakeAsync(() => {
1471+
const d: Date = new Date(0);
1472+
expect(d.getFullYear()).toBeLessThan(1971);
1473+
const d1: Date = new Date('December 17, 1995 03:24:00');
1474+
expect(d1.getFullYear()).toEqual(1995);
1475+
const d2: Date = new Date(1995, 11, 17, 3, 24, 0);
1476+
expect(d2.getFullYear()).toEqual(1995);
1477+
}));
1478+
1479+
it('should get Date.UTC() correctly', fakeAsync(() => {
1480+
const utcDate = new Date(Date.UTC(96, 11, 1, 0, 0, 0));
1481+
expect(utcDate.getFullYear()).toBe(1996);
1482+
}));
1483+
1484+
it('should call Date.parse() correctly', fakeAsync(() => {
1485+
const unixTimeZero = Date.parse('01 Jan 1970 00:00:00 GMT');
1486+
expect(unixTimeZero).toBe(0);
1487+
}));
1488+
});
14301489
});
14311490

14321491
describe('ProxyZone', () => {

0 commit comments

Comments
 (0)