2018-05-28 10:55:19 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import moment from 'moment';
|
2020-02-14 02:23:21 +00:00
|
|
|
import { getSetting } from '@woocommerce/wc-admin-settings';
|
2019-09-23 21:47:08 +00:00
|
|
|
|
2018-05-28 10:55:19 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2018-07-13 02:05:03 +00:00
|
|
|
import {
|
2018-10-17 16:01:58 +00:00
|
|
|
appendTimestamp,
|
2018-07-13 02:05:03 +00:00
|
|
|
toMoment,
|
|
|
|
getLastPeriod,
|
|
|
|
getCurrentPeriod,
|
|
|
|
getRangeLabel,
|
|
|
|
loadLocaleData,
|
|
|
|
getCurrentDates,
|
2018-07-22 23:43:21 +00:00
|
|
|
validateDateInputForRange,
|
|
|
|
dateValidationMessages,
|
2018-09-03 15:25:38 +00:00
|
|
|
isoDateFormat,
|
|
|
|
getDateDifferenceInDays,
|
|
|
|
getPreviousDate,
|
2018-11-14 01:45:05 +00:00
|
|
|
getChartTypeForQuery,
|
2019-01-11 15:51:24 +00:00
|
|
|
getAllowedIntervalsForQuery,
|
2021-04-16 20:13:39 +00:00
|
|
|
getStoreTimeZoneMoment,
|
2018-10-30 18:57:48 +00:00
|
|
|
} from '../src';
|
2018-05-28 10:55:19 +00:00
|
|
|
|
2021-04-16 20:13:39 +00:00
|
|
|
jest.mock( 'moment', () => {
|
|
|
|
const m = jest.requireActual( 'moment' );
|
|
|
|
m.prototype.tz = jest.fn().mockImplementation( () => m() );
|
|
|
|
|
|
|
|
return m;
|
|
|
|
} );
|
|
|
|
|
2018-10-17 16:01:58 +00:00
|
|
|
describe( 'appendTimestamp', () => {
|
|
|
|
it( 'should append `start` timestamp', () => {
|
2020-02-14 02:23:21 +00:00
|
|
|
expect( appendTimestamp( moment( '2018-01-01' ), 'start' ) ).toEqual(
|
|
|
|
'2018-01-01T00:00:00'
|
|
|
|
);
|
2019-01-07 10:41:46 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should append `now` timestamp', () => {
|
|
|
|
const nowTimestamp = moment().format( 'HH:mm:00' );
|
2021-04-16 20:13:39 +00:00
|
|
|
expect(
|
|
|
|
appendTimestamp( moment( '2018-01-01 ' + nowTimestamp ), 'now' )
|
|
|
|
).toEqual( '2018-01-01T' + nowTimestamp );
|
2018-10-17 16:01:58 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should append `end` timestamp', () => {
|
2020-02-14 02:23:21 +00:00
|
|
|
expect( appendTimestamp( moment( '2018-01-01' ), 'end' ) ).toEqual(
|
|
|
|
'2018-01-01T23:59:59'
|
|
|
|
);
|
2018-10-17 16:01:58 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should throw and error if `timeOfDay` is not valid', () => {
|
2020-02-14 02:23:21 +00:00
|
|
|
expect( () => appendTimestamp( moment( '2018-01-01' ) ) ).toThrow(
|
|
|
|
Error
|
|
|
|
);
|
2018-10-17 16:01:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2018-05-28 10:55:19 +00:00
|
|
|
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();
|
|
|
|
} );
|
2018-07-22 23:43:21 +00:00
|
|
|
|
2019-07-18 10:11:21 +00:00
|
|
|
it( 'should return null on invalid date', () => {
|
2018-07-22 23:43:21 +00:00
|
|
|
const invalidDate = toMoment( 'YYYY', '2018-00-00' );
|
|
|
|
expect( invalidDate ).toBe( null );
|
|
|
|
} );
|
2018-05-28 10:55:19 +00:00
|
|
|
} );
|
2018-06-27 04:58:43 +00:00
|
|
|
|
2019-01-11 15:51:24 +00:00
|
|
|
describe( 'getAllowedIntervalsForQuery', () => {
|
|
|
|
it( 'should return days when query period is defined but empty', () => {
|
|
|
|
const allowedIntervals = getAllowedIntervalsForQuery( { period: '' } );
|
|
|
|
expect( allowedIntervals ).toEqual( [ 'day' ] );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return days and hours for today and yesterday periods', () => {
|
2020-02-14 02:23:21 +00:00
|
|
|
const allowedIntervalsToday = getAllowedIntervalsForQuery( {
|
|
|
|
period: 'today',
|
|
|
|
} );
|
2019-01-11 15:51:24 +00:00
|
|
|
expect( allowedIntervalsToday ).toEqual( [ 'hour', 'day' ] );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const allowedIntervalsYesterday = getAllowedIntervalsForQuery( {
|
|
|
|
period: 'yesterday',
|
|
|
|
} );
|
2019-01-11 15:51:24 +00:00
|
|
|
expect( allowedIntervalsYesterday ).toEqual( [ 'hour', 'day' ] );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return day for week and last_week periods', () => {
|
2020-02-14 02:23:21 +00:00
|
|
|
const allowedIntervalsWeek = getAllowedIntervalsForQuery( {
|
|
|
|
period: 'week',
|
|
|
|
} );
|
2019-01-11 15:51:24 +00:00
|
|
|
expect( allowedIntervalsWeek ).toEqual( [ 'day' ] );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const allowedIntervalsLastWeek = getAllowedIntervalsForQuery( {
|
|
|
|
period: 'last_week',
|
|
|
|
} );
|
2019-01-11 15:51:24 +00:00
|
|
|
expect( allowedIntervalsLastWeek ).toEqual( [ 'day' ] );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return day, week for month and last_month periods', () => {
|
2020-02-14 02:23:21 +00:00
|
|
|
const allowedIntervalsMonth = getAllowedIntervalsForQuery( {
|
|
|
|
period: 'month',
|
|
|
|
} );
|
2019-01-11 15:51:24 +00:00
|
|
|
expect( allowedIntervalsMonth ).toEqual( [ 'day', 'week' ] );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const allowedIntervalsLastMonth = getAllowedIntervalsForQuery( {
|
|
|
|
period: 'last_month',
|
|
|
|
} );
|
2019-01-11 15:51:24 +00:00
|
|
|
expect( allowedIntervalsLastMonth ).toEqual( [ 'day', 'week' ] );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return day, week, month for quarter and last_quarter periods', () => {
|
2020-02-14 02:23:21 +00:00
|
|
|
const allowedIntervalsQuarter = getAllowedIntervalsForQuery( {
|
|
|
|
period: 'quarter',
|
|
|
|
} );
|
2019-01-11 15:51:24 +00:00
|
|
|
expect( allowedIntervalsQuarter ).toEqual( [ 'day', 'week', 'month' ] );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const allowedIntervalsLastQuarter = getAllowedIntervalsForQuery( {
|
|
|
|
period: 'last_quarter',
|
|
|
|
} );
|
|
|
|
expect( allowedIntervalsLastQuarter ).toEqual( [
|
|
|
|
'day',
|
|
|
|
'week',
|
|
|
|
'month',
|
|
|
|
] );
|
2019-01-11 15:51:24 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return day, week, month, quarter for year and last_year periods', () => {
|
2020-02-14 02:23:21 +00:00
|
|
|
const allowedIntervalsYear = getAllowedIntervalsForQuery( {
|
|
|
|
period: 'year',
|
|
|
|
} );
|
|
|
|
expect( allowedIntervalsYear ).toEqual( [
|
|
|
|
'day',
|
|
|
|
'week',
|
|
|
|
'month',
|
|
|
|
'quarter',
|
|
|
|
] );
|
|
|
|
|
|
|
|
const allowedIntervalsLastYear = getAllowedIntervalsForQuery( {
|
|
|
|
period: 'last_year',
|
|
|
|
} );
|
|
|
|
expect( allowedIntervalsLastYear ).toEqual( [
|
|
|
|
'day',
|
|
|
|
'week',
|
|
|
|
'month',
|
|
|
|
'quarter',
|
|
|
|
] );
|
2019-01-11 15:51:24 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2018-06-27 04:58:43 +00:00
|
|
|
describe( 'getCurrentPeriod', () => {
|
|
|
|
it( 'should return a DateValue object with correct properties', () => {
|
|
|
|
const dateValue = getCurrentPeriod( 'day', 'previous_period' );
|
|
|
|
|
|
|
|
expect( dateValue.primaryStart ).toBeDefined();
|
|
|
|
expect( dateValue.primaryEnd ).toBeDefined();
|
|
|
|
expect( dateValue.secondaryStart ).toBeDefined();
|
|
|
|
expect( dateValue.secondaryEnd ).toBeDefined();
|
|
|
|
} );
|
|
|
|
|
|
|
|
// day
|
|
|
|
const today = moment();
|
|
|
|
const yesterday = moment().subtract( 1, 'days' );
|
|
|
|
const todayLastYear = moment().subtract( 1, 'years' );
|
|
|
|
|
|
|
|
// week
|
|
|
|
const thisWeekStart = moment().startOf( 'week' );
|
|
|
|
const lastWeekStart = thisWeekStart.clone().subtract( 1, 'week' );
|
|
|
|
const todayLastWeek = today.clone().subtract( 1, 'week' );
|
|
|
|
|
|
|
|
// month
|
|
|
|
const thisMonthStart = moment().startOf( 'month' );
|
|
|
|
const lastMonthStart = thisMonthStart.clone().subtract( 1, 'month' );
|
|
|
|
const todayLastMonth = today.clone().subtract( 1, 'month' );
|
|
|
|
|
|
|
|
// quarter
|
|
|
|
const thisQuarterStart = moment().startOf( 'quarter' );
|
|
|
|
const lastQuarterStart = thisQuarterStart.clone().subtract( 1, 'quarter' );
|
|
|
|
const todayLastQuarter = today.clone().subtract( 1, 'quarter' );
|
|
|
|
|
|
|
|
// year
|
|
|
|
const thisYearStart = moment().startOf( 'year' );
|
|
|
|
const lastYearStart = thisYearStart.clone().subtract( 1, 'year' );
|
|
|
|
|
|
|
|
describe( 'day', () => {
|
|
|
|
it( 'should return correct values for primary period', () => {
|
|
|
|
const dateValue = getCurrentPeriod( 'day', 'previous_period' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect( today.isSame( dateValue.primaryStart, 'day' ) ).toBe(
|
|
|
|
true
|
|
|
|
);
|
2018-06-27 04:58:43 +00:00
|
|
|
expect( today.isSame( dateValue.primaryEnd, 'day' ) ).toBe( true );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct values for previous_period', () => {
|
|
|
|
const dateValue = getCurrentPeriod( 'day', 'previous_period' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect( yesterday.isSame( dateValue.secondaryStart, 'day' ) ).toBe(
|
|
|
|
true
|
|
|
|
);
|
|
|
|
expect( yesterday.isSame( dateValue.secondaryEnd, 'day' ) ).toBe(
|
|
|
|
true
|
|
|
|
);
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct values for previous_year', () => {
|
|
|
|
const dateValue = getCurrentPeriod( 'day', 'previous_year' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect(
|
|
|
|
todayLastYear.isSame( dateValue.secondaryStart, 'day' )
|
|
|
|
).toBe( true );
|
|
|
|
expect(
|
|
|
|
todayLastYear.isSame( dateValue.secondaryEnd, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'week', () => {
|
|
|
|
it( 'should return correct values for primary period', () => {
|
|
|
|
const dateValue = getCurrentPeriod( 'week', 'previous_period' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect(
|
|
|
|
thisWeekStart.isSame( dateValue.primaryStart, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
expect( today.isSame( dateValue.primaryEnd, 'day' ) ).toBe( true );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct values for previous_period', () => {
|
|
|
|
const dateValue = getCurrentPeriod( 'week', 'previous_period' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect(
|
|
|
|
lastWeekStart.isSame( dateValue.secondaryStart, 'day' )
|
|
|
|
).toBe( true );
|
|
|
|
expect(
|
|
|
|
todayLastWeek.isSame( dateValue.secondaryEnd, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct values for previous_year', () => {
|
|
|
|
const dateValue = getCurrentPeriod( 'week', 'previous_year' );
|
|
|
|
const daysSoFar = today.diff( thisWeekStart, 'days' );
|
2019-11-25 13:19:19 +00:00
|
|
|
// Last year weeks are aligned by calendar date not day of week.
|
2020-02-14 02:23:21 +00:00
|
|
|
const thisWeekLastYearStart = thisWeekStart
|
|
|
|
.clone()
|
|
|
|
.subtract( 1, 'years' );
|
|
|
|
const todayThisWeekLastYear = thisWeekLastYearStart
|
|
|
|
.clone()
|
|
|
|
.add( daysSoFar, 'days' );
|
|
|
|
|
|
|
|
expect(
|
|
|
|
thisWeekLastYearStart.isSame( dateValue.secondaryStart, 'day' )
|
|
|
|
).toBe( true );
|
|
|
|
expect(
|
|
|
|
todayThisWeekLastYear.isSame( dateValue.secondaryEnd, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'month', () => {
|
|
|
|
it( 'should return correct values for primary period', () => {
|
|
|
|
const dateValue = getCurrentPeriod( 'month', 'previous_period' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect(
|
|
|
|
thisMonthStart.isSame( dateValue.primaryStart, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
expect( today.isSame( dateValue.primaryEnd, 'day' ) ).toBe( true );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct values for previous_period', () => {
|
|
|
|
const dateValue = getCurrentPeriod( 'month', 'previous_period' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect(
|
|
|
|
lastMonthStart.isSame( dateValue.secondaryStart, 'day' )
|
|
|
|
).toBe( true );
|
|
|
|
expect(
|
|
|
|
todayLastMonth.isSame( dateValue.secondaryEnd, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct values for previous_year', () => {
|
|
|
|
const dateValue = getCurrentPeriod( 'month', 'previous_year' );
|
|
|
|
const daysSoFar = today.diff( thisMonthStart, 'days' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const thisMonthLastYearStart = thisMonthStart
|
|
|
|
.clone()
|
|
|
|
.subtract( 1, 'years' );
|
|
|
|
const thisMonthLastYearEnd = thisMonthLastYearStart
|
|
|
|
.clone()
|
|
|
|
.add( daysSoFar, 'days' );
|
|
|
|
|
|
|
|
expect(
|
|
|
|
thisMonthLastYearStart.isSame( dateValue.secondaryStart, 'day' )
|
|
|
|
).toBe( true );
|
|
|
|
expect(
|
|
|
|
thisMonthLastYearEnd.isSame( dateValue.secondaryEnd, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'quarter', () => {
|
|
|
|
it( 'should return correct values for primary period', () => {
|
|
|
|
const dateValue = getCurrentPeriod( 'quarter', 'previous_period' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect(
|
|
|
|
thisQuarterStart.isSame( dateValue.primaryStart, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
expect( today.isSame( dateValue.primaryEnd, 'day' ) ).toBe( true );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct values for previous_period', () => {
|
|
|
|
const dateValue = getCurrentPeriod( 'quarter', 'previous_period' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect(
|
|
|
|
lastQuarterStart.isSame( dateValue.secondaryStart, 'day' )
|
|
|
|
).toBe( true );
|
|
|
|
expect(
|
|
|
|
todayLastQuarter.isSame( dateValue.secondaryEnd, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct values for previous_year', () => {
|
|
|
|
const dateValue = getCurrentPeriod( 'quarter', 'previous_year' );
|
|
|
|
const daysSoFar = today.diff( thisQuarterStart, 'days' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const thisQuarterLastYearStart = thisQuarterStart
|
|
|
|
.clone()
|
|
|
|
.subtract( 1, 'years' );
|
|
|
|
const thisQuarterLastYearEnd = thisQuarterLastYearStart
|
|
|
|
.clone()
|
|
|
|
.add( daysSoFar, 'days' );
|
|
|
|
|
|
|
|
expect(
|
|
|
|
thisQuarterLastYearStart.isSame(
|
|
|
|
dateValue.secondaryStart,
|
|
|
|
'day'
|
|
|
|
)
|
|
|
|
).toBe( true );
|
|
|
|
expect(
|
|
|
|
thisQuarterLastYearEnd.isSame( dateValue.secondaryEnd, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'year', () => {
|
|
|
|
it( 'should return correct values for primary period', () => {
|
|
|
|
const dateValue = getCurrentPeriod( 'year', 'previous_period' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect(
|
|
|
|
thisYearStart.isSame( dateValue.primaryStart, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
expect( today.isSame( dateValue.primaryEnd, 'day' ) ).toBe( true );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct values for previous_period', () => {
|
|
|
|
const dateValue = getCurrentPeriod( 'year', 'previous_period' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect(
|
|
|
|
lastYearStart.isSame( dateValue.secondaryStart, 'day' )
|
|
|
|
).toBe( true );
|
|
|
|
expect(
|
|
|
|
todayLastYear.isSame( dateValue.secondaryEnd, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct values for previous_year', () => {
|
|
|
|
const dateValue = getCurrentPeriod( 'year', 'previous_year' );
|
|
|
|
const daysSoFar = today.diff( thisYearStart, 'days' );
|
|
|
|
|
|
|
|
const lastYearEnd = lastYearStart.clone().add( daysSoFar, 'days' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect(
|
|
|
|
lastYearStart.isSame( dateValue.secondaryStart, 'day' )
|
|
|
|
).toBe( true );
|
|
|
|
expect( lastYearEnd.isSame( dateValue.secondaryEnd, 'day' ) ).toBe(
|
|
|
|
true
|
|
|
|
);
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'getLastPeriod', () => {
|
|
|
|
it( 'should return a DateValue object with correct properties', () => {
|
|
|
|
const dateValue = getLastPeriod( 'day', 'previous_period' );
|
|
|
|
|
|
|
|
expect( dateValue.primaryStart ).toBeDefined();
|
|
|
|
expect( dateValue.primaryEnd ).toBeDefined();
|
|
|
|
expect( dateValue.secondaryStart ).toBeDefined();
|
|
|
|
expect( dateValue.secondaryEnd ).toBeDefined();
|
|
|
|
} );
|
|
|
|
|
|
|
|
// day
|
|
|
|
const yesterday = moment().subtract( 1, 'days' );
|
|
|
|
const twoDaysAgo = moment().subtract( 2, 'days' );
|
|
|
|
const yesterdayLastYear = moment()
|
|
|
|
.subtract( 1, 'days' )
|
|
|
|
.subtract( 1, 'years' );
|
|
|
|
|
|
|
|
// week
|
2020-07-28 02:32:58 +00:00
|
|
|
const lastWeekStart = moment().startOf( 'week' ).subtract( 1, 'week' );
|
2018-06-27 04:58:43 +00:00
|
|
|
const lastWeekEnd = lastWeekStart.clone().endOf( 'week' );
|
|
|
|
|
|
|
|
// month
|
2020-07-28 02:32:58 +00:00
|
|
|
const lastMonthStart = moment().startOf( 'month' ).subtract( 1, 'month' );
|
2018-06-27 04:58:43 +00:00
|
|
|
const lastMonthEnd = lastMonthStart.clone().endOf( 'month' );
|
|
|
|
|
|
|
|
// quarter
|
|
|
|
const lastQuarterStart = moment()
|
|
|
|
.startOf( 'quarter' )
|
|
|
|
.subtract( 1, 'quarter' );
|
|
|
|
const lastQuarterEnd = lastQuarterStart.clone().endOf( 'quarter' );
|
|
|
|
|
|
|
|
// year
|
2020-07-28 02:32:58 +00:00
|
|
|
const lastYearStart = moment().startOf( 'year' ).subtract( 1, 'year' );
|
2018-06-27 04:58:43 +00:00
|
|
|
const lastYearEnd = lastYearStart.clone().endOf( 'year' );
|
2020-07-28 02:32:58 +00:00
|
|
|
const twoYearsAgoStart = moment().startOf( 'year' ).subtract( 2, 'year' );
|
2018-06-27 04:58:43 +00:00
|
|
|
|
|
|
|
describe( 'day', () => {
|
|
|
|
it( 'should return correct values for primary period', () => {
|
|
|
|
const dateValue = getLastPeriod( 'day', 'previous_period' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect( yesterday.isSame( dateValue.primaryStart, 'day' ) ).toBe(
|
|
|
|
true
|
|
|
|
);
|
|
|
|
expect( yesterday.isSame( dateValue.primaryEnd, 'day' ) ).toBe(
|
|
|
|
true
|
|
|
|
);
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct values for previous_period', () => {
|
|
|
|
const dateValue = getLastPeriod( 'day', 'previous_period' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect( twoDaysAgo.isSame( dateValue.secondaryStart, 'day' ) ).toBe(
|
|
|
|
true
|
|
|
|
);
|
|
|
|
expect( twoDaysAgo.isSame( dateValue.secondaryEnd, 'day' ) ).toBe(
|
|
|
|
true
|
|
|
|
);
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct values for previous_year', () => {
|
|
|
|
const dateValue = getLastPeriod( 'day', 'previous_year' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect(
|
|
|
|
yesterdayLastYear.isSame( dateValue.secondaryStart, 'day' )
|
|
|
|
).toBe( true );
|
|
|
|
expect(
|
|
|
|
yesterdayLastYear.isSame( dateValue.secondaryEnd, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'week', () => {
|
|
|
|
it( 'should return correct values for primary period', () => {
|
|
|
|
const dateValue = getLastPeriod( 'week', 'previous_period' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect(
|
|
|
|
lastWeekStart.isSame( dateValue.primaryStart, 'day' )
|
|
|
|
).toBe( true );
|
|
|
|
expect( lastWeekEnd.isSame( dateValue.primaryEnd, 'day' ) ).toBe(
|
|
|
|
true
|
|
|
|
);
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct values for previous_period', () => {
|
|
|
|
const dateValue = getLastPeriod( 'week', 'previous_period' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const twoWeeksAgoStart = lastWeekStart
|
|
|
|
.clone()
|
|
|
|
.subtract( 1, 'week' );
|
2018-06-27 04:58:43 +00:00
|
|
|
const twoWeeksAgoEnd = twoWeeksAgoStart.clone().endOf( 'week' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect(
|
|
|
|
twoWeeksAgoStart.isSame( dateValue.secondaryStart, 'day' )
|
|
|
|
).toBe( true );
|
|
|
|
expect(
|
|
|
|
twoWeeksAgoEnd.isSame( dateValue.secondaryEnd, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct values for previous_year', () => {
|
|
|
|
const dateValue = getLastPeriod( 'week', 'previous_year' );
|
|
|
|
|
2019-11-25 13:19:19 +00:00
|
|
|
// Last year weeks are aligned by calendar date not day of week.
|
2020-02-14 02:23:21 +00:00
|
|
|
const lastWeekLastYearStart = lastWeekStart
|
|
|
|
.clone()
|
|
|
|
.subtract( 1, 'year' );
|
|
|
|
const lastWeekLastYearEnd = lastWeekEnd
|
|
|
|
.clone()
|
|
|
|
.subtract( 1, 'year' );
|
|
|
|
|
|
|
|
expect(
|
|
|
|
lastWeekLastYearStart.isSame( dateValue.secondaryStart, 'day' )
|
|
|
|
).toBe( true );
|
|
|
|
expect(
|
|
|
|
lastWeekLastYearEnd.isSame( dateValue.secondaryEnd, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'month', () => {
|
|
|
|
it( 'should return correct values for primary period', () => {
|
|
|
|
const dateValue = getLastPeriod( 'month', 'previous_period' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect(
|
|
|
|
lastMonthStart.isSame( dateValue.primaryStart, 'day' )
|
|
|
|
).toBe( true );
|
|
|
|
expect( lastMonthEnd.isSame( dateValue.primaryEnd, 'day' ) ).toBe(
|
|
|
|
true
|
|
|
|
);
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct values for previous_period', () => {
|
|
|
|
const dateValue = getLastPeriod( 'month', 'previous_period' );
|
|
|
|
const daysDiff = lastMonthEnd.diff( lastMonthStart, 'days' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const twoMonthsAgoEnd = lastMonthStart
|
|
|
|
.clone()
|
|
|
|
.subtract( 1, 'days' );
|
|
|
|
const twoMonthsAgoStart = twoMonthsAgoEnd
|
|
|
|
.clone()
|
|
|
|
.subtract( daysDiff, 'days' );
|
|
|
|
|
|
|
|
expect(
|
|
|
|
twoMonthsAgoStart.isSame( dateValue.secondaryStart, 'day' )
|
|
|
|
).toBe( true );
|
|
|
|
expect(
|
|
|
|
twoMonthsAgoEnd.isSame( dateValue.secondaryEnd, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
|
2021-03-01 20:40:26 +00:00
|
|
|
it( 'should return correct values for previous_year', () => {
|
2018-06-27 04:58:43 +00:00
|
|
|
const dateValue = getLastPeriod( 'month', 'previous_year' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const lastMonthkLastYearStart = lastMonthStart
|
|
|
|
.clone()
|
|
|
|
.subtract( 1, 'year' );
|
|
|
|
const lastMonthkLastYearEnd = lastMonthkLastYearStart
|
|
|
|
.clone()
|
|
|
|
.endOf( 'month' );
|
|
|
|
expect(
|
|
|
|
lastMonthkLastYearStart.isSame(
|
|
|
|
dateValue.secondaryStart,
|
|
|
|
'day'
|
|
|
|
)
|
|
|
|
).toBe( true );
|
|
|
|
expect(
|
|
|
|
lastMonthkLastYearEnd.isSame( dateValue.secondaryEnd, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'quarter', () => {
|
|
|
|
it( 'should return correct values for primary period', () => {
|
|
|
|
const dateValue = getLastPeriod( 'quarter', 'previous_period' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect(
|
|
|
|
lastQuarterStart.isSame( dateValue.primaryStart, 'day' )
|
|
|
|
).toBe( true );
|
|
|
|
expect( lastQuarterEnd.isSame( dateValue.primaryEnd, 'day' ) ).toBe(
|
|
|
|
true
|
|
|
|
);
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct values for previous_period', () => {
|
|
|
|
const dateValue = getLastPeriod( 'quarter', 'previous_period' );
|
|
|
|
const daysDiff = lastQuarterEnd.diff( lastQuarterStart, 'days' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const twoQuartersAgoEnd = lastQuarterStart
|
|
|
|
.clone()
|
|
|
|
.subtract( 1, 'days' );
|
|
|
|
const twoQuartersAgoStart = twoQuartersAgoEnd
|
|
|
|
.clone()
|
|
|
|
.subtract( daysDiff, 'days' );
|
|
|
|
|
|
|
|
expect(
|
|
|
|
twoQuartersAgoStart.isSame( dateValue.secondaryStart, 'day' )
|
|
|
|
).toBe( true );
|
|
|
|
expect(
|
|
|
|
twoQuartersAgoEnd.isSame( dateValue.secondaryEnd, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct values for previous_year', () => {
|
|
|
|
const dateValue = getLastPeriod( 'quarter', 'previous_year' );
|
2020-02-14 02:23:21 +00:00
|
|
|
const lastQuarterLastYearStart = lastQuarterStart
|
|
|
|
.clone()
|
|
|
|
.subtract( 1, 'year' );
|
|
|
|
const lastQuarterLastYearEnd = lastQuarterLastYearStart
|
|
|
|
.clone()
|
|
|
|
.endOf( 'quarter' );
|
|
|
|
|
|
|
|
expect(
|
|
|
|
lastQuarterLastYearStart.isSame(
|
|
|
|
dateValue.secondaryStart,
|
|
|
|
'day'
|
|
|
|
)
|
|
|
|
).toBe( true );
|
|
|
|
expect(
|
|
|
|
lastQuarterLastYearEnd.isSame( dateValue.secondaryEnd, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'year', () => {
|
|
|
|
it( 'should return correct values for primary period', () => {
|
|
|
|
const dateValue = getLastPeriod( 'year', 'previous_period' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect(
|
|
|
|
lastYearStart.isSame( dateValue.primaryStart, 'day' )
|
|
|
|
).toBe( true );
|
|
|
|
expect( lastYearEnd.isSame( dateValue.primaryEnd, 'day' ) ).toBe(
|
|
|
|
true
|
|
|
|
);
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct values for previous_period', () => {
|
|
|
|
const dateValue = getLastPeriod( 'year', 'previous_period' );
|
|
|
|
const twoYearsAgoEnd = twoYearsAgoStart.clone().endOf( 'year' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect(
|
|
|
|
twoYearsAgoStart.isSame( dateValue.secondaryStart, 'day' )
|
|
|
|
).toBe( true );
|
|
|
|
expect(
|
|
|
|
twoYearsAgoEnd.isSame( dateValue.secondaryEnd, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
it( 'should return correct values for previous_year', () => {
|
2018-06-27 04:58:43 +00:00
|
|
|
const dateValue = getLastPeriod( 'year', 'previous_year' );
|
|
|
|
const twoYearsAgoEnd = twoYearsAgoStart.clone().endOf( 'year' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect(
|
|
|
|
twoYearsAgoStart.isSame( dateValue.secondaryStart, 'day' )
|
|
|
|
).toBe( true );
|
|
|
|
expect(
|
|
|
|
twoYearsAgoEnd.isSame( dateValue.secondaryEnd, 'day' )
|
|
|
|
).toBe( true );
|
2018-06-27 04:58:43 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'getRangeLabel', () => {
|
|
|
|
it( 'should return correct string for dates on the same day', () => {
|
2020-02-14 02:23:21 +00:00
|
|
|
const label = getRangeLabel(
|
|
|
|
moment( '2018-04-15' ),
|
|
|
|
moment( '2018-04-15' )
|
|
|
|
);
|
2018-06-27 04:58:43 +00:00
|
|
|
expect( label ).toBe( 'Apr 15, 2018' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct string for dates in the same month', () => {
|
2020-02-14 02:23:21 +00:00
|
|
|
const label = getRangeLabel(
|
|
|
|
moment( '2018-04-01' ),
|
|
|
|
moment( '2018-04-15' )
|
|
|
|
);
|
2018-06-27 04:58:43 +00:00
|
|
|
expect( label ).toBe( 'Apr 1 - 15, 2018' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct string for dates in the same year, but different months', () => {
|
2020-02-14 02:23:21 +00:00
|
|
|
const label = getRangeLabel(
|
|
|
|
moment( '2018-04-01' ),
|
|
|
|
moment( '2018-05-15' )
|
|
|
|
);
|
2018-06-27 04:58:43 +00:00
|
|
|
expect( label ).toBe( 'Apr 1 - May 15, 2018' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return correct string for dates in different years', () => {
|
2020-02-14 02:23:21 +00:00
|
|
|
const label = getRangeLabel(
|
|
|
|
moment( '2017-04-01' ),
|
|
|
|
moment( '2018-05-15' )
|
|
|
|
);
|
2018-06-27 04:58:43 +00:00
|
|
|
expect( label ).toBe( 'Apr 1, 2017 - May 15, 2018' );
|
|
|
|
} );
|
|
|
|
} );
|
2018-07-04 01:50:12 +00:00
|
|
|
|
|
|
|
describe( 'loadLocaleData', () => {
|
2019-09-23 21:47:08 +00:00
|
|
|
const originalLocale = getSetting( 'locale' );
|
2018-07-04 01:50:12 +00:00
|
|
|
beforeEach( () => {
|
|
|
|
// Reset to default settings
|
2019-11-26 19:39:40 +00:00
|
|
|
loadLocaleData( originalLocale );
|
2018-07-04 01:50:12 +00:00
|
|
|
} );
|
|
|
|
|
2019-01-01 23:25:52 +00:00
|
|
|
it( 'should load locale data on user locale', () => {
|
|
|
|
// initialize locale. Gutenberg normaly does this, but not in test environment.
|
|
|
|
moment.locale( 'fr_FR', {} );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const weekdaysShort = [
|
|
|
|
'dim',
|
|
|
|
'lun',
|
|
|
|
'mar',
|
|
|
|
'mer',
|
|
|
|
'jeu',
|
|
|
|
'ven',
|
|
|
|
'sam',
|
|
|
|
];
|
2019-11-26 19:39:40 +00:00
|
|
|
|
|
|
|
loadLocaleData( {
|
|
|
|
userLocale: 'fr_FR',
|
|
|
|
weekdaysShort,
|
|
|
|
} );
|
|
|
|
expect( moment.localeData().weekdaysMin() ).toEqual( weekdaysShort );
|
2018-07-04 01:50:12 +00:00
|
|
|
} );
|
|
|
|
} );
|
2018-07-13 02:05:03 +00:00
|
|
|
|
|
|
|
describe( 'getCurrentDates', () => {
|
|
|
|
it( 'should return a correctly shaped object', () => {
|
|
|
|
const query = {};
|
|
|
|
const currentDates = getCurrentDates( query );
|
|
|
|
|
|
|
|
expect( currentDates.primary ).toBeDefined();
|
2019-01-07 10:41:46 +00:00
|
|
|
expect( typeof currentDates.primary.label ).toBe( 'string' );
|
|
|
|
expect( typeof currentDates.primary.range ).toBe( 'string' );
|
|
|
|
expect( moment.isMoment( currentDates.primary.after ) ).toBe( true );
|
|
|
|
expect( moment.isMoment( currentDates.primary.before ) ).toBe( true );
|
2018-07-13 02:05:03 +00:00
|
|
|
|
|
|
|
expect( currentDates.secondary ).toBeDefined();
|
2019-01-07 10:41:46 +00:00
|
|
|
expect( typeof currentDates.secondary.label ).toBe( 'string' );
|
|
|
|
expect( typeof currentDates.secondary.range ).toBe( 'string' );
|
|
|
|
expect( moment.isMoment( currentDates.secondary.after ) ).toBe( true );
|
|
|
|
expect( moment.isMoment( currentDates.secondary.before ) ).toBe( true );
|
2018-07-13 02:05:03 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should correctly apply default values', () => {
|
|
|
|
const query = {};
|
2018-09-03 15:25:38 +00:00
|
|
|
const today = moment().format( isoDateFormat );
|
2018-10-18 04:37:28 +00:00
|
|
|
const startOfMonth = moment()
|
|
|
|
.startOf( 'month' )
|
|
|
|
.format( isoDateFormat );
|
|
|
|
const startOfMonthYearAgo = moment()
|
|
|
|
.startOf( 'month' )
|
|
|
|
.subtract( 1, 'year' )
|
|
|
|
.format( isoDateFormat );
|
|
|
|
const todayLastYear = moment()
|
|
|
|
.subtract( 1, 'year' )
|
2018-09-03 15:25:38 +00:00
|
|
|
.format( isoDateFormat );
|
2018-07-13 02:05:03 +00:00
|
|
|
const currentDates = getCurrentDates( query );
|
|
|
|
|
2018-10-18 04:37:28 +00:00
|
|
|
// Ensure default period is 'month'
|
2020-02-14 02:23:21 +00:00
|
|
|
expect( currentDates.primary.after.format( isoDateFormat ) ).toBe(
|
|
|
|
startOfMonth
|
|
|
|
);
|
|
|
|
expect( currentDates.primary.before.format( isoDateFormat ) ).toBe(
|
|
|
|
today
|
|
|
|
);
|
2018-07-13 02:05:03 +00:00
|
|
|
|
|
|
|
// Ensure default compare is `previous_period`
|
2020-02-14 02:23:21 +00:00
|
|
|
expect( currentDates.secondary.after.format( isoDateFormat ) ).toBe(
|
|
|
|
startOfMonthYearAgo
|
|
|
|
);
|
|
|
|
expect( currentDates.secondary.before.format( isoDateFormat ) ).toBe(
|
|
|
|
todayLastYear
|
|
|
|
);
|
2018-07-13 02:05:03 +00:00
|
|
|
} );
|
|
|
|
} );
|
2018-07-22 23:43:21 +00:00
|
|
|
|
|
|
|
describe( 'validateDateInputForRange', () => {
|
|
|
|
const dateFormat = 'YYYY-MM-DD';
|
|
|
|
|
|
|
|
it( 'should return a valid date in Moment object', () => {
|
2020-02-14 02:23:21 +00:00
|
|
|
const validated = validateDateInputForRange(
|
|
|
|
'after',
|
|
|
|
'2018-04-15',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
dateFormat
|
|
|
|
);
|
2018-07-22 23:43:21 +00:00
|
|
|
expect( moment.isMoment( validated.date ) ).toBe( true );
|
|
|
|
expect( validated.error ).toBe( undefined );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return a null date on invalid date string', () => {
|
|
|
|
const validated = validateDateInputForRange(
|
|
|
|
'after',
|
|
|
|
'BAd-2018-Date-Format/15/4',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
dateFormat
|
|
|
|
);
|
|
|
|
expect( validated.date ).toBe( null );
|
|
|
|
expect( validated.error ).toBe( dateValidationMessages.invalid );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return a correct error for a date in the future', () => {
|
|
|
|
const futureDateString = moment()
|
|
|
|
.add( 1, 'months' )
|
|
|
|
.format( dateFormat );
|
|
|
|
const validated = validateDateInputForRange(
|
|
|
|
'after',
|
|
|
|
futureDateString,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
dateFormat
|
|
|
|
);
|
|
|
|
expect( validated.date ).toBe( null );
|
|
|
|
expect( validated.error ).toBe( dateValidationMessages.future );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return a correct error for start', () => {
|
|
|
|
const futureDateString = moment()
|
|
|
|
.add( 1, 'months' )
|
|
|
|
.format( dateFormat );
|
|
|
|
const validated = validateDateInputForRange(
|
|
|
|
'after',
|
|
|
|
futureDateString,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
dateFormat
|
|
|
|
);
|
|
|
|
expect( validated.date ).toBe( null );
|
|
|
|
expect( validated.error ).toBe( dateValidationMessages.future );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return a correct error for start after end', () => {
|
|
|
|
const end = moment().subtract( 5, 'months' );
|
2020-07-28 02:32:58 +00:00
|
|
|
const value = end.clone().add( 1, 'months' ).format( dateFormat );
|
2020-02-14 02:23:21 +00:00
|
|
|
const validated = validateDateInputForRange(
|
|
|
|
'after',
|
|
|
|
value,
|
|
|
|
end,
|
|
|
|
null,
|
|
|
|
dateFormat
|
|
|
|
);
|
2018-07-22 23:43:21 +00:00
|
|
|
expect( validated.date ).toBe( null );
|
|
|
|
expect( validated.error ).toBe( dateValidationMessages.startAfterEnd );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return a correct error for end after start', () => {
|
|
|
|
const start = moment().subtract( 5, 'months' );
|
|
|
|
const value = start
|
|
|
|
.clone()
|
|
|
|
.subtract( 1, 'months' )
|
|
|
|
.format( dateFormat );
|
2020-02-14 02:23:21 +00:00
|
|
|
const validated = validateDateInputForRange(
|
|
|
|
'before',
|
|
|
|
value,
|
|
|
|
null,
|
|
|
|
start,
|
|
|
|
dateFormat
|
|
|
|
);
|
2018-07-22 23:43:21 +00:00
|
|
|
expect( validated.date ).toBe( null );
|
|
|
|
expect( validated.error ).toBe( dateValidationMessages.endBeforeStart );
|
|
|
|
} );
|
|
|
|
} );
|
2018-09-03 15:25:38 +00:00
|
|
|
|
|
|
|
describe( 'getDateDifferenceInDays', () => {
|
|
|
|
it( 'should calculate the day difference between two dates', () => {
|
2020-02-14 02:23:21 +00:00
|
|
|
const difference = getDateDifferenceInDays(
|
|
|
|
'2018-08-22',
|
|
|
|
'2018-05-22'
|
|
|
|
);
|
2018-09-03 15:25:38 +00:00
|
|
|
expect( difference ).toBe( 92 );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'getPreviousDate', () => {
|
2018-09-04 18:03:52 +00:00
|
|
|
it( 'should return valid date for previous period by days', () => {
|
|
|
|
const date = '2018-08-21';
|
|
|
|
const primaryStart = '2018-08-25';
|
|
|
|
const secondaryStart = '2018-08-15';
|
|
|
|
const previousDate = getPreviousDate(
|
|
|
|
date,
|
|
|
|
primaryStart,
|
|
|
|
secondaryStart,
|
|
|
|
'previous_period',
|
|
|
|
'day'
|
|
|
|
);
|
|
|
|
expect( previousDate.format( isoDateFormat ) ).toBe( '2018-08-11' );
|
|
|
|
} );
|
|
|
|
it( 'should return valid date for previous period by months', () => {
|
|
|
|
const date = '2018-08-21';
|
|
|
|
const primaryStart = '2018-08-01';
|
|
|
|
const secondaryStart = '2018-07-01';
|
|
|
|
const previousDate = getPreviousDate(
|
|
|
|
date,
|
|
|
|
primaryStart,
|
|
|
|
secondaryStart,
|
|
|
|
'previous_period',
|
|
|
|
'month'
|
|
|
|
);
|
|
|
|
expect( previousDate.format( isoDateFormat ) ).toBe( '2018-07-21' );
|
2018-09-03 15:25:38 +00:00
|
|
|
} );
|
|
|
|
it( 'should return valid date for previous year', () => {
|
2018-09-04 18:03:52 +00:00
|
|
|
const date = '2018-08-21';
|
|
|
|
const primaryStart = '2018-08-01';
|
|
|
|
const secondaryStart = '2018-07-01';
|
|
|
|
const previousDate = getPreviousDate(
|
|
|
|
date,
|
|
|
|
primaryStart,
|
|
|
|
secondaryStart,
|
|
|
|
'previous_year',
|
|
|
|
'day'
|
|
|
|
);
|
2018-09-03 15:25:38 +00:00
|
|
|
expect( previousDate.format( isoDateFormat ) ).toBe( '2017-08-21' );
|
|
|
|
} );
|
|
|
|
} );
|
2018-11-14 01:45:05 +00:00
|
|
|
|
|
|
|
describe( 'getChartTypeForQuery', () => {
|
|
|
|
it( 'should return allowed type', () => {
|
|
|
|
const query = {
|
2019-02-14 19:34:57 +00:00
|
|
|
chartType: 'bar',
|
2018-11-14 01:45:05 +00:00
|
|
|
};
|
|
|
|
expect( getChartTypeForQuery( query ) ).toBe( 'bar' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should default to line', () => {
|
|
|
|
expect( getChartTypeForQuery( {} ) ).toBe( 'line' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return line for not allowed type', () => {
|
|
|
|
const query = {
|
2019-02-14 19:34:57 +00:00
|
|
|
chartType: 'burrito',
|
2018-11-14 01:45:05 +00:00
|
|
|
};
|
|
|
|
expect( getChartTypeForQuery( query ) ).toBe( 'line' );
|
|
|
|
} );
|
|
|
|
} );
|
2021-04-16 20:13:39 +00:00
|
|
|
|
|
|
|
describe( 'getStoreTimeZoneMoment', () => {
|
|
|
|
it( 'should return the default moment when no timezone exists', () => {
|
|
|
|
const mockTz = ( moment.prototype.tz = jest.fn() );
|
|
|
|
const utcOffset = ( moment.prototype.utcOffset = jest.fn() );
|
|
|
|
|
|
|
|
expect( getStoreTimeZoneMoment() ).toHaveProperty( '_isAMomentObject' );
|
|
|
|
|
|
|
|
expect( mockTz ).not.toHaveBeenCalled();
|
|
|
|
expect( utcOffset ).not.toHaveBeenCalled();
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should use the timezone string when one is set', () => {
|
|
|
|
global.window.wcSettings = {
|
|
|
|
timeZone: 'Asia/Taipei',
|
|
|
|
};
|
|
|
|
|
|
|
|
const mockTz = ( moment.prototype.tz = jest.fn() );
|
|
|
|
const utcOffset = ( moment.prototype.utcOffset = jest.fn() );
|
|
|
|
|
|
|
|
getStoreTimeZoneMoment();
|
|
|
|
|
|
|
|
expect( mockTz ).toHaveBeenCalledWith( 'Asia/Taipei' );
|
|
|
|
expect( utcOffset ).not.toHaveBeenCalled();
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should use the utc offest when it is set', () => {
|
|
|
|
global.window.wcSettings = {
|
|
|
|
timeZone: '+06:00',
|
|
|
|
};
|
|
|
|
|
|
|
|
const mockTz = ( moment.prototype.tz = jest.fn() );
|
|
|
|
const utcOffset = ( moment.prototype.utcOffset = jest.fn() );
|
|
|
|
|
|
|
|
getStoreTimeZoneMoment();
|
|
|
|
|
|
|
|
expect( mockTz ).not.toHaveBeenCalled();
|
|
|
|
expect( utcOffset ).toHaveBeenCalledWith( '+06:00' );
|
|
|
|
|
|
|
|
global.window.wcSettings = {
|
|
|
|
timeZone: '-04:00',
|
|
|
|
};
|
|
|
|
|
|
|
|
getStoreTimeZoneMoment();
|
|
|
|
|
|
|
|
expect( mockTz ).not.toHaveBeenCalled();
|
|
|
|
expect( utcOffset ).toHaveBeenCalledWith( '-04:00' );
|
|
|
|
} );
|
|
|
|
} );
|