frequi_origin/tests/unit/timeformat.spec.ts

105 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-04-11 04:38:26 +00:00
import { describe, it, expect } from 'vitest';
2022-06-05 12:27:51 +00:00
import {
exportForTesting,
timestampms,
timestampmsWithTimezone,
setTimezone,
timestampToDateString,
2023-12-31 11:00:35 +00:00
timestampToTimeRangeString,
2022-06-05 12:27:51 +00:00
dateStringToTimeRange,
timestampHour,
2023-04-10 15:40:56 +00:00
dateFromString,
2023-09-06 18:29:30 +00:00
timestampmsOrNa,
2024-07-05 12:27:43 +00:00
} from '@/utils/formatters/timeformat';
2022-06-05 12:27:51 +00:00
const { getTimeZone } = exportForTesting;
// 1651057500000 = 2022-04-27T11:05:00+00:00
describe('timeformatter.ts', () => {
it('sets timezone correctly', () => {
2022-11-26 08:11:44 +00:00
expect(getTimeZone()).toEqual('UTC');
expect(getTimeZone(undefined)).toEqual('UTC');
2022-06-05 12:27:51 +00:00
setTimezone('CET');
2022-11-26 08:11:44 +00:00
expect(getTimeZone()).toEqual('CET');
2022-06-05 12:27:51 +00:00
setTimezone('UTC');
2022-11-26 08:11:44 +00:00
expect(getTimeZone()).toEqual('UTC');
2022-06-05 12:27:51 +00:00
});
it('timestampmsWithTimezone convert correctly', () => {
2022-11-26 08:11:44 +00:00
expect(getTimeZone(undefined)).toEqual('UTC');
2022-06-05 12:27:51 +00:00
setTimezone('UTC');
expect(timestampmsWithTimezone(1651057500000, 'CET')).toEqual('2022-04-27 13:05:00 (GMT+2)');
expect(timestampmsWithTimezone(1651057500000, 'UTC')).toEqual('2022-04-27 11:05:00 (UTC)');
expect(timestampmsWithTimezone(1651057500000)).toEqual('2022-04-27 11:05:00 (UTC)');
setTimezone('UTC');
expect(timestampmsWithTimezone(1651057500000)).toEqual('2022-04-27 11:05:00 (UTC)');
expect(timestampmsWithTimezone(0)).toEqual('N/A');
expect(timestampmsWithTimezone(null)).toEqual('N/A');
2022-06-05 12:27:51 +00:00
});
it('timestampms convert correctly', () => {
setTimezone('UTC');
expect(timestampms(1651057500000)).toEqual('2022-04-27 11:05:00');
setTimezone('CET');
expect(timestampms(1651057500000)).toEqual('2022-04-27 13:05:00');
});
2023-09-06 18:29:30 +00:00
it('timestampmsOrNA convert correctly', () => {
setTimezone('UTC');
expect(timestampmsOrNa(1651057500000)).toEqual('2022-04-27 11:05:00');
setTimezone('CET');
expect(timestampmsOrNa(1651057500000)).toEqual('2022-04-27 13:05:00');
expect(timestampmsOrNa(0)).toEqual('N/A');
expect(timestampmsOrNa(null)).toEqual('N/A');
});
2023-12-31 11:00:35 +00:00
2022-06-05 12:27:51 +00:00
it('timestampToDateString converts to date', () => {
expect(timestampToDateString(1651057500000)).toEqual('2022-04-27');
// Set close to midnight - so timezone matters
// 2022-04-27T11:26:19 UTC
const timestamp = 1651101979000;
setTimezone('UTC');
expect(timestampToDateString(timestamp)).toEqual('2022-04-27');
setTimezone('CET');
expect(timestampToDateString(timestamp)).toEqual('2022-04-28');
});
2023-12-31 11:00:35 +00:00
it('timestampToDateString converts to date', () => {
expect(timestampToTimeRangeString(1651057500000)).toEqual('20220427');
// Set close to midnight - so timezone matters
// 2022-04-27T11:26:19 UTC
const timestamp = 1651101979000;
setTimezone('UTC');
expect(timestampToTimeRangeString(timestamp)).toEqual('20220427');
setTimezone('CET');
expect(timestampToTimeRangeString(timestamp)).toEqual('20220428');
});
2022-06-05 12:27:51 +00:00
it('dateStringToTimeRange converts to correct timerange', () => {
expect(dateStringToTimeRange('2022-04-28')).toEqual('20220428');
expect(dateStringToTimeRange('2019-04-01')).toEqual('20190401');
});
2023-12-31 11:00:35 +00:00
2022-06-05 12:27:51 +00:00
it('timestampHour converts', () => {
// Hour conversion
setTimezone('UTC');
expect(timestampHour(1651057500000)).toEqual(11);
setTimezone('CET');
expect(timestampHour(1651057500000)).toEqual(13);
});
2023-12-31 11:00:35 +00:00
2023-04-10 15:40:56 +00:00
it('dateFromString converts correctly', () => {
expect(dateFromString('2022-04-04', 'yyyy-MM-dd')).toEqual(
new Date('2022-04-04T00:00:00.000Z'),
);
expect(dateFromString('2023-02-08', 'yyyy-MM-dd')).toEqual(
new Date('2023-02-08T00:00:00.000Z'),
);
expect(dateFromString('2022-12-31', 'yyyy-MM-dd')).toEqual(
new Date('2022-12-31T00:00:00.000Z'),
);
});
2022-06-05 12:27:51 +00:00
});