diff --git a/public/pages/Overview/utils/constants.ts b/public/pages/Overview/utils/constants.ts index 12b80c0b1..d8438e667 100644 --- a/public/pages/Overview/utils/constants.ts +++ b/public/pages/Overview/utils/constants.ts @@ -9,18 +9,3 @@ export const summaryGroupByOptions = [ ]; export const moreLink = 'https://opensearch.org/docs/latest/security-analytics/'; - -/** - * Time axis' timeUnit map - * for each time search unit there is a mapped chart timeUnit - */ -export const TimeUnitsMap: { - [key: string]: string; -} = { - minutes: 'yearmonthdatehoursminutes', - hours: 'yearmonthdatehoursminutes', - days: 'yearmonthdatehours', - weeks: 'yearmonthdatehours', - months: 'yearmonthdatehours', - years: 'yearmonthdate', -}; diff --git a/public/pages/Overview/utils/helper.test.ts b/public/pages/Overview/utils/helper.test.ts index a267253c1..c681cc338 100644 --- a/public/pages/Overview/utils/helper.test.ts +++ b/public/pages/Overview/utils/helper.test.ts @@ -1,18 +1,53 @@ -import { getChartTimeUnit } from './helpers'; -import { TimeUnitsMap } from './constants'; +import { getChartTimeUnit, TimeUnit, TimeUnits } from './helpers'; describe('helper utilities spec', () => { describe('tests getChartTimeUnit function', () => { - const defaultTimeUnit = 'yearmonthdatehoursminutes'; + const defaultTimeUnit = { + timeUnit: { unit: 'yearmonthdatehoursminutes', step: 1 }, + dateFormat: '%Y-%m-%d %H:%M', + }; it(' - function should return default timeUnit if fn params are invalid', () => { - expect(getChartTimeUnit('', '')).toBe(defaultTimeUnit); + const timeUnits = getChartTimeUnit('', ''); + expect(timeUnits.dateFormat).toBe(defaultTimeUnit.dateFormat); + expect(timeUnits.timeUnit.unit).toBe(defaultTimeUnit.timeUnit.unit); + expect(timeUnits.timeUnit.step).toBe(defaultTimeUnit.timeUnit.step); }); it(' - function should return default timeUnit if one is passed as param', () => { - const defaultFormat = 'yearmonthdate'; - expect(getChartTimeUnit('', '', defaultFormat)).toBe(defaultFormat); + const defaultFormat = 'yearmonthdatehoursminutes'; + const timeUnits = getChartTimeUnit('', '', defaultFormat); + expect(timeUnits.timeUnit.unit).toBe(defaultFormat); }); + const timeUnitsExpected: { + [key: string]: TimeUnits; + } = { + minutes: { + dateFormat: '%Y-%m-%d %H:%M', + timeUnit: { step: 1, unit: 'yearmonthdatehoursminutes' }, + }, + hours: { + dateFormat: '%Y-%m-%d %H:%M', + timeUnit: { step: 1, unit: 'yearmonthdatehours' }, + }, + days: { + dateFormat: '%Y-%m-%d', + timeUnit: { step: 1, unit: 'yearmonthdate' }, + }, + weeks: { + dateFormat: '%Y-%m-%d', + timeUnit: { step: 1, unit: 'yearmonthdate' }, + }, + months: { + dateFormat: '%Y-%m-%d', + timeUnit: { step: 1, unit: 'yearmonthdate' }, + }, + years: { + dateFormat: '%Y', + timeUnit: { step: 1, unit: 'year' }, + }, + }; + const timeUnits: { [key: string]: string; } = { @@ -24,9 +59,16 @@ describe('helper utilities spec', () => { years: 'now-15y', }; + const validateTimeUnit = (timeUnit: TimeUnits, expectedTimeUnit: TimeUnits) => { + expect(timeUnit.dateFormat).toBe(expectedTimeUnit.dateFormat); + expect(timeUnit.timeUnit.unit).toBe(expectedTimeUnit.timeUnit.unit); + expect(timeUnit.timeUnit.step).toBe(expectedTimeUnit.timeUnit.step); + }; + for (const [unit, start] of Object.entries(timeUnits)) { - it(` - function should return ${TimeUnitsMap[unit]} if unit is ${unit}`, () => { - expect(getChartTimeUnit(start, 'now')).toBe(TimeUnitsMap[unit]); + it(` - filter ${start} should return valid timeUnit object`, () => { + const timeUnitResult = getChartTimeUnit(start, 'now'); + validateTimeUnit(timeUnitResult, timeUnitsExpected[unit]); }); } });