2018-05-28 10:55:19 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import moment from 'moment';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { toMoment } from 'lib/date';
|
|
|
|
|
|
|
|
describe( 'toMoment', () => {
|
|
|
|
it( 'should pass through a valid Moment object as an argument', () => {
|
|
|
|
const now = moment();
|
2018-06-27 03:59:58 +00:00
|
|
|
const myMoment = toMoment( 'YYYY', now );
|
2018-05-28 10:55:19 +00:00
|
|
|
expect( myMoment ).toEqual( now );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should handle isoFormat dates', () => {
|
2018-06-27 03:59:58 +00:00
|
|
|
const myMoment = toMoment( 'YYYY', '2018-04-15' );
|
2018-05-28 10:55:19 +00:00
|
|
|
expect( moment.isMoment( myMoment ) ).toBe( true );
|
|
|
|
expect( myMoment.isValid() ).toBe( true );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should handle local formats', () => {
|
2018-06-27 03:59:58 +00:00
|
|
|
const longDate = toMoment( 'MMMM D, YYYY', 'April 15, 2018' );
|
|
|
|
expect( moment.isMoment( longDate ) ).toBe( true );
|
|
|
|
expect( longDate.isValid() ).toBe( true );
|
|
|
|
expect( longDate.date() ).toBe( 15 );
|
|
|
|
expect( longDate.month() ).toBe( 3 );
|
|
|
|
expect( longDate.year() ).toBe( 2018 );
|
|
|
|
|
|
|
|
const shortDate = toMoment( 'DD/MM/YYYY', '15/04/2018' );
|
|
|
|
expect( moment.isMoment( shortDate ) ).toBe( true );
|
|
|
|
expect( shortDate.isValid() ).toBe( true );
|
|
|
|
expect( shortDate.date() ).toBe( 15 );
|
|
|
|
expect( shortDate.month() ).toBe( 3 );
|
|
|
|
expect( shortDate.year() ).toBe( 2018 );
|
2018-05-28 10:55:19 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should throw on an invalid argument', () => {
|
2018-06-27 03:59:58 +00:00
|
|
|
const fn = () => toMoment( '', 77 );
|
2018-05-28 10:55:19 +00:00
|
|
|
expect( fn ).toThrow();
|
|
|
|
} );
|
|
|
|
} );
|