import assert from 'assert' ; import set from '.' ; describe ( 'set' , ( ) => { it ( 'sets all values' , ( ) => { const result = set ( new Date ( 2013 , 0 ) , { year : 2014 , month : 8 , date : 20 , hours : 12 , minutes : 12 , seconds : 12 , milliseconds : 12 , } ) ; assert . deepStrictEqual ( result . toString ( ) , new Date ( 2014 , 8 , 20 , 12 , 12 , 12 , 12 ) . toString ( ) ) ; } ) ; it ( 'sets year' , ( ) => { const result = set ( new Date ( 2013 , 8 ) , { year : 2014 } ) ; assert . deepStrictEqual ( result , new Date ( 2014 , 8 ) ) ; } ) ; it ( 'sets month' , ( ) => { const result = set ( new Date ( 2014 , 8 ) , { month : 9 } ) ; assert . deepStrictEqual ( result , new Date ( 2014 , 9 ) ) ; } ) ; it ( 'sets day of month' , ( ) => { const result = set ( new Date ( 2014 , 8 ) , { date : 20 } ) ; assert . deepStrictEqual ( result , new Date ( 2014 , 8 , 20 ) ) ; } ) ; it ( 'sets hours' , ( ) => { const result = set ( new Date ( 2014 , 8 , 1 ) , { hours : 12 } ) ; assert . deepStrictEqual ( result , new Date ( 2014 , 8 , 1 , 12 ) ) ; } ) ; it ( 'sets minutes' , ( ) => { const result = set ( new Date ( 2014 , 8 , 1 , 1 ) , { minutes : 12 } ) ; assert . deepStrictEqual ( result , new Date ( 2014 , 8 , 1 , 1 , 12 ) ) ; } ) ; it ( 'sets seconds' , ( ) => { const result = set ( new Date ( 2014 , 8 , 1 , 1 , 1 ) , { seconds : 12 } ) ; assert . deepStrictEqual ( result , new Date ( 2014 , 8 , 1 , 1 , 1 , 12 ) ) ; } ) ; it ( 'sets milliseconds' , ( ) => { const result = set ( new Date ( 2014 , 8 , 1 , 1 , 1 , 1 ) , { milliseconds : 500 , } ) ; assert . deepStrictEqual ( result , new Date ( 2014 , 8 , 1 , 1 , 1 , 1 , 500 ) ) ; } ) ; describe ( 'value overflow' , ( ) => { it ( 'months overflow into years' , ( ) => { const result = set ( new Date ( 2014 , 8 , 1 ) , { month : 12 , } ) ; assert . deepStrictEqual ( result , new Date ( 2015 , 0 , 1 ) ) ; } ) ; it ( 'days of months overflow into months' , ( ) => { const result = set ( new Date ( 2014 , 8 , 1 ) , { date : 31 } ) ; assert . deepStrictEqual ( result , new Date ( 2014 , 9 , 1 ) ) ; } ) ; it ( 'hours overflow into days' , ( ) => { const result = set ( new Date ( 2014 , 8 , 19 ) , { hours : 24 } ) ; assert . deepStrictEqual ( result , new Date ( 2014 , 8 , 20 ) ) ; } ) ; it ( 'minutes overflow into hours' , ( ) => { const result = set ( new Date ( 2014 , 8 , 20 , 11 ) , { minutes : 60 } ) ; assert . deepStrictEqual ( result , new Date ( 2014 , 8 , 20 , 12 ) ) ; } ) ; it ( 'seconds overflow into minutes' , ( ) => { const result = set ( new Date ( 2014 , 8 , 20 , 12 , 58 ) , { seconds : 60 , } ) ; assert . deepStrictEqual ( result , new Date ( 2014 , 8 , 20 , 12 , 59 ) ) ; } ) ; it ( 'milliseconds overflow into seconds' , ( ) => { const result = set ( new Date ( 2014 , 8 , 20 , 12 , 58 , 30 ) , { milliseconds : 1000 , } ) ; assert . deepStrictEqual ( result , new Date ( 2014 , 8 , 20 , 12 , 58 , 31 ) ) ; } ) ; } ) ; describe ( 'edge cases' , ( ) => { it ( 'sets January' , ( ) => { const result = set ( new Date ( 2014 , 8 ) , { month : 0 } ) ; assert . deepStrictEqual ( result , new Date ( 2014 , 0 ) ) ; } ) ; it ( 'sets the last day of new month if the initial date was the last day of a longer month' , ( ) => { const result = set ( new Date ( 2014 , 7 , 31 ) , { month : 8 , } ) ; assert . deepStrictEqual ( result , new Date ( 2014 , 8 , 30 ) ) ; } ) ; it ( 'ignores undefined values' , ( ) => { const result = set ( new Date ( 2014 , 8 ) , { year : undefined } ) ; assert . deepStrictEqual ( result , new Date ( 2014 , 8 ) ) ; } ) ; it ( 'ignores null values' , ( ) => { const result = set ( new Date ( 2014 , 8 ) , { year : null , } ) ; assert . deepStrictEqual ( result , new Date ( 2014 , 8 ) ) ; } ) ; it ( 'throws TypeError exception if passed less than 2 arguments' , ( ) => { assert . throws ( set . bind ( null ) , TypeError ) ; } ) ; it ( 'returns Invalid Date if any value in values is NaN' , ( ) => { const result = set ( new Date ( 2014 , 8 ) , { year : NaN } ) ; assert ( isNaN ( result . getTime ( ) ) ) ; } ) ; it ( 'returns Invalid Date the initial date was Invalid Date as well' , ( ) => { const result = set ( new Date ( NaN ) , { year : 2019 } ) ; assert ( isNaN ( result . getTime ( ) ) ) ; } ) ; it ( 'throws RangeError exception if `values` is not an object' , ( ) => { assert . throws ( ( ) => set ( new Date ( ) , true ) , RangeError ) ; } ) ; it ( 'throws RangeError exception if `values` is null' , ( ) => { assert . throws ( ( ) => set ( new Date ( ) , null ) , RangeError ) ; } ) ; } ) ; } ) ;