1
- import moment from 'moment'
2
1
import MockDate from 'mockdate'
2
+ import moment from 'moment'
3
3
import dayjs from '../../src'
4
4
import objectSupport from '../../src/plugin/objectSupport'
5
5
import quarterOfYear from '../../src/plugin/quarterOfYear'
6
6
import utc from '../../src/plugin/utc'
7
+ import utils from '../../src/utils'
7
8
8
9
dayjs . extend ( utc )
9
10
dayjs . extend ( quarterOfYear )
@@ -17,40 +18,43 @@ afterEach(() => {
17
18
MockDate . reset ( )
18
19
} )
19
20
const now = new Date ( )
21
+ const currentYear = now . getFullYear ( )
22
+ const currentMonth = utils . s ( now . getMonth ( ) + 1 , 2 , '0' )
23
+ const currentDate = utils . s ( now . getDate ( ) , 2 , '0' )
20
24
const fmt = 'YYYY-MM-DD HH:mm:ss.SSS'
21
25
const tests = [
22
26
[ { year : 2010 } , '2010-01-01 00:00:00.000' ] ,
23
- [ { year : 2010 , month : 1 } , '2010-01 -01 00:00:00.000' ] ,
24
- [ { year : 2010 , month : 1 , day : 12 } , '2010-01 -12 00:00:00.000' ] ,
25
- [ { year : 2010 , month : 1 , date : 12 } , '2010-01 -12 00:00:00.000' ] ,
27
+ [ { year : 2010 , month : 1 } , '2010-02 -01 00:00:00.000' ] ,
28
+ [ { year : 2010 , month : 1 , day : 12 } , '2010-02 -12 00:00:00.000' ] ,
29
+ [ { year : 2010 , month : 1 , date : 12 } , '2010-02 -12 00:00:00.000' ] ,
26
30
[
27
31
{
28
32
hour : 15 , minute : 25 , second : 50 , millisecond : 125
29
33
} ,
30
- '1970-01-01 15:25:50.125' ] ,
34
+ ` ${ currentYear } - ${ currentMonth } - ${ currentDate } 15:25:50.125` ] ,
31
35
[
32
36
{
33
37
year : 2010 , month : 1 , day : 12 , hours : 1
34
38
} ,
35
- '2010-01 -12 01:00:00.000'
39
+ '2010-02 -12 01:00:00.000'
36
40
] ,
37
41
[
38
42
{
39
43
year : 2010 , month : 1 , date : 12 , hours : 1
40
44
} ,
41
- '2010-01 -12 01:00:00.000'
45
+ '2010-02 -12 01:00:00.000'
42
46
] ,
43
47
[
44
48
{
45
49
year : 2010 , month : 1 , day : 12 , hours : 1 , minutes : 1
46
50
} ,
47
- '2010-01 -12 01:01:00.000'
51
+ '2010-02 -12 01:01:00.000'
48
52
] ,
49
53
[
50
54
{
51
55
year : 2010 , month : 1 , date : 12 , hours : 1 , minutes : 1
52
56
} ,
53
- '2010-01 -12 01:01:00.000'
57
+ '2010-02 -12 01:01:00.000'
54
58
] ,
55
59
[
56
60
{
@@ -61,7 +65,7 @@ const tests = [
61
65
minutes : 1 ,
62
66
seconds : 1
63
67
} ,
64
- '2010-01 -12 01:01:01.000'
68
+ '2010-02 -12 01:01:01.000'
65
69
] ,
66
70
[
67
71
{
@@ -73,7 +77,7 @@ const tests = [
73
77
seconds : 1 ,
74
78
milliseconds : 1
75
79
} ,
76
- '2010-01 -12 01:01:01.001'
80
+ '2010-02 -12 01:01:01.001'
77
81
] ,
78
82
[
79
83
{
@@ -85,7 +89,7 @@ const tests = [
85
89
seconds : 50 ,
86
90
milliseconds : 125
87
91
} ,
88
- '2010-01 -14 15:25:50.125'
92
+ '2010-02 -14 15:25:50.125'
89
93
] ,
90
94
[
91
95
{
@@ -97,24 +101,26 @@ const tests = [
97
101
second : 50 ,
98
102
millisecond : 125
99
103
} ,
100
- '2010-01 -14 15:25:50.125'
104
+ '2010-02 -14 15:25:50.125'
101
105
] ,
102
106
[
103
107
{
104
108
y : 2010 , M : 1 , d : 14 , h : 15 , m : 25 , s : 50 , ms : 125
105
109
} ,
106
- '2010-01 -14 15:25:50.125'
110
+ '2010-02 -14 15:25:50.125'
107
111
]
108
112
]
109
113
it ( 'Constructor from Object' , ( ) => {
110
114
for ( let i = 0 ; i < tests . length ; i += 1 ) {
111
115
expect ( dayjs ( tests [ i ] [ 0 ] ) . format ( fmt ) ) . toBe ( tests [ i ] [ 1 ] )
116
+ expect ( moment ( tests [ i ] [ 0 ] ) . format ( fmt ) ) . toBe ( tests [ i ] [ 1 ] )
112
117
}
113
118
} )
114
119
115
120
it ( 'Constructor from Object UTC' , ( ) => {
116
121
for ( let i = 0 ; i < tests . length ; i += 1 ) {
117
122
expect ( dayjs . utc ( tests [ i ] [ 0 ] ) . format ( fmt ) ) . toBe ( tests [ i ] [ 1 ] )
123
+ expect ( moment . utc ( tests [ i ] [ 0 ] ) . format ( fmt ) ) . toBe ( tests [ i ] [ 1 ] )
118
124
}
119
125
} )
120
126
it ( 'Set from Object' , ( ) => {
@@ -139,9 +145,28 @@ it('add short reverse args', () => {
139
145
expect ( a . add ( { h : 1 } ) . hour ( ) ) . toBe ( 7 )
140
146
expect ( a . add ( { d : 1 } ) . date ( ) ) . toBe ( 13 )
141
147
expect ( a . add ( { w : 1 } ) . date ( ) ) . toBe ( 19 )
142
- expect ( a . add ( { M : 1 } ) . month ( ) ) . toBe ( 9 )
148
+ expect ( a . add ( { M : 1 } ) . month ( ) ) . toBe ( 10 )
143
149
expect ( a . add ( { y : 1 } ) . year ( ) ) . toBe ( 2012 )
144
- expect ( a . add ( { Q : 1 } ) . month ( ) ) . toBe ( 11 )
150
+ expect ( a . add ( { Q : 1 } ) . month ( ) ) . toBe ( 0 )
151
+
152
+ const aM = moment ( {
153
+ year : 2011 ,
154
+ month : 9 ,
155
+ date : 12 ,
156
+ hour : 6 ,
157
+ minute : 7 ,
158
+ second : 8 ,
159
+ millisecond : 500
160
+ } )
161
+ expect ( aM . clone ( ) . add ( { ms : 50 } ) . millisecond ( ) ) . toBe ( 550 )
162
+ expect ( aM . clone ( ) . add ( { s : 1 } ) . second ( ) ) . toBe ( 9 )
163
+ expect ( aM . clone ( ) . add ( { m : 1 } ) . minute ( ) ) . toBe ( 8 )
164
+ expect ( aM . clone ( ) . add ( { h : 1 } ) . hour ( ) ) . toBe ( 7 )
165
+ expect ( aM . clone ( ) . add ( { d : 1 } ) . date ( ) ) . toBe ( 13 )
166
+ expect ( aM . clone ( ) . add ( { w : 1 } ) . date ( ) ) . toBe ( 19 )
167
+ expect ( aM . clone ( ) . add ( { M : 1 } ) . month ( ) ) . toBe ( 10 )
168
+ expect ( aM . clone ( ) . add ( { y : 1 } ) . year ( ) ) . toBe ( 2012 )
169
+ expect ( aM . clone ( ) . add ( { Q : 1 } ) . month ( ) ) . toBe ( 0 )
145
170
146
171
const b = dayjs ( [ 2010 , 1 , 31 ] ) . add ( { M : 1 } )
147
172
const c = dayjs ( [ 2010 , 2 , 28 ] ) . subtract ( { M : 1 } )
@@ -173,9 +198,9 @@ it('add long reverse args', () => {
173
198
expect ( a . add ( { hours : 1 } ) . hour ( ) ) . toBe ( 7 )
174
199
expect ( a . add ( { days : 1 } ) . date ( ) ) . toBe ( 13 )
175
200
expect ( a . add ( { weeks : 1 } ) . date ( ) ) . toBe ( 19 )
176
- expect ( a . add ( { months : 1 } ) . month ( ) ) . toBe ( 9 )
201
+ expect ( a . add ( { months : 1 } ) . month ( ) ) . toBe ( 10 )
177
202
expect ( a . add ( { years : 1 } ) . year ( ) ) . toBe ( 2012 )
178
- expect ( a . add ( { quarters : 1 } ) . month ( ) ) . toBe ( 11 )
203
+ expect ( a . add ( { quarters : 1 } ) . month ( ) ) . toBe ( 0 )
179
204
} )
180
205
181
206
it ( 'add long singular reverse args' , ( ) => {
@@ -195,9 +220,9 @@ it('add long singular reverse args', () => {
195
220
expect ( a . add ( { hour : 1 } ) . hour ( ) ) . toBe ( 7 )
196
221
expect ( a . add ( { day : 1 } ) . date ( ) ) . toBe ( 13 )
197
222
expect ( a . add ( { week : 1 } ) . date ( ) ) . toBe ( 19 )
198
- expect ( a . add ( { month : 1 } ) . month ( ) ) . toBe ( 9 )
223
+ expect ( a . add ( { month : 1 } ) . month ( ) ) . toBe ( 10 )
199
224
expect ( a . add ( { year : 1 } ) . year ( ) ) . toBe ( 2012 )
200
- expect ( a . add ( { quarter : 1 } ) . month ( ) ) . toBe ( 11 )
225
+ expect ( a . add ( { quarter : 1 } ) . month ( ) ) . toBe ( 0 )
201
226
} )
202
227
203
228
it ( 'add string long' , ( ) => {
@@ -217,9 +242,9 @@ it('add string long', () => {
217
242
expect ( a . add ( 1 , 'hour' ) . hour ( ) ) . toBe ( 7 )
218
243
expect ( a . add ( 1 , 'day' ) . date ( ) ) . toBe ( 13 )
219
244
expect ( a . add ( 1 , 'week' ) . date ( ) ) . toBe ( 19 )
220
- expect ( a . add ( 1 , 'month' ) . month ( ) ) . toBe ( 9 )
245
+ expect ( a . add ( 1 , 'month' ) . month ( ) ) . toBe ( 10 )
221
246
expect ( a . add ( 1 , 'year' ) . year ( ) ) . toBe ( 2012 )
222
- expect ( a . add ( 1 , 'quarter' ) . month ( ) ) . toBe ( 11 )
247
+ expect ( a . add ( 1 , 'quarter' ) . month ( ) ) . toBe ( 0 )
223
248
} )
224
249
225
250
it ( 'add string long singular' , ( ) => {
@@ -239,9 +264,9 @@ it('add string long singular', () => {
239
264
expect ( a . add ( 1 , 'hours' ) . hour ( ) ) . toBe ( 7 )
240
265
expect ( a . add ( 1 , 'days' ) . date ( ) ) . toBe ( 13 )
241
266
expect ( a . add ( 1 , 'weeks' ) . date ( ) ) . toBe ( 19 )
242
- expect ( a . add ( 1 , 'months' ) . month ( ) ) . toBe ( 9 )
267
+ expect ( a . add ( 1 , 'months' ) . month ( ) ) . toBe ( 10 )
243
268
expect ( a . add ( 1 , 'years' ) . year ( ) ) . toBe ( 2012 )
244
- expect ( a . add ( 1 , 'quarters' ) . month ( ) ) . toBe ( 11 )
269
+ expect ( a . add ( 1 , 'quarters' ) . month ( ) ) . toBe ( 0 )
245
270
} )
246
271
247
272
it ( 'add string short' , ( ) => {
@@ -261,9 +286,9 @@ it('add string short', () => {
261
286
expect ( a . add ( 1 , 'h' ) . hour ( ) ) . toBe ( 7 )
262
287
expect ( a . add ( 1 , 'd' ) . date ( ) ) . toBe ( 13 )
263
288
expect ( a . add ( 1 , 'w' ) . date ( ) ) . toBe ( 19 )
264
- expect ( a . add ( 1 , 'M' ) . month ( ) ) . toBe ( 9 )
289
+ expect ( a . add ( 1 , 'M' ) . month ( ) ) . toBe ( 10 )
265
290
expect ( a . add ( 1 , 'y' ) . year ( ) ) . toBe ( 2012 )
266
- expect ( a . add ( 1 , 'Q' ) . month ( ) ) . toBe ( 11 )
291
+ expect ( a . add ( 1 , 'Q' ) . month ( ) ) . toBe ( 0 )
267
292
} )
268
293
269
294
it ( 'add strings string short' , ( ) => {
@@ -283,9 +308,9 @@ it('add strings string short', () => {
283
308
expect ( a . add ( '1' , 'h' ) . hour ( ) ) . toBe ( 7 )
284
309
expect ( a . add ( '1' , 'd' ) . date ( ) ) . toBe ( 13 )
285
310
expect ( a . add ( '1' , 'w' ) . date ( ) ) . toBe ( 19 )
286
- expect ( a . add ( '1' , 'M' ) . month ( ) ) . toBe ( 9 )
311
+ expect ( a . add ( '1' , 'M' ) . month ( ) ) . toBe ( 10 )
287
312
expect ( a . add ( '1' , 'y' ) . year ( ) ) . toBe ( 2012 )
288
- expect ( a . add ( '1' , 'Q' ) . month ( ) ) . toBe ( 11 )
313
+ expect ( a . add ( '1' , 'Q' ) . month ( ) ) . toBe ( 0 )
289
314
} )
290
315
291
316
it ( 'add no string with milliseconds default' , ( ) => {
@@ -318,9 +343,9 @@ it('subtract strings string short', () => {
318
343
expect ( a . subtract ( '1' , 'h' ) . hour ( ) ) . toBe ( 5 )
319
344
expect ( a . subtract ( '1' , 'd' ) . date ( ) ) . toBe ( 11 )
320
345
expect ( a . subtract ( '1' , 'w' ) . date ( ) ) . toBe ( 5 )
321
- expect ( a . subtract ( '1' , 'M' ) . month ( ) ) . toBe ( 7 )
346
+ expect ( a . subtract ( '1' , 'M' ) . month ( ) ) . toBe ( 8 )
322
347
expect ( a . subtract ( '1' , 'y' ) . year ( ) ) . toBe ( 2010 )
323
- expect ( a . subtract ( '1' , 'Q' ) . month ( ) ) . toBe ( 5 )
348
+ expect ( a . subtract ( '1' , 'Q' ) . month ( ) ) . toBe ( 6 )
324
349
} )
325
350
326
351
it ( 'add decimal values of days and months' , ( ) => {
0 commit comments