import assert from 'assert' ; import sinon from 'sinon' ; import toDate from '.' ; describe ( 'toDate' , ( ) => { describe ( 'date argument' , ( ) => { it ( 'returns a clone of the given date' , ( ) => { const date = new Date ( 2016 , 0 , 1 ) ; const dateClone = toDate ( date ) ; dateClone . setFullYear ( 2015 ) ; assert . deepStrictEqual ( date , new Date ( 2016 , 0 , 1 ) ) ; } ) ; } ) ; describe ( 'timestamp argument' , ( ) => { it ( 'creates a date from the timestamp' , ( ) => { const timestamp = new Date ( 2016 , 0 , 1 , 23 , 30 , 45 , 123 ) . getTime ( ) ; const result = toDate ( timestamp ) ; assert . deepStrictEqual ( result , new Date ( 2016 , 0 , 1 , 23 , 30 , 45 , 123 ) ) ; } ) ; } ) ; describe ( 'invalid argument' , ( ) => { mockConsoleWarn ( ) ; it ( 'returns Invalid Date if argument is a string' , ( ) => { const result = toDate ( '1987-02-11' ) ; assert ( result instanceof Date ) ; assert ( isNaN ( result . getTime ( ) ) ) ; } ) ; it ( 'prints deprecation warning if the argument is a string' , ( ) => { console . warn = sinon . spy ( ) ; toDate ( '1987-02-11' ) ; assert ( console . warn . calledWith ( "Starting with v2.0.0-beta.1 date-fns doesn't accept strings as date arguments. Please use `parseISO` to parse strings. See: https://git.io/fjule" ) ) ; } ) ; it ( 'returns Invalid Date if argument is NaN' , ( ) => { const result = toDate ( NaN ) ; assert ( result instanceof Date ) ; assert ( isNaN ( result . getTime ( ) ) ) ; } ) ; it ( 'returns Invalid Date if argument is Invalid Date' , ( ) => { const result = toDate ( new Date ( NaN ) ) ; assert ( result instanceof Date ) ; assert ( isNaN ( result . getTime ( ) ) ) ; } ) ; it ( 'returns Invalid Date if argument is null' , ( ) => { const result = toDate ( null ) ; assert ( result instanceof Date ) ; assert ( isNaN ( result . getTime ( ) ) ) ; } ) ; it ( 'returns Invalid Date if argument is undefined' , ( ) => { const result = toDate ( undefined ) ; assert ( result instanceof Date ) ; assert ( isNaN ( result . getTime ( ) ) ) ; } ) ; it ( 'returns Invalid Date if argument is false' , ( ) => { const result = toDate ( false ) ; assert ( result instanceof Date ) ; assert ( isNaN ( result . getTime ( ) ) ) ; } ) ; it ( 'returns Invalid Date if argument is true' , ( ) => { const result = toDate ( true ) ; assert ( result instanceof Date ) ; assert ( isNaN ( result . getTime ( ) ) ) ; } ) ; } ) ; describe ( 'argument conversion' , ( ) => { it ( 'implicitly converts instance of Number into a number' , ( ) => { const timestamp = new Number ( new Date ( 2016 , 0 , 1 , 23 , 30 , 45 , 123 ) . getTime ( ) ) ; const result = toDate ( timestamp ) ; assert . deepStrictEqual ( result , new Date ( 2016 , 0 , 1 , 23 , 30 , 45 , 123 ) ) ; } ) ; } ) ; it ( 'throws TypeError exception if passed less than 1 argument' , ( ) => { assert . throws ( toDate . bind ( null ) , TypeError ) ; } ) ; } ) ; function mockConsoleWarn ( ) { let originalWarn ; beforeEach ( ( ) => { originalWarn = console . warn ; console . warn = ( ) => { } ; } ) ; afterEach ( ( ) => { console . warn = originalWarn ; } ) ; }