@@ -59,15 +59,56 @@ it('Format Complex with other string - : / ', () => {
5959 expect ( dayjs ( ) . format ( string ) ) . toBe ( moment ( ) . format ( string ) )
6060} )
6161
62- it ( 'Difference' , ( ) => {
63- const dateString = '20110101'
64-
65- const dayjsA = dayjs ( )
66- const dayjsB = dayjs ( dateString )
67-
68- const momentA = moment ( )
69- const momentB = moment ( dateString )
70- expect ( dayjsA . diff ( dayjsB ) ) . toBe ( momentA . diff ( momentB ) )
62+ describe ( 'Difference' , ( ) => {
63+ it ( 'empty -> default milliseconds' , ( ) => {
64+ const dateString = '20110101'
65+ const dayjsA = dayjs ( )
66+ const dayjsB = dayjs ( dateString )
67+ const momentA = moment ( )
68+ const momentB = moment ( dateString )
69+ expect ( dayjsA . diff ( dayjsB ) ) . toBe ( momentA . diff ( momentB ) )
70+ } )
71+
72+ it ( 'diff -> none dayjs object' , ( ) => {
73+ const dateString = '2013-02-08'
74+ const dayjsA = dayjs ( )
75+ const dayjsB = new Date ( dateString )
76+ const momentA = moment ( )
77+ const momentB = new Date ( dateString )
78+ expect ( dayjsA . diff ( dayjsB ) ) . toBe ( momentA . diff ( momentB ) )
79+ } )
80+
81+ it ( 'diff -> in seconds, days, weeks, months, quarters, years ' , ( ) => {
82+ const dayjsA = dayjs ( )
83+ const dayjsB = dayjs ( ) . add ( 1000 , 'days' )
84+ const dayjsC = dayjs ( ) . subtract ( 1000 , 'days' )
85+ const momentA = moment ( )
86+ const momentB = moment ( ) . add ( 1000 , 'days' )
87+ const momentC = moment ( ) . subtract ( 1000 , 'days' )
88+ const units = [ 'seconds' , 'days' , 'weeks' , 'months' , 'quarters' , 'years' ]
89+ units . forEach ( ( unit ) => {
90+ expect ( dayjsA . diff ( dayjsB , unit ) ) . toBe ( momentA . diff ( momentB , unit ) )
91+ expect ( dayjsA . diff ( dayjsB , unit , true ) ) . toBe ( momentA . diff ( momentB , unit , true ) )
92+ expect ( dayjsA . diff ( dayjsC , unit ) ) . toBe ( momentA . diff ( momentC , unit ) )
93+ expect ( dayjsA . diff ( dayjsC , unit , true ) ) . toBe ( momentA . diff ( momentC , unit , true ) )
94+ } )
95+ } )
96+
97+ it ( 'Special diff in month according to moment.js' , ( ) => {
98+ const dayjsA = dayjs ( '20160115' )
99+ const dayjsB = dayjs ( '20160215' )
100+ const dayjsC = dayjs ( '20170115' )
101+ const momentA = moment ( '20160115' )
102+ const momentB = moment ( '20160215' )
103+ const momentC = moment ( '20170115' )
104+ const units = [ 'months' , 'quarters' , 'years' ]
105+ units . forEach ( ( unit ) => {
106+ expect ( dayjsA . diff ( dayjsB , unit ) ) . toBe ( momentA . diff ( momentB , unit ) )
107+ expect ( dayjsA . diff ( dayjsB , unit , true ) ) . toBe ( momentA . diff ( momentB , unit , true ) )
108+ expect ( dayjsA . diff ( dayjsC , unit ) ) . toBe ( momentA . diff ( momentC , unit ) )
109+ expect ( dayjsA . diff ( dayjsC , unit , true ) ) . toBe ( momentA . diff ( momentC , unit , true ) )
110+ } )
111+ } )
71112} )
72113
73114it ( 'Unix Timestamp (milliseconds)' , ( ) => {
@@ -80,9 +121,10 @@ it('Unix Timestamp (seconds)', () => {
80121
81122it ( 'Days in Month' , ( ) => {
82123 expect ( dayjs ( ) . daysInMonth ( ) ) . toBe ( moment ( ) . daysInMonth ( ) )
124+ expect ( dayjs ( '20140201' ) . daysInMonth ( ) ) . toBe ( moment ( '20140201' ) . daysInMonth ( ) )
83125} )
84126
85- it ( 'As Javascript Date' , ( ) => {
127+ it ( 'As Javascript Date -> toDate ' , ( ) => {
86128 const base = dayjs ( )
87129 const momentBase = moment ( )
88130 const jsDate = base . toDate ( )
@@ -93,7 +135,18 @@ it('As Javascript Date', () => {
93135 expect ( jsDate . toUTCString ( ) ) . not . toBe ( base . toString ( ) )
94136} )
95137
96- it ( 'As ISO 8601 String e.g. 2013-02-04T22:44:30.652Z' , ( ) => {
138+ it ( 'As Array -> toArray' , ( ) => {
139+ expect ( dayjs ( ) . toArray ( ) ) . toEqual ( moment ( ) . toArray ( ) )
140+ } )
141+
142+ it ( 'As JSON -> toJSON' , ( ) => {
143+ expect ( dayjs ( ) . toJSON ( ) ) . toBe ( moment ( ) . toJSON ( ) )
144+ } )
145+
146+ it ( 'As ISO 8601 String -> toISOString e.g. 2013-02-04T22:44:30.652Z' , ( ) => {
97147 expect ( dayjs ( ) . toISOString ( ) ) . toBe ( moment ( ) . toISOString ( ) )
98148} )
99149
150+ it ( 'As Object -> toObject' , ( ) => {
151+ expect ( dayjs ( ) . toObject ( ) ) . toEqual ( moment ( ) . toObject ( ) )
152+ } )
0 commit comments