Skip to content

Commit

Permalink
breaking timezone function
Browse files Browse the repository at this point in the history
- travel is called by default #3
- signature also takes travel arguments
  • Loading branch information
paed01 committed Aug 9, 2023
1 parent aca7ae2 commit 68ec055
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 18 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Mock time and date for traveling and freezing. Inspired and borrowed from [timek
- [`defrost()`](#defrost)
- [`reset()`](#reset)
- [`isKeepingTime()`](#iskeepingtime)
- [`timezone(timeZone)`](#timezonetimezone)
- [`timezone(timeZone[, ...args])`](#timezonetimezone-args)
- [`new TimeZoneTraveller(timeZone)`](#new-timezonetravellertimezone)
- [`timezone.freeze([...args])`](#timezonefreezeargs)
- [`timezone.travel([...args])`](#timezonetravelargs)
Expand Down Expand Up @@ -168,15 +168,23 @@ ck.travel(1893448800000);
console.log(ck.isKeepingTime() ? 'Is' : 'Not', 'keeping time');
```

## `timezone(timeZone)`
## `timezone(timeZone[, ...args])`

Freeze and travel in different time zones. Returns [`TimeZoneTraveller` api](#new-timezonetravellertimezone)
Travel to time zone.

- `timeZone`: IANA time zone string
- `...args`: Optional travel to date arguments

Returns [`TimeZoneTraveller` api](#new-timezonetravellertimezone)

```javascript
import * as ck from 'chronokinesis';

const tz = ck.timezone('Asia/Shanghai');

// Now in Shanghai
console.log(new Date())

tz.freeze();
```

Expand Down
8 changes: 5 additions & 3 deletions dist/chronokinesis.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

FakeDate.prototype = NativeDate.prototype;

FakeDate.now = function() {
FakeDate.now = function fakeNow() {
if (freezedAt) return freezedAt.getTime();
return time();
};
Expand Down Expand Up @@ -139,8 +139,10 @@
return travel(this.getTime(...args));
};

function timezone(timeZone) {
return new TimeZoneTraveller(timeZone);
function timezone(timeZone, ...args) {
const tz = new TimeZoneTraveller(timeZone);
tz.travel(...args);
return tz;
}

function useFakeDate() {
Expand Down
8 changes: 5 additions & 3 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ FakeDate.parse = NativeDate.parse;

FakeDate.prototype = NativeDate.prototype;

FakeDate.now = function() {
FakeDate.now = function fakeNow() {
if (freezedAt) return freezedAt.getTime();
return time();
};
Expand Down Expand Up @@ -135,8 +135,10 @@ TimeZoneTraveller.prototype.travel = function timeZoneTravel(...args) {
return travel(this.getTime(...args));
};

function timezone(timeZone) {
return new TimeZoneTraveller(timeZone);
function timezone(timeZone, ...args) {
const tz = new TimeZoneTraveller(timeZone);
tz.travel(...args);
return tz;
}

function useFakeDate() {
Expand Down
9 changes: 5 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class TimeZoneTraveller {
* @param timeZone IANA time zone
*/
constructor(timeZone: string);
/** Time zone */
/** IANA time zone */
readonly timeZone: string;
defrost: typeof defrost;
reset: typeof reset;
Expand All @@ -47,7 +47,8 @@ export class TimeZoneTraveller {
}

/**
* Freeze and travel in different time zones.
* @param timeZone string IANA time zone
* Travel to time zone.
* @param timeZone IANA time zone
* @param args Optional travel to date arguments
*/
export function timezone(timeZone: string): TimeZoneTraveller;
export function timezone(timeZone: string, ...args: any[]): TimeZoneTraveller;
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ FakeDate.parse = NativeDate.parse;

FakeDate.prototype = NativeDate.prototype;

FakeDate.now = function() {
FakeDate.now = function fakeNow() {
if (freezedAt) return freezedAt.getTime();
return time();
};
Expand Down Expand Up @@ -133,8 +133,10 @@ TimeZoneTraveller.prototype.travel = function timeZoneTravel(...args) {
return travel(this.getTime(...args));
};

export function timezone(timeZone) {
return new TimeZoneTraveller(timeZone);
export function timezone(timeZone, ...args) {
const tz = new TimeZoneTraveller(timeZone);
tz.travel(...args);
return tz;
}

function useFakeDate() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chronokinesis",
"version": "5.1.0",
"version": "6.0.0",
"description": "Module for testing time-dependent code",
"author": {
"name": "Pål Edman",
Expand Down
28 changes: 27 additions & 1 deletion test/timezone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,37 @@ describe('chronokinesis', () => {
it('throws if timezone is unresolved', () => {
expect(() => ck.timezone('Moon/Crater')).to.throw(RangeError, /Moon\/Crater/i);
});

it('immediately starts travelling in time zone', () => {
ck.timezone('Europe/Helsinki');
expect(ck.isKeepingTime()).to.be.true;
const fdate = new Date();

ck.timezone('Europe/Stockholm');
expect(ck.isKeepingTime()).to.be.true;
const sdate = new Date();

expect(sdate.getHours() - fdate.getHours()).to.be.above(0);
});

it('arguments are passed to time zone travel function', () => {
const utc = Date.UTC(2021, 7, 26, 18, 0);

ck.timezone('Europe/Helsinki', 2021, 7, 26, 18, 0);
const fdate = new Date();

ck.timezone('Europe/Stockholm', 2021, 7, 26, 18, 0);
const sdate = new Date();

expect(diffHrs(utc, fdate), 'Finland').to.equal(-3);
expect(diffHrs(utc, sdate), 'Sweden').to.equal(-2);

expect(diffHrs(fdate, sdate)).to.equal(1);
});
});

describe('#freeze in timezone', () => {
beforeEach(ck.reset);

it('with arguments freezes at specified time in timezone', () => {
const utc = Date.UTC(2021, 7, 26, 15, 0);

Expand Down

0 comments on commit 68ec055

Please sign in to comment.