Skip to content

Commit 1d6b65a

Browse files
committed
fix: unit test
1 parent cfa83ea commit 1d6b65a

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"luxon": "3.x",
7676
"mockdate": "^3.0.2",
7777
"moment": "^2.24.0",
78+
"moment-timezone": "^0.5.45",
7879
"np": "^10.0.2",
7980
"prettier": "^3.1.0",
8081
"rc-test": "^7.0.9",

tests/generateWithTZ.spec.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import dayjsGenerateConfig from '../src/generate/dayjs';
44
import dayjs from 'dayjs';
55
import utc from 'dayjs/plugin/utc';
66
import timezone from 'dayjs/plugin/timezone';
7+
import moment from 'moment-timezone';
78

89
dayjs.extend(utc);
910
dayjs.extend(timezone);
@@ -12,7 +13,7 @@ const CN = 'Asia/Shanghai';
1213
const JP = 'Asia/Tokyo';
1314

1415
beforeEach(() => {
15-
MockDate.set(dayjs.tz('2024-09-23 05:02:03.172', CN).toDate());
16+
MockDate.set(new Date());
1617
});
1718

1819
afterEach(() => {
@@ -22,16 +23,18 @@ afterEach(() => {
2223

2324
describe('dayjs: getNow', () => {
2425
it('normal', () => {
25-
const now = new Date();
26-
expect(now.toDateString()).toEqual('Mon Sep 23 2024');
27-
expect(now.toTimeString()).toContain('05:02:03 GMT+0800');
26+
const D_now = dayjsGenerateConfig.getNow();
27+
const M_now = moment();
28+
29+
expect(D_now.format()).toEqual(M_now.format());
2830
});
2931

30-
it('should be work', async () => {
32+
it('should work normally in timezone', async () => {
3133
dayjs.tz.setDefault(JP);
32-
const now = dayjsGenerateConfig.getNow();
33-
const L_now = dayjs();
34-
expect(L_now.format()).toEqual('2024-09-23T05:02:03+08:00');
35-
expect(now.format()).toEqual('2024-09-23T06:02:03+09:00');
34+
const D_now = dayjsGenerateConfig.getNow();
35+
const M_now = moment().tz(JP);
36+
37+
expect(D_now.format()).toEqual(M_now.format());
38+
expect(D_now.get('hour') - D_now.utc().get('hour')).toEqual(9);
3639
});
3740
});

0 commit comments

Comments
 (0)