Skip to content

Commit

Permalink
Correctly mock date.constructor (#28)
Browse files Browse the repository at this point in the history
* Correctly mock date.constructor

* Add more test cases

* test: change travis to github action (#29)

---------

Co-authored-by: hustcc <i@hust.cc>
  • Loading branch information
robaw and hustcc committed Mar 27, 2024
1 parent a567883 commit 83c8617
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
12 changes: 12 additions & 0 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@ describe('jest-date-mock', () => {
// Date isEqual
expect(new Date()).toEqual(new Date(0));

// .constructor()
expect(+new Date(1000).constructor()).toBe(0);
expect(+new Date().constructor(10000)).toBe(10000);
expect(new Date().constructor(10000)).toBeInstanceOf(Date);


// +
expect(+new Date()).toBe(0);
expect(+new Date(10000)).toBe(10000);


// getTime
expect(new Date().getTime()).toBe(0);
expect(new Date(10000).getTime()).toBe(10000);

// instanceof
expect(new Date()).toBeInstanceOf(Date);
expect(new Date().constructor(10000)).toBeInstanceOf(Date);

// 2018-05-27 08:00:00
expect(new Date(Date.UTC(2018, 5, 27, 0, 0, 0)).getTime()).toBe(1530057600000);
Expand Down Expand Up @@ -91,6 +99,9 @@ describe('jest-date-mock', () => {
expect(+new Date() - now).toBe(0);
advanceBy();
expect(+new Date() - now).toBe(0);

advanceBy(3000);
expect(+new Date().constructor() - now).toBe(3000); // .constructor()
});

test('clear', () => {
Expand All @@ -99,6 +110,7 @@ describe('jest-date-mock', () => {

advanceTo();
expect(+new Date()).toBe(0);
expect(+new Date().constructor()).toBe(0);
});

test('usage', () => {
Expand Down
1 change: 1 addition & 0 deletions src/mockDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const mockDateClass = D => {
const dateArgs = args.length === 0 ? [mockNow()] : args;
const instance = new D(...dateArgs);
Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
instance.constructor = Date;
return instance;
}

Expand Down

0 comments on commit 83c8617

Please sign in to comment.