import assert from 'assert' ; import differenceInMonths from '.' ; describe ( 'differenceInMonths' , ( ) => { it ( 'returns the number of full months between the given dates' , ( ) => { const result = differenceInMonths ( new Date ( 2012 , 6 , 2 , 18 , 0 ) , new Date ( 2011 , 6 , 2 , 6 , 0 ) ) ; assert ( result === 12 ) ; } ) ; it ( 'returns a negative number if the time value of the first date is smaller' , ( ) => { const result = differenceInMonths ( new Date ( 2011 , 6 , 2 , 6 , 0 ) , new Date ( 2012 , 6 , 2 , 18 , 0 ) ) ; assert ( result === - 12 ) ; } ) ; it ( 'accepts timestamps' , ( ) => { const result = differenceInMonths ( new Date ( 2014 , 7 , 2 ) . getTime ( ) , new Date ( 2010 , 6 , 2 ) . getTime ( ) ) ; assert ( result === 49 ) ; } ) ; describe ( 'edge cases' , ( ) => { it ( 'it returns diff of 1 month between Feb 28 2021 and Jan 30 2021' , ( ) => { const result = differenceInMonths ( new Date ( 2021 , 1 , 28 ) , new Date ( 2021 , 0 , 30 ) ) ; assert ( result === 1 ) ; } ) ; it ( 'it returns diff of 1 month between Feb 28 2021 and Jan 31 2021' , ( ) => { const result = differenceInMonths ( new Date ( 2021 , 1 , 28 ) , new Date ( 2021 , 0 , 31 ) ) ; assert ( result === 1 ) ; } ) ; it ( 'it returns diff of 1 month between Nov, 30 2021 and Oct, 31 2021' , ( ) => { const result = differenceInMonths ( new Date ( 2021 , 10 , 30 ) , new Date ( 2021 , 9 , 31 ) ) ; assert ( result === 1 ) ; } ) ; it ( 'it returns diff of 1 month between Oct, 31 2021 and Sep, 30 2021' , ( ) => { const result = differenceInMonths ( new Date ( 2021 , 9 , 31 ) , new Date ( 2021 , 8 , 30 ) ) ; assert ( result === 1 ) ; } ) ; it ( 'it returns diff of 6 month between Oct, 31 2021 and Apr, 30 2021' , ( ) => { const result = differenceInMonths ( new Date ( 2021 , 9 , 31 ) , new Date ( 2021 , 3 , 30 ) ) ; assert ( result === 6 ) ; } ) ; it ( 'it returns diff of -1 month between Sep, 30 2021 and Oct, 31 2021' , ( ) => { const result = differenceInMonths ( new Date ( 2021 , 8 , 30 ) , new Date ( 2021 , 9 , 31 ) ) ; assert ( result === - 1 ) ; } ) ; it ( 'the difference is less than a month, but the given dates are in different calendar months' , ( ) => { const result = differenceInMonths ( new Date ( 2014 , 7 , 1 ) , new Date ( 2014 , 6 , 31 ) ) ; assert ( result === 0 ) ; } ) ; it ( 'the same for the swapped dates' , ( ) => { const result = differenceInMonths ( new Date ( 2014 , 6 , 31 ) , new Date ( 2014 , 7 , 1 ) ) ; assert ( result === 0 ) ; } ) ; it ( 'the days of months of the given dates are the same' , ( ) => { const result = differenceInMonths ( new Date ( 2014 , 8 , 6 ) , new Date ( 2014 , 7 , 6 ) ) ; assert ( result === 1 ) ; } ) ; it ( 'the given dates are the same' , ( ) => { const result = differenceInMonths ( new Date ( 2014 , 8 , 5 , 0 , 0 ) , new Date ( 2014 , 8 , 5 , 0 , 0 ) ) ; assert ( result === 0 ) ; } ) ; it ( 'does not return -0 when the given dates are the same' , ( ) => { function isNegativeZero ( x ) { return x === 0 && 1 / x < 0 ; } const result = differenceInMonths ( new Date ( 2014 , 8 , 5 , 0 , 0 ) , new Date ( 2014 , 8 , 5 , 0 , 0 ) ) ; const resultIsNegative = isNegativeZero ( result ) ; assert ( resultIsNegative === false ) ; } ) ; } ) ; it ( 'returns NaN if the first date is `Invalid Date`' , ( ) => { const result = differenceInMonths ( new Date ( NaN ) , new Date ( 2017 , 0 , 1 ) ) ; assert ( isNaN ( result ) ) ; } ) ; it ( 'returns NaN if the second date is `Invalid Date`' , ( ) => { const result = differenceInMonths ( new Date ( 2017 , 0 , 1 ) , new Date ( NaN ) ) ; assert ( isNaN ( result ) ) ; } ) ; it ( 'returns NaN if the both dates are `Invalid Date`' , ( ) => { const result = differenceInMonths ( new Date ( NaN ) , new Date ( NaN ) ) ; assert ( isNaN ( result ) ) ; } ) ; it ( 'throws TypeError exception if passed less than 2 arguments' , ( ) => { assert . throws ( differenceInMonths . bind ( null ) , TypeError ) ; assert . throws ( differenceInMonths . bind ( null , 1 ) , TypeError ) ; } ) ; describe ( 'edge cases' , ( ) => { it ( 'returns the number of full months between the given dates - end of Feb' , ( ) => { assert ( differenceInMonths ( new Date ( 2012 , 1 , 29 , 9 , 0 , 0 ) , new Date ( 2012 , 1 , 29 , 10 , 0 , 0 ) ) === 0 ) ; assert ( differenceInMonths ( new Date ( 2012 , 1 , 28 , 9 , 0 , 0 ) , new Date ( 2012 , 1 , 29 , 10 , 0 , 0 ) ) === 0 ) ; assert ( differenceInMonths ( new Date ( 2012 , 1 , 27 , 9 , 0 , 0 ) , new Date ( 2012 , 1 , 27 , 10 , 0 , 0 ) ) === 0 ) ; assert ( differenceInMonths ( new Date ( 2012 , 1 , 28 , 9 , 0 , 0 ) , new Date ( 2012 , 1 , 28 , 10 , 0 , 0 ) ) === 0 ) ; } ) ; assert ( differenceInMonths ( new Date ( 2021 , 1 , 28 , 7 , 23 , 7 ) , new Date ( 2021 , 1 , 28 , 7 , 38 , 18 ) ) === 0 ) ; } ) ; } ) ;