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
This commit is contained in:
RoxanaAnamariaTurc 2023-07-21 11:24:22 +01:00 committed by GitHub
parent e7ad60bfc7
commit eea18d6741
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 105 additions and 5 deletions

View File

@ -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);

View File

@ -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));
});
});
});

View File

@ -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');
});
});
});