From 67535bb320d73e3693e273f90d1321ea6065a3a3 Mon Sep 17 00:00:00 2001 From: Yongjie Zhao Date: Fri, 23 Apr 2021 16:05:50 +0800 Subject: [PATCH] refactor: reorganize dateFilterUtils.tx (#14309) * refactor: reorganize dateFilterUtils.tx * move spec to src dirctory --- .../spec/javascripts/explore/utils_spec.jsx | 38 ------------------- .../DateFilterControl/DateFilterLabel.tsx | 13 +++---- .../components/AdvancedFrame.tsx | 2 +- .../utils}/dateFilterUtils.ts | 0 .../DateFilterControl/utils/dateParser.ts | 2 +- .../controls/DateFilterControl/utils/index.ts | 1 + .../DateFilterControl/utils/utils.test.ts} | 36 ++++++++++++++++++ 7 files changed, 45 insertions(+), 47 deletions(-) rename superset-frontend/src/explore/{ => components/controls/DateFilterControl/utils}/dateFilterUtils.ts (100%) rename superset-frontend/{spec/javascripts/explore/components/DateFilterControl/utils_spec.ts => src/explore/components/controls/DateFilterControl/utils/utils.test.ts} (88%) diff --git a/superset-frontend/spec/javascripts/explore/utils_spec.jsx b/superset-frontend/spec/javascripts/explore/utils_spec.jsx index d416fc05ff0..2c1eb036e3b 100644 --- a/superset-frontend/spec/javascripts/explore/utils_spec.jsx +++ b/superset-frontend/spec/javascripts/explore/utils_spec.jsx @@ -26,10 +26,6 @@ import { shouldUseLegacyApi, getSimpleSQLExpression, } from 'src/explore/exploreUtils'; -import { - buildTimeRangeString, - formatTimeRange, -} from 'src/explore/dateFilterUtils'; import { DashboardStandaloneMode } from 'src/dashboard/util/constants'; import * as hostNamesConfig from 'src/utils/hostNamesConfig'; import { getChartMetadataRegistry } from '@superset-ui/core'; @@ -264,40 +260,6 @@ describe('exploreUtils', () => { }); }); - describe('buildTimeRangeString', () => { - it('generates proper time range string', () => { - expect( - buildTimeRangeString('2010-07-30T00:00:00', '2020-07-30T00:00:00'), - ).toBe('2010-07-30T00:00:00 : 2020-07-30T00:00:00'); - expect(buildTimeRangeString('', '2020-07-30T00:00:00')).toBe( - ' : 2020-07-30T00:00:00', - ); - expect(buildTimeRangeString('', '')).toBe(' : '); - }); - }); - - describe('formatTimeRange', () => { - it('generates a readable time range', () => { - expect(formatTimeRange('Last 7 days')).toBe('Last 7 days'); - expect(formatTimeRange('No filter')).toBe('No filter'); - expect(formatTimeRange('Yesterday : Tomorrow')).toBe( - 'Yesterday < col < Tomorrow', - ); - expect( - formatTimeRange('2010-07-30T00:00:00 : 2020-07-30T00:00:00', [ - 'inclusive', - 'exclusive', - ]), - ).toBe('2010-07-30 ≤ col < 2020-07-30'); - expect( - formatTimeRange('2010-07-30T01:00:00 : ', ['exclusive', 'inclusive']), - ).toBe('2010-07-30T01:00:00 < col ≤ ∞'); - expect(formatTimeRange(' : 2020-07-30T00:00:00')).toBe( - '-∞ < col < 2020-07-30', - ); - }); - }); - describe('getSimpleSQLExpression', () => { it('returns empty string when subject is undefined', () => { expect(getSimpleSQLExpression(undefined, '=', 10)).toBe(''); diff --git a/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterLabel.tsx b/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterLabel.tsx index 811d5fbdf09..2674eca5f1d 100644 --- a/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterLabel.tsx +++ b/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterLabel.tsx @@ -28,7 +28,11 @@ import { import { buildTimeRangeString, formatTimeRange, -} from 'src/explore/dateFilterUtils'; + COMMON_RANGE_VALUES_SET, + CALENDAR_RANGE_VALUES_SET, + FRAME_OPTIONS, + customTimeRangeDecode, +} from 'src/explore/components/controls/DateFilterControl/utils'; import { getClientErrorObject } from 'src/utils/getClientErrorObject'; import Button from 'src/components/Button'; import ControlHeader from 'src/explore/components/ControlHeader'; @@ -43,12 +47,7 @@ import { useDebouncedEffect } from 'src/explore/exploreUtils'; import { SLOW_DEBOUNCE } from 'src/constants'; import { testWithId } from 'src/utils/testUtils'; import { SelectOptionType, FrameType } from './types'; -import { - COMMON_RANGE_VALUES_SET, - CALENDAR_RANGE_VALUES_SET, - FRAME_OPTIONS, - customTimeRangeDecode, -} from './utils'; + import { CommonFrame, CalendarFrame, diff --git a/superset-frontend/src/explore/components/controls/DateFilterControl/components/AdvancedFrame.tsx b/superset-frontend/src/explore/components/controls/DateFilterControl/components/AdvancedFrame.tsx index 0264da3b7a8..607cf992022 100644 --- a/superset-frontend/src/explore/components/controls/DateFilterControl/components/AdvancedFrame.tsx +++ b/superset-frontend/src/explore/components/controls/DateFilterControl/components/AdvancedFrame.tsx @@ -18,7 +18,7 @@ */ import React from 'react'; import { t } from '@superset-ui/core'; -import { SEPARATOR } from 'src/explore/dateFilterUtils'; +import { SEPARATOR } from 'src/explore/components/controls/DateFilterControl/utils'; import { Input } from 'src/common/components'; import { InfoTooltipWithTrigger } from '@superset-ui/chart-controls'; import { FrameComponentProps } from 'src/explore/components/controls/DateFilterControl/types'; diff --git a/superset-frontend/src/explore/dateFilterUtils.ts b/superset-frontend/src/explore/components/controls/DateFilterControl/utils/dateFilterUtils.ts similarity index 100% rename from superset-frontend/src/explore/dateFilterUtils.ts rename to superset-frontend/src/explore/components/controls/DateFilterControl/utils/dateFilterUtils.ts diff --git a/superset-frontend/src/explore/components/controls/DateFilterControl/utils/dateParser.ts b/superset-frontend/src/explore/components/controls/DateFilterControl/utils/dateParser.ts index 6dce8d46e52..d1be0def7e3 100644 --- a/superset-frontend/src/explore/components/controls/DateFilterControl/utils/dateParser.ts +++ b/superset-frontend/src/explore/components/controls/DateFilterControl/utils/dateParser.ts @@ -17,13 +17,13 @@ * under the License. */ import moment, { Moment } from 'moment'; -import { SEPARATOR } from 'src/explore/dateFilterUtils'; import { CustomRangeDecodeType, CustomRangeType, DateTimeGrainType, DateTimeModeType, } from 'src/explore/components/controls/DateFilterControl/types'; +import { SEPARATOR } from './dateFilterUtils'; import { SEVEN_DAYS_AGO, MIDNIGHT, MOMENT_FORMAT } from './constants'; /** diff --git a/superset-frontend/src/explore/components/controls/DateFilterControl/utils/index.ts b/superset-frontend/src/explore/components/controls/DateFilterControl/utils/index.ts index bc555aa365d..542e8ee616e 100644 --- a/superset-frontend/src/explore/components/controls/DateFilterControl/utils/index.ts +++ b/superset-frontend/src/explore/components/controls/DateFilterControl/utils/index.ts @@ -18,3 +18,4 @@ */ export * from './dateParser'; export * from './constants'; +export * from './dateFilterUtils'; diff --git a/superset-frontend/spec/javascripts/explore/components/DateFilterControl/utils_spec.ts b/superset-frontend/src/explore/components/controls/DateFilterControl/utils/utils.test.ts similarity index 88% rename from superset-frontend/spec/javascripts/explore/components/DateFilterControl/utils_spec.ts rename to superset-frontend/src/explore/components/controls/DateFilterControl/utils/utils.test.ts index d44b08dddd9..012aab606c7 100644 --- a/superset-frontend/spec/javascripts/explore/components/DateFilterControl/utils_spec.ts +++ b/superset-frontend/src/explore/components/controls/DateFilterControl/utils/utils.test.ts @@ -20,6 +20,8 @@ import { customTimeRangeEncode, customTimeRangeDecode, + buildTimeRangeString, + formatTimeRange, } from 'src/explore/components/controls/DateFilterControl/utils'; describe('Custom TimeRange', () => { @@ -296,3 +298,37 @@ describe('Custom TimeRange', () => { }); }); }); + +describe('buildTimeRangeString', () => { + it('generates proper time range string', () => { + expect( + buildTimeRangeString('2010-07-30T00:00:00', '2020-07-30T00:00:00'), + ).toBe('2010-07-30T00:00:00 : 2020-07-30T00:00:00'); + expect(buildTimeRangeString('', '2020-07-30T00:00:00')).toBe( + ' : 2020-07-30T00:00:00', + ); + expect(buildTimeRangeString('', '')).toBe(' : '); + }); +}); + +describe('formatTimeRange', () => { + it('generates a readable time range', () => { + expect(formatTimeRange('Last 7 days')).toBe('Last 7 days'); + expect(formatTimeRange('No filter')).toBe('No filter'); + expect(formatTimeRange('Yesterday : Tomorrow')).toBe( + 'Yesterday < col < Tomorrow', + ); + expect( + formatTimeRange('2010-07-30T00:00:00 : 2020-07-30T00:00:00', [ + 'inclusive', + 'exclusive', + ]), + ).toBe('2010-07-30 ≤ col < 2020-07-30'); + expect( + formatTimeRange('2010-07-30T01:00:00 : ', ['exclusive', 'inclusive']), + ).toBe('2010-07-30T01:00:00 < col ≤ ∞'); + expect(formatTimeRange(' : 2020-07-30T00:00:00')).toBe( + '-∞ < col < 2020-07-30', + ); + }); +});