Skip to content

Commit

Permalink
fix #19, advanceBy firstly
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcc committed Jan 12, 2019
1 parent 9321739 commit 38b3b40
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
13 changes: 6 additions & 7 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
import { advanceBy, advanceTo, clear, version } from '../src';
import pkg from '../package.json';

beforeEach(() => {
advanceTo(); // advanceTo to 0 ms.
});
afterEach(() => {
clear();
});
describe('jest-date-mock', () => {
test('Date Class', () => {
advanceTo();
// Date isEqual
expect(new Date()).toEqual(new Date(0));

Expand Down Expand Up @@ -68,14 +66,15 @@ describe('jest-date-mock', () => {
});

test('advanceBy', () => {
advanceTo(1000);
advanceBy(3000); // advanceBy time 3 seconds
expect(+new Date()).toBe(4000);
const now = new Date();
advanceBy(4000);
expect(+new Date() - now).toBe(4000);

advanceBy(-4000); // advanceBy time -4 seconds
expect(+new Date()).toBe(0);
expect(+new Date() - now).toBe(0);
advanceBy();
expect(+new Date()).toBe(0);
expect(+new Date() - now).toBe(0);
});

test('clear', () => {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "jest-date-mock",
"version": "1.0.6",
"version": "1.0.7",
"description": "Mock `window.Date` when run unit test cases with jest. Make tests of `Date` easier.",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"scripts": {
"test": "jest",
"test": "jest --no-cache",
"build": "babel src --out-dir lib --copy-files && npm run test",
"coveralls": "cat ./coverage/lcov.info | coveralls"
},
Expand Down
3 changes: 2 additions & 1 deletion src/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Contract: [email protected]
*/

// undefined means unmock.
const DEFAULT = undefined;

// current Date of timestamp
Expand All @@ -12,7 +13,7 @@ let nowDate = DEFAULT;
* move date by offset `ms`
* @param ms
*/
export const advanceBy = ms => nowDate += (ms || 0);
export const advanceBy = ms => nowDate = (nowDate === undefined ? +new Date() : nowDate) + (ms || 0);

/**
* reset Date
Expand Down
4 changes: 2 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
* @param ms
* @default 0
*/
export declare function advanceBy(ms: number): void;
export declare function advanceBy(ms?: number): void;

/**
* Sets date to a timestamp or Date.
*
* @param ms
* @default 0
*/
export declare function advanceTo(ms?: number | Date): void;
export declare function advanceTo(ms?: number | Date | string): void;

/**
* Un-mocks the Date class.
Expand Down
1 change: 1 addition & 0 deletions src/mockDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { now } from './date';

export const mockDateClass = D => {
// if undefined, use real date, or else mock date
const mockNow = () => now() === undefined ? D.now() : now();

function MD(...args) {
Expand Down

0 comments on commit 38b3b40

Please sign in to comment.