From 7d89799194c9b9fbea2c2eaefe626f98a15b0b81 Mon Sep 17 00:00:00 2001 From: Timmy Crawford Date: Fri, 11 Jan 2019 07:51:24 -0800 Subject: [PATCH] Remove hour from default clause in getAllowedIntervalsForQuery when period is empty (https://github.com/woocommerce/woocommerce-admin/pull/1279) --- .../packages/date/CHANGELOG.md | 4 ++ .../packages/date/src/index.js | 2 +- .../packages/date/test/index.js | 48 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/plugins/woocommerce-admin/packages/date/CHANGELOG.md b/plugins/woocommerce-admin/packages/date/CHANGELOG.md index 04506f3d1ae..9f1028c1f46 100644 --- a/plugins/woocommerce-admin/packages/date/CHANGELOG.md +++ b/plugins/woocommerce-admin/packages/date/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.0.5 Not Released + +- Fixed bug in getAllowedIntervalsForQuery() to not return `hour` for default intervals + # 1.0.4 - Remove deprecated @wordpress/date::getSettings() usage. diff --git a/plugins/woocommerce-admin/packages/date/src/index.js b/plugins/woocommerce-admin/packages/date/src/index.js index 74a3afa1579..4dab1e1c185 100644 --- a/plugins/woocommerce-admin/packages/date/src/index.js +++ b/plugins/woocommerce-admin/packages/date/src/index.js @@ -395,7 +395,7 @@ export function getAllowedIntervalsForQuery( query ) { allowed = [ 'day', 'week', 'month', 'quarter' ]; break; default: - allowed = [ 'hour', 'day' ]; + allowed = [ 'day' ]; break; } } diff --git a/plugins/woocommerce-admin/packages/date/test/index.js b/plugins/woocommerce-admin/packages/date/test/index.js index 9cea2010b98..78c8914ed68 100644 --- a/plugins/woocommerce-admin/packages/date/test/index.js +++ b/plugins/woocommerce-admin/packages/date/test/index.js @@ -21,6 +21,7 @@ import { getDateDifferenceInDays, getPreviousDate, getChartTypeForQuery, + getAllowedIntervalsForQuery, } from '../src'; describe( 'appendTimestamp', () => { @@ -82,6 +83,53 @@ describe( 'toMoment', () => { } ); } ); +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', () => { + const allowedIntervalsToday = getAllowedIntervalsForQuery( { period: 'today' } ); + expect( allowedIntervalsToday ).toEqual( [ 'hour', 'day' ] ); + + const allowedIntervalsYesterday = getAllowedIntervalsForQuery( { period: 'yesterday' } ); + expect( allowedIntervalsYesterday ).toEqual( [ 'hour', 'day' ] ); + } ); + + it( 'should return day for week and last_week periods', () => { + const allowedIntervalsWeek = getAllowedIntervalsForQuery( { period: 'week' } ); + expect( allowedIntervalsWeek ).toEqual( [ 'day' ] ); + + const allowedIntervalsLastWeek = getAllowedIntervalsForQuery( { period: 'last_week' } ); + expect( allowedIntervalsLastWeek ).toEqual( [ 'day' ] ); + } ); + + it( 'should return day, week for month and last_month periods', () => { + const allowedIntervalsMonth = getAllowedIntervalsForQuery( { period: 'month' } ); + expect( allowedIntervalsMonth ).toEqual( [ 'day', 'week' ] ); + + const allowedIntervalsLastMonth = getAllowedIntervalsForQuery( { period: 'last_month' } ); + expect( allowedIntervalsLastMonth ).toEqual( [ 'day', 'week' ] ); + } ); + + it( 'should return day, week, month for quarter and last_quarter periods', () => { + const allowedIntervalsQuarter = getAllowedIntervalsForQuery( { period: 'quarter' } ); + expect( allowedIntervalsQuarter ).toEqual( [ 'day', 'week', 'month' ] ); + + const allowedIntervalsLastQuarter = getAllowedIntervalsForQuery( { period: 'last_quarter' } ); + expect( allowedIntervalsLastQuarter ).toEqual( [ 'day', 'week', 'month' ] ); + } ); + + it( 'should return day, week, month, quarter for year and last_year periods', () => { + 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' ] ); + } ); +} ); + describe( 'getCurrentPeriod', () => { it( 'should return a DateValue object with correct properties', () => { const dateValue = getCurrentPeriod( 'day', 'previous_period' );