From eea18d674101284c17364b1e532e8ed0c3f8a142 Mon Sep 17 00:00:00 2001 From: RoxanaAnamariaTurc <106086831+RoxanaAnamariaTurc@users.noreply.github.com> Date: Fri, 21 Jul 2023 11:24:22 +0100 Subject: [PATCH] Datetime: Added more tests to the files in the datetime folder (#71731) * Datetime: Added more tests to the files in the datetime folder * Updated tests suites according to feedback received * Corrected the lint error * Removed repetitive code --- .../src/datetime/datemath.test.ts | 23 ++++++ .../src/datetime/durationutil.test.ts | 12 +++ .../src/datetime/formatter.test.ts | 75 +++++++++++++++++-- 3 files changed, 105 insertions(+), 5 deletions(-) diff --git a/packages/grafana-data/src/datetime/datemath.test.ts b/packages/grafana-data/src/datetime/datemath.test.ts index cc857c9311f..c790d98c743 100644 --- a/packages/grafana-data/src/datetime/datemath.test.ts +++ b/packages/grafana-data/src/datetime/datemath.test.ts @@ -183,6 +183,29 @@ describe('DateMath', () => { }); }); + describe('isMathString', () => { + it('should return true when valid date text', () => { + expect(dateMath.isMathString('now-1h')).toBe(true); + }); + + it('should return false when an absolute date is inserted', () => { + const date = new Date(); + const result = dateMath.isMathString(date); + expect(result).toBe(false); + }); + + it('should return false when invalid date text', () => { + expect(dateMath.isMathString('2 + 2')).toBe(false); + }); + + it('should return false if no text is passed', () => { + expect(dateMath.isMathString('')).toBe(false); + }); + it('should return false if nothing is passed ', () => { + expect(dateMath.isMathString(' ')).toBe(false); + }); + }); + describe('Round to fiscal start/end', () => { it('Should round to start of fiscal year when datetime is the same year as the start of the fiscal year', () => { let date = dateMath.roundToFiscal(1, dateTime([2021, 3, 5]), 'y', false); diff --git a/packages/grafana-data/src/datetime/durationutil.test.ts b/packages/grafana-data/src/datetime/durationutil.test.ts index 8320527fbdf..f7b8814e70b 100644 --- a/packages/grafana-data/src/datetime/durationutil.test.ts +++ b/packages/grafana-data/src/datetime/durationutil.test.ts @@ -4,6 +4,7 @@ import { parseDuration, isValidDuration, isValidGoDuration, + durationToMilliseconds, } from './durationutil'; describe('Duration util', () => { @@ -66,4 +67,15 @@ describe('Duration util', () => { expect(isValidGoDuration(durationString)).toEqual(false); }); }); + + describe('durationToMilliseconds', () => { + it('converts a duration to milliseconds', () => { + const duration = { hours: 1, minutes: 30, seconds: 45 }; + + const now = new Date('2023-07-12'); + const result = addDurationToDate(now, duration).getTime() - now.getTime(); + + expect(result).toEqual(durationToMilliseconds(duration)); + }); + }); }); diff --git a/packages/grafana-data/src/datetime/formatter.test.ts b/packages/grafana-data/src/datetime/formatter.test.ts index a97a6d534d0..888f3d07c8e 100644 --- a/packages/grafana-data/src/datetime/formatter.test.ts +++ b/packages/grafana-data/src/datetime/formatter.test.ts @@ -1,4 +1,4 @@ -import { dateTimeFormat } from './formatter'; +import { dateTimeFormat, dateTimeFormatTimeAgo, dateTimeFormatWithAbbrevation, timeZoneAbbrevation } from './formatter'; describe('dateTimeFormat', () => { describe('when no time zone have been set', () => { @@ -66,11 +66,76 @@ describe('dateTimeFormat', () => { }); }); - describe('when Africa/Djibouti time zone have been set', () => { - const options = { timeZone: 'Africa/Djibouti' }; + describe('when time zone have been set', () => { + it.each([ + ['Africa/Djibouti', '2020-04-17 15:36:15'], + ['Europe/London', '2020-04-17 13:36:15'], + ['Europe/Berlin', '2020-04-17 14:36:15'], + ['Europe/Moscow', '2020-04-17 15:36:15'], + ['Europe/Madrid', '2020-04-17 14:36:15'], + ['America/New_York', '2020-04-17 08:36:15'], + ['America/Chicago', '2020-04-17 07:36:15'], + ['America/Denver', '2020-04-17 06:36:15'], + ])('should format with default formatting in correct time zone', (timeZone, expected) => { + expect(dateTimeFormat(1587126975779, { timeZone })).toBe(expected); + }); + }); - it('should format with default formatting in correct time zone', () => { - expect(dateTimeFormat(1587126975779, options)).toBe('2020-04-17 15:36:15'); + describe('DateTimeFormatISO', () => { + it('should format according to ISO standard', () => { + const options = { timeZone: 'Europe/Stockholm', format: 'YYYY-MM-DDTHH:mm:ss.SSSZ' }; + expect(dateTimeFormat(1587126975779, options)).toBe('2020-04-17T14:36:15.779+02:00'); + }); + it('should format according to ISO standard', () => { + const options = { timeZone: 'America/New_York', format: 'YYYY-MM-DDTHH:mm:ss.SSSZ' }; + expect(dateTimeFormat(1587126975779, options)).toBe('2020-04-17T08:36:15.779-04:00'); + }); + it('should format according to ISO standard', () => { + const options = { timeZone: 'Europe/Madrid', format: 'YYYY-MM-DDTHH:mm:ss.SSSZ' }; + expect(dateTimeFormat(1587126975779, options)).toBe('2020-04-17T14:36:15.779+02:00'); + }); + }); + + describe('dateTimeFormatTimeAgo', () => { + it('should return the correct format for 3 years ago', () => { + const options = { timeZone: 'Europe/Stockholm' }; + expect(dateTimeFormatTimeAgo(1587126975779, options)).toBe('3 years ago'); + }); + it('should return the correct format for 2 year ago', () => { + const options = { timeZone: 'Europe/Stockholm' }; + expect(dateTimeFormatTimeAgo(1626154993000, options)).toBe('2 years ago'); + }); + it('should return the correct format for 1 year ago', () => { + const options = { timeZone: 'Europe/Stockholm' }; + expect(dateTimeFormatTimeAgo(1657731795000, options)).toBe('a year ago'); + }); + }); + describe('dateTimeFormatWithAbbreviation', () => { + it('should return the correct format with zone abbreviation', () => { + const options = { timeZone: 'Europe/Stockholm' }; + expect(dateTimeFormatWithAbbrevation(1587126975779, options)).toBe('2020-04-17 14:36:15 CEST'); + }); + it('should return the correct format with zone abbreviation', () => { + const options = { timeZone: 'America/New_York' }; + expect(dateTimeFormatWithAbbrevation(1587126975779, options)).toBe('2020-04-17 08:36:15 EDT'); + }); + it('should return the correct format with zone abbreviation', () => { + const options = { timeZone: 'Europe/Bucharest' }; + expect(dateTimeFormatWithAbbrevation(1587126975779, options)).toBe('2020-04-17 15:36:15 EEST'); + }); + }); + describe('timeZoneAbbrevation', () => { + it('should return the correct abbreviation', () => { + const options = { timeZone: 'Europe/Stockholm' }; + expect(timeZoneAbbrevation(1587126975779, options)).toBe('CEST'); + }); + it('should return the correct abbreviation', () => { + const options = { timeZone: 'America/New_York' }; + expect(timeZoneAbbrevation(1587126975779, options)).toBe('EDT'); + }); + it('should return the correct abbreviation', () => { + const options = { timeZone: 'Europe/Bucharest' }; + expect(timeZoneAbbrevation(1587126975779, options)).toBe('EEST'); }); }); });