mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
* add unit tests to several API util functions * fix timezone tests to account for daylight saving * add a note for future reference * Update api/src/utils/get-date-formatted.test.ts Co-authored-by: Brainslug <br41nslug@users.noreply.github.com> * remove unnecessary note as it is not an issue * fix getEnv mock in validate-env test Co-authored-by: Brainslug <br41nslug@users.noreply.github.com> Co-authored-by: Rijk van Zanten <rijkvanzanten@me.com>
38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { afterEach, beforeEach, expect, test, vi } from 'vitest';
|
|
|
|
import { getDateFormatted } from './get-date-formatted';
|
|
|
|
beforeEach(() => {
|
|
vi.useFakeTimers();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
function getUtcDateForString(date: string) {
|
|
const now = new Date(date);
|
|
|
|
// account for timezone difference depending on the machine where this test is ran
|
|
const timezoneOffsetInMinutes = now.getTimezoneOffset();
|
|
const timezoneOffsetInMilliseconds = timezoneOffsetInMinutes * 60 * 1000;
|
|
const nowUTC = new Date(now.valueOf() + timezoneOffsetInMilliseconds);
|
|
|
|
return nowUTC;
|
|
}
|
|
|
|
test.each([
|
|
{ utc: '2023-01-01T01:23:45.678Z', expected: '20230101-12345' },
|
|
{ utc: '2023-01-11T01:23:45.678Z', expected: '20230111-12345' },
|
|
{ utc: '2023-11-01T01:23:45.678Z', expected: '20231101-12345' },
|
|
{ utc: '2023-11-11T12:34:56.789Z', expected: '20231111-123456' },
|
|
{ utc: '2023-06-01T01:23:45.678Z', expected: '20230601-12345' },
|
|
{ utc: '2023-06-11T12:34:56.789Z', expected: '20230611-123456' },
|
|
])('should format $utc into "$expected"', ({ utc, expected }) => {
|
|
const nowUTC = getUtcDateForString(utc);
|
|
|
|
vi.setSystemTime(nowUTC);
|
|
|
|
expect(getDateFormatted()).toBe(expected);
|
|
});
|