2020-02-25 07:08:48 -05:00
|
|
|
import $ from 'jquery';
|
2020-07-13 08:09:18 -04:00
|
|
|
import timezoneMock from 'timezone-mock';
|
2020-08-17 17:09:56 -04:00
|
|
|
import { __, s__ } from '~/locale';
|
2020-02-25 07:08:48 -05:00
|
|
|
import '~/commons/bootstrap';
|
2017-11-10 18:41:04 -05:00
|
|
|
import * as datetimeUtility from '~/lib/utils/datetime_utility';
|
2016-12-13 22:01:05 -05:00
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
describe('Date time utils', () => {
|
|
|
|
describe('timeFor', () => {
|
2019-11-27 10:06:45 -05:00
|
|
|
it('returns localize `past due` when in past', () => {
|
2017-12-07 06:09:17 -05:00
|
|
|
const date = new Date();
|
|
|
|
date.setFullYear(date.getFullYear() - 1);
|
|
|
|
|
2019-11-27 10:06:45 -05:00
|
|
|
expect(datetimeUtility.timeFor(date)).toBe(s__('Timeago|Past due'));
|
2017-06-07 16:13:44 -04:00
|
|
|
});
|
|
|
|
|
2019-11-27 10:06:45 -05:00
|
|
|
it('returns localized remaining time when in the future', () => {
|
2017-12-07 06:09:17 -05:00
|
|
|
const date = new Date();
|
|
|
|
date.setFullYear(date.getFullYear() + 1);
|
|
|
|
|
|
|
|
// Add a day to prevent a transient error. If date is even 1 second
|
|
|
|
// short of a full year, timeFor will return '11 months remaining'
|
|
|
|
date.setDate(date.getDate() + 1);
|
2016-08-25 21:19:44 -04:00
|
|
|
|
2019-11-27 10:06:45 -05:00
|
|
|
expect(datetimeUtility.timeFor(date)).toBe(s__('Timeago|1 year remaining'));
|
2016-08-25 21:19:44 -04:00
|
|
|
});
|
|
|
|
});
|
2017-06-12 05:20:19 -04:00
|
|
|
|
2019-11-27 10:06:45 -05:00
|
|
|
describe('get localized day name', () => {
|
2017-12-07 06:09:17 -05:00
|
|
|
it('should return Sunday', () => {
|
|
|
|
const day = datetimeUtility.getDayName(new Date('07/17/2016'));
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2019-11-27 10:06:45 -05:00
|
|
|
expect(day).toBe(__('Sunday'));
|
2017-12-07 06:09:17 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return Monday', () => {
|
|
|
|
const day = datetimeUtility.getDayName(new Date('07/18/2016'));
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2019-11-27 10:06:45 -05:00
|
|
|
expect(day).toBe(__('Monday'));
|
2017-12-07 06:09:17 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return Tuesday', () => {
|
|
|
|
const day = datetimeUtility.getDayName(new Date('07/19/2016'));
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2019-11-27 10:06:45 -05:00
|
|
|
expect(day).toBe(__('Tuesday'));
|
2017-12-07 06:09:17 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return Wednesday', () => {
|
|
|
|
const day = datetimeUtility.getDayName(new Date('07/20/2016'));
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2019-11-27 10:06:45 -05:00
|
|
|
expect(day).toBe(__('Wednesday'));
|
2017-12-07 06:09:17 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return Thursday', () => {
|
|
|
|
const day = datetimeUtility.getDayName(new Date('07/21/2016'));
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2019-11-27 10:06:45 -05:00
|
|
|
expect(day).toBe(__('Thursday'));
|
2017-12-07 06:09:17 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return Friday', () => {
|
|
|
|
const day = datetimeUtility.getDayName(new Date('07/22/2016'));
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2019-11-27 10:06:45 -05:00
|
|
|
expect(day).toBe(__('Friday'));
|
2017-12-07 06:09:17 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return Saturday', () => {
|
|
|
|
const day = datetimeUtility.getDayName(new Date('07/23/2016'));
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2019-11-27 10:06:45 -05:00
|
|
|
expect(day).toBe(__('Saturday'));
|
2017-11-10 18:41:04 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-05-28 03:53:44 -04:00
|
|
|
describe('formatDate', () => {
|
|
|
|
it('should format date properly', () => {
|
|
|
|
const formattedDate = datetimeUtility.formatDate(new Date('07/23/2016'));
|
|
|
|
|
|
|
|
expect(formattedDate).toBe('Jul 23, 2016 12:00am GMT+0000');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should format ISO date properly', () => {
|
|
|
|
const formattedDate = datetimeUtility.formatDate('2016-07-23T00:00:00.559Z');
|
|
|
|
|
|
|
|
expect(formattedDate).toBe('Jul 23, 2016 12:00am GMT+0000');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if date is invalid', () => {
|
|
|
|
expect(() => {
|
|
|
|
datetimeUtility.formatDate('2016-07-23 00:00:00 UTC');
|
|
|
|
}).toThrow(new Error('Invalid date'));
|
|
|
|
});
|
2020-07-13 08:09:18 -04:00
|
|
|
|
|
|
|
describe('convert local timezone to UTC with utc parameter', () => {
|
|
|
|
const midnightUTC = '2020-07-09';
|
|
|
|
const format = 'mmm d, yyyy';
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
timezoneMock.register('US/Pacific');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
timezoneMock.unregister();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('defaults to false', () => {
|
|
|
|
const formattedDate = datetimeUtility.formatDate(midnightUTC, format);
|
|
|
|
|
|
|
|
expect(formattedDate).toBe('Jul 8, 2020');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('converts local time to UTC if utc flag is true', () => {
|
|
|
|
const formattedDate = datetimeUtility.formatDate(midnightUTC, format, true);
|
|
|
|
|
|
|
|
expect(formattedDate).toBe('Jul 9, 2020');
|
|
|
|
});
|
|
|
|
});
|
2019-05-28 03:53:44 -04:00
|
|
|
});
|
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
describe('get day difference', () => {
|
|
|
|
it('should return 7', () => {
|
|
|
|
const firstDay = new Date('07/01/2016');
|
|
|
|
const secondDay = new Date('07/08/2016');
|
|
|
|
const difference = datetimeUtility.getDayDifference(firstDay, secondDay);
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
expect(difference).toBe(7);
|
|
|
|
});
|
2017-11-10 18:41:04 -05:00
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
it('should return 31', () => {
|
|
|
|
const firstDay = new Date('07/01/2016');
|
|
|
|
const secondDay = new Date('08/01/2016');
|
|
|
|
const difference = datetimeUtility.getDayDifference(firstDay, secondDay);
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
expect(difference).toBe(31);
|
2017-11-10 18:41:04 -05:00
|
|
|
});
|
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
it('should return 365', () => {
|
|
|
|
const firstDay = new Date('07/02/2015');
|
|
|
|
const secondDay = new Date('07/01/2016');
|
|
|
|
const difference = datetimeUtility.getDayDifference(firstDay, secondDay);
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
expect(difference).toBe(365);
|
2017-06-12 05:20:19 -04:00
|
|
|
});
|
|
|
|
});
|
2017-12-07 06:09:17 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('timeIntervalInWords', () => {
|
|
|
|
it('should return string with number of minutes and seconds', () => {
|
2019-11-27 10:06:45 -05:00
|
|
|
expect(datetimeUtility.timeIntervalInWords(9.54)).toEqual(s__('Timeago|9 seconds'));
|
|
|
|
expect(datetimeUtility.timeIntervalInWords(1)).toEqual(s__('Timeago|1 second'));
|
|
|
|
expect(datetimeUtility.timeIntervalInWords(200)).toEqual(s__('Timeago|3 minutes 20 seconds'));
|
|
|
|
expect(datetimeUtility.timeIntervalInWords(6008)).toEqual(s__('Timeago|100 minutes 8 seconds'));
|
2017-12-07 06:09:17 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('dateInWords', () => {
|
|
|
|
const date = new Date('07/01/2016');
|
|
|
|
|
|
|
|
it('should return date in words', () => {
|
2019-11-27 10:06:45 -05:00
|
|
|
expect(datetimeUtility.dateInWords(date)).toEqual(s__('July 1, 2016'));
|
2017-12-07 06:09:17 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return abbreviated month name', () => {
|
2019-11-27 10:06:45 -05:00
|
|
|
expect(datetimeUtility.dateInWords(date, true)).toEqual(s__('Jul 1, 2016'));
|
2017-12-07 06:09:17 -05:00
|
|
|
});
|
2018-02-03 03:07:24 -05:00
|
|
|
|
|
|
|
it('should return date in words without year', () => {
|
2019-11-27 10:06:45 -05:00
|
|
|
expect(datetimeUtility.dateInWords(date, true, true)).toEqual(s__('Jul 1'));
|
2018-02-03 03:07:24 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('monthInWords', () => {
|
|
|
|
const date = new Date('2017-01-20');
|
|
|
|
|
|
|
|
it('returns month name from provided date', () => {
|
2019-11-27 10:06:45 -05:00
|
|
|
expect(datetimeUtility.monthInWords(date)).toBe(s__('January'));
|
2018-02-03 03:07:24 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns abbreviated month name from provided date', () => {
|
2019-11-27 10:06:45 -05:00
|
|
|
expect(datetimeUtility.monthInWords(date, true)).toBe(s__('Jan'));
|
2018-02-03 03:07:24 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('totalDaysInMonth', () => {
|
|
|
|
it('returns number of days in a month for given date', () => {
|
|
|
|
// 1st Feb, 2016 (leap year)
|
|
|
|
expect(datetimeUtility.totalDaysInMonth(new Date(2016, 1, 1))).toBe(29);
|
|
|
|
|
|
|
|
// 1st Feb, 2017
|
|
|
|
expect(datetimeUtility.totalDaysInMonth(new Date(2017, 1, 1))).toBe(28);
|
|
|
|
|
|
|
|
// 1st Jan, 2017
|
|
|
|
expect(datetimeUtility.totalDaysInMonth(new Date(2017, 0, 1))).toBe(31);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getSundays', () => {
|
|
|
|
it('returns array of dates representing all Sundays of the month', () => {
|
|
|
|
// December, 2017 (it has 5 Sundays)
|
|
|
|
const dateOfSundays = [3, 10, 17, 24, 31];
|
|
|
|
const sundays = datetimeUtility.getSundays(new Date(2017, 11, 1));
|
|
|
|
|
|
|
|
expect(sundays.length).toBe(5);
|
|
|
|
sundays.forEach((sunday, index) => {
|
|
|
|
expect(sunday.getDate()).toBe(dateOfSundays[index]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-06-07 12:08:27 -04:00
|
|
|
describe('getTimeframeWindowFrom', () => {
|
2019-01-08 08:10:41 -05:00
|
|
|
it('returns array of date objects upto provided length (positive number) into the future starting from provided startDate', () => {
|
2018-06-07 12:08:27 -04:00
|
|
|
const startDate = new Date(2018, 0, 1);
|
2018-02-03 03:07:24 -05:00
|
|
|
const mockTimeframe = [
|
|
|
|
new Date(2018, 0, 1),
|
|
|
|
new Date(2018, 1, 1),
|
2018-06-07 12:08:27 -04:00
|
|
|
new Date(2018, 2, 1),
|
|
|
|
new Date(2018, 3, 1),
|
|
|
|
new Date(2018, 4, 31),
|
2018-02-03 03:07:24 -05:00
|
|
|
];
|
2018-06-07 12:08:27 -04:00
|
|
|
const timeframe = datetimeUtility.getTimeframeWindowFrom(startDate, 5);
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2018-06-07 12:08:27 -04:00
|
|
|
expect(timeframe.length).toBe(5);
|
2018-02-03 03:07:24 -05:00
|
|
|
timeframe.forEach((timeframeItem, index) => {
|
2018-10-09 14:30:03 -04:00
|
|
|
expect(timeframeItem.getFullYear()).toBe(mockTimeframe[index].getFullYear());
|
|
|
|
expect(timeframeItem.getMonth()).toBe(mockTimeframe[index].getMonth());
|
|
|
|
expect(timeframeItem.getDate()).toBe(mockTimeframe[index].getDate());
|
2018-02-03 03:07:24 -05:00
|
|
|
});
|
|
|
|
});
|
2019-01-08 08:10:41 -05:00
|
|
|
|
|
|
|
it('returns array of date objects upto provided length (negative number) into the past starting from provided startDate', () => {
|
|
|
|
const startDate = new Date(2018, 0, 1);
|
|
|
|
const mockTimeframe = [
|
|
|
|
new Date(2018, 0, 1),
|
|
|
|
new Date(2017, 11, 1),
|
|
|
|
new Date(2017, 10, 1),
|
|
|
|
new Date(2017, 9, 1),
|
|
|
|
new Date(2017, 8, 1),
|
|
|
|
];
|
|
|
|
const timeframe = datetimeUtility.getTimeframeWindowFrom(startDate, -5);
|
|
|
|
|
|
|
|
expect(timeframe.length).toBe(5);
|
|
|
|
timeframe.forEach((timeframeItem, index) => {
|
|
|
|
expect(timeframeItem.getFullYear()).toBe(mockTimeframe[index].getFullYear());
|
|
|
|
expect(timeframeItem.getMonth()).toBe(mockTimeframe[index].getMonth());
|
|
|
|
expect(timeframeItem.getDate()).toBe(mockTimeframe[index].getDate());
|
|
|
|
});
|
|
|
|
});
|
2017-12-07 06:09:17 -05:00
|
|
|
});
|
2018-10-01 09:21:33 -04:00
|
|
|
|
|
|
|
describe('formatTime', () => {
|
|
|
|
const expectedTimestamps = [
|
|
|
|
[0, '00:00:00'],
|
|
|
|
[1000, '00:00:01'],
|
|
|
|
[42000, '00:00:42'],
|
|
|
|
[121000, '00:02:01'],
|
|
|
|
[10921000, '03:02:01'],
|
|
|
|
[108000000, '30:00:00'],
|
|
|
|
];
|
|
|
|
|
|
|
|
expectedTimestamps.forEach(([milliseconds, expectedTimestamp]) => {
|
|
|
|
it(`formats ${milliseconds}ms as ${expectedTimestamp}`, () => {
|
|
|
|
expect(datetimeUtility.formatTime(milliseconds)).toBe(expectedTimestamp);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-10-24 06:50:55 -04:00
|
|
|
|
|
|
|
describe('datefix', () => {
|
|
|
|
describe('pad', () => {
|
|
|
|
it('should add a 0 when length is smaller than 2', () => {
|
|
|
|
expect(datetimeUtility.pad(2)).toEqual('02');
|
|
|
|
});
|
|
|
|
|
2018-10-30 06:53:01 -04:00
|
|
|
it('should not add a zero when length matches the default', () => {
|
2018-10-24 06:50:55 -04:00
|
|
|
expect(datetimeUtility.pad(12)).toEqual('12');
|
|
|
|
});
|
|
|
|
|
2018-10-30 06:53:01 -04:00
|
|
|
it('should add a 0 when length is smaller than the provided', () => {
|
2018-10-24 06:50:55 -04:00
|
|
|
expect(datetimeUtility.pad(12, 3)).toEqual('012');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('parsePikadayDate', () => {
|
2019-09-18 10:02:45 -04:00
|
|
|
// removed because of https://gitlab.com/gitlab-org/gitlab-foss/issues/39834
|
2018-10-24 06:50:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('pikadayToString', () => {
|
|
|
|
it('should format a UTC date into yyyy-mm-dd format', () => {
|
|
|
|
expect(datetimeUtility.pikadayToString(new Date('2020-01-29:00:00'))).toEqual('2020-01-29');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-10-24 07:02:23 -04:00
|
|
|
|
|
|
|
describe('prettyTime methods', () => {
|
|
|
|
const assertTimeUnits = (obj, minutes, hours, days, weeks) => {
|
|
|
|
expect(obj.minutes).toBe(minutes);
|
|
|
|
expect(obj.hours).toBe(hours);
|
|
|
|
expect(obj.days).toBe(days);
|
|
|
|
expect(obj.weeks).toBe(weeks);
|
2018-10-24 07:30:39 -04:00
|
|
|
};
|
2018-10-24 07:02:23 -04:00
|
|
|
|
|
|
|
describe('parseSeconds', () => {
|
|
|
|
it('should correctly parse a negative value', () => {
|
|
|
|
const zeroSeconds = datetimeUtility.parseSeconds(-1000);
|
|
|
|
|
|
|
|
assertTimeUnits(zeroSeconds, 16, 0, 0, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should correctly parse a zero value', () => {
|
|
|
|
const zeroSeconds = datetimeUtility.parseSeconds(0);
|
|
|
|
|
|
|
|
assertTimeUnits(zeroSeconds, 0, 0, 0, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should correctly parse a small non-zero second values', () => {
|
|
|
|
const subOneMinute = datetimeUtility.parseSeconds(10);
|
|
|
|
const aboveOneMinute = datetimeUtility.parseSeconds(100);
|
|
|
|
const manyMinutes = datetimeUtility.parseSeconds(1000);
|
|
|
|
|
|
|
|
assertTimeUnits(subOneMinute, 0, 0, 0, 0);
|
|
|
|
assertTimeUnits(aboveOneMinute, 1, 0, 0, 0);
|
|
|
|
assertTimeUnits(manyMinutes, 16, 0, 0, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should correctly parse large second values', () => {
|
|
|
|
const aboveOneHour = datetimeUtility.parseSeconds(4800);
|
|
|
|
const aboveOneDay = datetimeUtility.parseSeconds(110000);
|
|
|
|
const aboveOneWeek = datetimeUtility.parseSeconds(25000000);
|
|
|
|
|
|
|
|
assertTimeUnits(aboveOneHour, 20, 1, 0, 0);
|
|
|
|
assertTimeUnits(aboveOneDay, 33, 6, 3, 0);
|
|
|
|
assertTimeUnits(aboveOneWeek, 26, 0, 3, 173);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should correctly accept a custom param for hoursPerDay', () => {
|
|
|
|
const config = { hoursPerDay: 24 };
|
|
|
|
|
|
|
|
const aboveOneHour = datetimeUtility.parseSeconds(4800, config);
|
|
|
|
const aboveOneDay = datetimeUtility.parseSeconds(110000, config);
|
|
|
|
const aboveOneWeek = datetimeUtility.parseSeconds(25000000, config);
|
|
|
|
|
|
|
|
assertTimeUnits(aboveOneHour, 20, 1, 0, 0);
|
|
|
|
assertTimeUnits(aboveOneDay, 33, 6, 1, 0);
|
|
|
|
assertTimeUnits(aboveOneWeek, 26, 8, 4, 57);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should correctly accept a custom param for daysPerWeek', () => {
|
|
|
|
const config = { daysPerWeek: 7 };
|
|
|
|
|
|
|
|
const aboveOneHour = datetimeUtility.parseSeconds(4800, config);
|
|
|
|
const aboveOneDay = datetimeUtility.parseSeconds(110000, config);
|
|
|
|
const aboveOneWeek = datetimeUtility.parseSeconds(25000000, config);
|
|
|
|
|
|
|
|
assertTimeUnits(aboveOneHour, 20, 1, 0, 0);
|
|
|
|
assertTimeUnits(aboveOneDay, 33, 6, 3, 0);
|
|
|
|
assertTimeUnits(aboveOneWeek, 26, 0, 0, 124);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should correctly accept custom params for daysPerWeek and hoursPerDay', () => {
|
|
|
|
const config = { daysPerWeek: 55, hoursPerDay: 14 };
|
|
|
|
|
|
|
|
const aboveOneHour = datetimeUtility.parseSeconds(4800, config);
|
|
|
|
const aboveOneDay = datetimeUtility.parseSeconds(110000, config);
|
|
|
|
const aboveOneWeek = datetimeUtility.parseSeconds(25000000, config);
|
|
|
|
|
|
|
|
assertTimeUnits(aboveOneHour, 20, 1, 0, 0);
|
|
|
|
assertTimeUnits(aboveOneDay, 33, 2, 2, 0);
|
|
|
|
assertTimeUnits(aboveOneWeek, 26, 0, 1, 9);
|
|
|
|
});
|
2019-06-11 06:40:01 -04:00
|
|
|
|
|
|
|
it('should correctly parse values when limitedToHours is true', () => {
|
|
|
|
const twoDays = datetimeUtility.parseSeconds(173000, { limitToHours: true });
|
|
|
|
|
|
|
|
assertTimeUnits(twoDays, 3, 48, 0, 0);
|
|
|
|
});
|
2019-12-24 07:08:01 -05:00
|
|
|
|
|
|
|
it('should correctly parse values when limitedToDays is true', () => {
|
|
|
|
const sevenDays = datetimeUtility.parseSeconds(648750, {
|
|
|
|
hoursPerDay: 24,
|
|
|
|
daysPerWeek: 7,
|
|
|
|
limitToDays: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
assertTimeUnits(sevenDays, 12, 12, 7, 0);
|
|
|
|
});
|
2018-10-24 07:02:23 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('stringifyTime', () => {
|
|
|
|
it('should stringify values with all non-zero units', () => {
|
|
|
|
const timeObject = {
|
|
|
|
weeks: 1,
|
|
|
|
days: 4,
|
|
|
|
hours: 7,
|
|
|
|
minutes: 20,
|
|
|
|
};
|
|
|
|
|
|
|
|
const timeString = datetimeUtility.stringifyTime(timeObject);
|
|
|
|
|
|
|
|
expect(timeString).toBe('1w 4d 7h 20m');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should stringify values with some non-zero units', () => {
|
|
|
|
const timeObject = {
|
|
|
|
weeks: 0,
|
|
|
|
days: 4,
|
|
|
|
hours: 0,
|
|
|
|
minutes: 20,
|
|
|
|
};
|
|
|
|
|
|
|
|
const timeString = datetimeUtility.stringifyTime(timeObject);
|
|
|
|
|
|
|
|
expect(timeString).toBe('4d 20m');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should stringify values with no non-zero units', () => {
|
|
|
|
const timeObject = {
|
|
|
|
weeks: 0,
|
|
|
|
days: 0,
|
|
|
|
hours: 0,
|
|
|
|
minutes: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
const timeString = datetimeUtility.stringifyTime(timeObject);
|
|
|
|
|
|
|
|
expect(timeString).toBe('0m');
|
2018-11-07 12:20:17 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return non-condensed representation of time object', () => {
|
|
|
|
const timeObject = { weeks: 1, days: 0, hours: 1, minutes: 0 };
|
|
|
|
|
|
|
|
expect(datetimeUtility.stringifyTime(timeObject, true)).toEqual('1 week 1 hour');
|
2018-10-24 07:02:23 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-10-19 13:53:41 -04:00
|
|
|
|
|
|
|
describe('calculateRemainingMilliseconds', () => {
|
|
|
|
beforeEach(() => {
|
2019-05-28 03:53:44 -04:00
|
|
|
jest.spyOn(Date, 'now').mockImplementation(() => new Date('2063-04-04T00:42:00Z').getTime());
|
2018-10-19 13:53:41 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('calculates the remaining time for a given end date', () => {
|
|
|
|
const milliseconds = datetimeUtility.calculateRemainingMilliseconds('2063-04-04T01:44:03Z');
|
|
|
|
|
|
|
|
expect(milliseconds).toBe(3723000);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns 0 if the end date has passed', () => {
|
|
|
|
const milliseconds = datetimeUtility.calculateRemainingMilliseconds('2063-04-03T00:00:00Z');
|
|
|
|
|
|
|
|
expect(milliseconds).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
2019-01-08 08:10:41 -05:00
|
|
|
|
|
|
|
describe('newDate', () => {
|
|
|
|
it('returns new date instance from existing date instance', () => {
|
|
|
|
const initialDate = new Date(2019, 0, 1);
|
|
|
|
const copiedDate = datetimeUtility.newDate(initialDate);
|
|
|
|
|
|
|
|
expect(copiedDate.getTime()).toBe(initialDate.getTime());
|
|
|
|
|
|
|
|
initialDate.setMonth(initialDate.getMonth() + 1);
|
|
|
|
|
|
|
|
expect(copiedDate.getTime()).not.toBe(initialDate.getTime());
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns date instance when provided date param is not of type date or is undefined', () => {
|
|
|
|
const initialDate = datetimeUtility.newDate();
|
|
|
|
|
|
|
|
expect(initialDate instanceof Date).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
2019-09-25 05:06:04 -04:00
|
|
|
|
|
|
|
describe('getDateInPast', () => {
|
2019-11-04 07:06:19 -05:00
|
|
|
const date = new Date('2019-07-16T00:00:00.000Z');
|
2019-09-27 17:06:33 -04:00
|
|
|
const daysInPast = 90;
|
|
|
|
|
2019-09-25 05:06:04 -04:00
|
|
|
it('returns the correct date in the past', () => {
|
|
|
|
const dateInPast = datetimeUtility.getDateInPast(date, daysInPast);
|
2019-11-04 07:06:19 -05:00
|
|
|
const expectedDateInPast = new Date('2019-04-17T00:00:00.000Z');
|
|
|
|
|
|
|
|
expect(dateInPast).toStrictEqual(expectedDateInPast);
|
2019-09-25 05:06:04 -04:00
|
|
|
});
|
2019-09-27 17:06:33 -04:00
|
|
|
|
|
|
|
it('does not modifiy the original date', () => {
|
|
|
|
datetimeUtility.getDateInPast(date, daysInPast);
|
2019-11-04 07:06:19 -05:00
|
|
|
expect(date).toStrictEqual(new Date('2019-07-16T00:00:00.000Z'));
|
2019-09-27 17:06:33 -04:00
|
|
|
});
|
2019-09-25 05:06:04 -04:00
|
|
|
});
|
2019-10-28 14:06:15 -04:00
|
|
|
|
2020-01-09 13:07:52 -05:00
|
|
|
describe('getDateInFuture', () => {
|
|
|
|
const date = new Date('2019-07-16T00:00:00.000Z');
|
|
|
|
const daysInFuture = 90;
|
|
|
|
|
|
|
|
it('returns the correct date in the future', () => {
|
|
|
|
const dateInFuture = datetimeUtility.getDateInFuture(date, daysInFuture);
|
|
|
|
const expectedDateInFuture = new Date('2019-10-14T00:00:00.000Z');
|
|
|
|
|
|
|
|
expect(dateInFuture).toStrictEqual(expectedDateInFuture);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not modifiy the original date', () => {
|
|
|
|
datetimeUtility.getDateInFuture(date, daysInFuture);
|
|
|
|
expect(date).toStrictEqual(new Date('2019-07-16T00:00:00.000Z'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-11 05:09:48 -04:00
|
|
|
describe('isValidDate', () => {
|
|
|
|
it.each`
|
|
|
|
valueToCheck | isValid
|
|
|
|
${new Date()} | ${true}
|
|
|
|
${new Date('December 17, 1995 03:24:00')} | ${true}
|
|
|
|
${new Date('1995-12-17T03:24:00')} | ${true}
|
|
|
|
${new Date('foo')} | ${false}
|
|
|
|
${5} | ${false}
|
|
|
|
${''} | ${false}
|
|
|
|
${false} | ${false}
|
|
|
|
${undefined} | ${false}
|
|
|
|
${null} | ${false}
|
|
|
|
`('returns $expectedReturnValue when called with $dateToCheck', ({ valueToCheck, isValid }) => {
|
|
|
|
expect(datetimeUtility.isValidDate(valueToCheck)).toBe(isValid);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-28 14:06:15 -04:00
|
|
|
describe('getDatesInRange', () => {
|
|
|
|
it('returns an empty array if 1st or 2nd argument is not a Date object', () => {
|
|
|
|
const d1 = new Date('2019-01-01');
|
|
|
|
const d2 = 90;
|
|
|
|
const range = datetimeUtility.getDatesInRange(d1, d2);
|
|
|
|
|
|
|
|
expect(range).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns a range of dates between two given dates', () => {
|
|
|
|
const d1 = new Date('2019-01-01');
|
|
|
|
const d2 = new Date('2019-01-31');
|
|
|
|
|
|
|
|
const range = datetimeUtility.getDatesInRange(d1, d2);
|
|
|
|
|
|
|
|
expect(range.length).toEqual(31);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('applies mapper function if provided fro each item in range', () => {
|
|
|
|
const d1 = new Date('2019-01-01');
|
|
|
|
const d2 = new Date('2019-01-31');
|
|
|
|
const formatter = date => date.getDate();
|
|
|
|
|
|
|
|
const range = datetimeUtility.getDatesInRange(d1, d2, formatter);
|
|
|
|
|
|
|
|
range.forEach((formattedItem, index) => {
|
|
|
|
expect(formattedItem).toEqual(index + 1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-11-08 07:06:32 -05:00
|
|
|
|
|
|
|
describe('secondsToMilliseconds', () => {
|
|
|
|
it('converts seconds to milliseconds correctly', () => {
|
|
|
|
expect(datetimeUtility.secondsToMilliseconds(0)).toBe(0);
|
|
|
|
expect(datetimeUtility.secondsToMilliseconds(60)).toBe(60000);
|
|
|
|
expect(datetimeUtility.secondsToMilliseconds(123)).toBe(123000);
|
|
|
|
});
|
|
|
|
});
|
2019-11-25 10:06:45 -05:00
|
|
|
|
|
|
|
describe('dayAfter', () => {
|
|
|
|
const date = new Date('2019-07-16T00:00:00.000Z');
|
|
|
|
|
|
|
|
it('returns the following date', () => {
|
|
|
|
const nextDay = datetimeUtility.dayAfter(date);
|
|
|
|
const expectedNextDate = new Date('2019-07-17T00:00:00.000Z');
|
|
|
|
|
|
|
|
expect(nextDay).toStrictEqual(expectedNextDate);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not modifiy the original date', () => {
|
|
|
|
datetimeUtility.dayAfter(date);
|
|
|
|
expect(date).toStrictEqual(new Date('2019-07-16T00:00:00.000Z'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('secondsToDays', () => {
|
|
|
|
it('converts seconds to days correctly', () => {
|
|
|
|
expect(datetimeUtility.secondsToDays(0)).toBe(0);
|
|
|
|
expect(datetimeUtility.secondsToDays(90000)).toBe(1);
|
|
|
|
expect(datetimeUtility.secondsToDays(270000)).toBe(3);
|
|
|
|
});
|
|
|
|
});
|
2019-12-24 07:08:01 -05:00
|
|
|
|
|
|
|
describe('approximateDuration', () => {
|
|
|
|
it.each`
|
|
|
|
seconds
|
|
|
|
${null}
|
|
|
|
${{}}
|
|
|
|
${[]}
|
|
|
|
${-1}
|
|
|
|
`('returns a blank string for seconds=$seconds', ({ seconds }) => {
|
|
|
|
expect(datetimeUtility.approximateDuration(seconds)).toBe('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
seconds | approximation
|
|
|
|
${0} | ${'less than a minute'}
|
|
|
|
${25} | ${'less than a minute'}
|
|
|
|
${45} | ${'1 minute'}
|
|
|
|
${90} | ${'1 minute'}
|
|
|
|
${100} | ${'1 minute'}
|
|
|
|
${150} | ${'2 minutes'}
|
|
|
|
${220} | ${'3 minutes'}
|
|
|
|
${3000} | ${'about 1 hour'}
|
|
|
|
${30000} | ${'about 8 hours'}
|
|
|
|
${100000} | ${'1 day'}
|
|
|
|
${180000} | ${'2 days'}
|
|
|
|
`('converts $seconds seconds to $approximation', ({ seconds, approximation }) => {
|
|
|
|
expect(datetimeUtility.approximateDuration(seconds)).toBe(approximation);
|
|
|
|
});
|
|
|
|
});
|
2020-02-25 07:08:48 -05:00
|
|
|
|
|
|
|
describe('localTimeAgo', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
document.body.innerHTML = `<time title="some time" datetime="2020-02-18T22:22:32Z">1 hour ago</time>`;
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
timeagoArg | title | dataOriginalTitle
|
|
|
|
${false} | ${'some time'} | ${null}
|
|
|
|
${true} | ${''} | ${'Feb 18, 2020 10:22pm GMT+0000'}
|
|
|
|
`('converts $seconds seconds to $approximation', ({ timeagoArg, title, dataOriginalTitle }) => {
|
|
|
|
const element = document.querySelector('time');
|
|
|
|
datetimeUtility.localTimeAgo($(element), timeagoArg);
|
|
|
|
|
|
|
|
jest.runAllTimers();
|
|
|
|
|
|
|
|
expect(element.getAttribute('data-original-title')).toBe(dataOriginalTitle);
|
|
|
|
expect(element.getAttribute('title')).toBe(title);
|
|
|
|
});
|
|
|
|
});
|
2020-08-10 08:09:55 -04:00
|
|
|
|
|
|
|
describe('dateFromParams', () => {
|
|
|
|
it('returns the expected date object', () => {
|
|
|
|
const expectedDate = new Date('2019-07-17T00:00:00.000Z');
|
|
|
|
const date = datetimeUtility.dateFromParams(2019, 6, 17);
|
|
|
|
|
|
|
|
expect(date.getYear()).toBe(expectedDate.getYear());
|
|
|
|
expect(date.getMonth()).toBe(expectedDate.getMonth());
|
|
|
|
expect(date.getDate()).toBe(expectedDate.getDate());
|
|
|
|
});
|
|
|
|
});
|
2020-08-13 14:10:36 -04:00
|
|
|
|
|
|
|
describe('differenceInSeconds', () => {
|
|
|
|
const startDateTime = new Date('2019-07-17T00:00:00.000Z');
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
startDate | endDate | expected
|
|
|
|
${startDateTime} | ${new Date('2019-07-17T00:00:00.000Z')} | ${0}
|
|
|
|
${startDateTime} | ${new Date('2019-07-17T12:00:00.000Z')} | ${43200}
|
|
|
|
${startDateTime} | ${new Date('2019-07-18T00:00:00.000Z')} | ${86400}
|
|
|
|
${new Date('2019-07-18T00:00:00.000Z')} | ${startDateTime} | ${-86400}
|
|
|
|
`('returns $expected for $endDate - $startDate', ({ startDate, endDate, expected }) => {
|
|
|
|
expect(datetimeUtility.differenceInSeconds(startDate, endDate)).toBe(expected);
|
|
|
|
});
|
|
|
|
});
|
2020-08-31 08:10:39 -04:00
|
|
|
|
|
|
|
describe('differenceInMilliseconds', () => {
|
|
|
|
const startDateTime = new Date('2019-07-17T00:00:00.000Z');
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
startDate | endDate | expected
|
|
|
|
${startDateTime.getTime()} | ${new Date('2019-07-17T00:00:00.000Z')} | ${0}
|
|
|
|
${startDateTime} | ${new Date('2019-07-17T12:00:00.000Z').getTime()} | ${43200000}
|
|
|
|
${startDateTime} | ${new Date('2019-07-18T00:00:00.000Z').getTime()} | ${86400000}
|
|
|
|
${new Date('2019-07-18T00:00:00.000Z')} | ${startDateTime.getTime()} | ${-86400000}
|
|
|
|
`('returns $expected for $endDate - $startDate', ({ startDate, endDate, expected }) => {
|
|
|
|
expect(datetimeUtility.differenceInMilliseconds(startDate, endDate)).toBe(expected);
|
|
|
|
});
|
|
|
|
});
|