refactor(date): Use Jest fake timers (#21631)

This commit is contained in:
Sergei Zharinov 2023-04-20 19:52:48 +03:00 committed by GitHub
parent 62d051f2a9
commit 58fe0bdc81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,3 +1,4 @@
import { DateTime } from 'luxon';
import {
getElapsedDays,
getElapsedHours,
@ -5,34 +6,40 @@ import {
getElapsedMs,
} from './date';
const ONE_MINUTE_MS = 60 * 1000;
const ONE_HOUR_MS = 60 * ONE_MINUTE_MS;
const ONE_DAY_MS = 24 * ONE_HOUR_MS;
describe('util/date', () => {
const Jan1 = new Date(new Date().getFullYear(), 0, 1);
const t0 = DateTime.fromISO('2020-10-10');
it('returns elapsed days', () => {
const elapsedDays = Math.floor(
(new Date().getTime() - new Date(Jan1).getTime()) / ONE_DAY_MS
);
expect(getElapsedDays(Jan1.toDateString())).toBe(elapsedDays);
beforeAll(() => {
jest.useFakeTimers();
});
beforeEach(() => {
jest.setSystemTime(t0.toMillis());
});
afterAll(() => {
jest.useRealTimers();
});
describe('getElapsedDays', () => {
it('returns elapsed days', () => {
const t = t0.minus({ days: 42 });
expect(getElapsedDays(t.toISO()!)).toBe(42);
});
});
describe('getElapsedMinutes', () => {
it('returns elapsed minutes', () => {
const elapsedMinutes = Math.floor(
(new Date().getTime() - new Date(Jan1).getTime()) / ONE_MINUTE_MS
);
expect(getElapsedMinutes(new Date(Jan1))).toBe(elapsedMinutes);
const t = t0.minus({ minutes: 42 });
expect(getElapsedMinutes(t.toJSDate())).toBe(42);
});
});
describe('getElapsedHours', () => {
it('returns elapsed hours', () => {
const elapsedHours = Math.floor(
(new Date().getTime() - new Date(Jan1).getTime()) / ONE_HOUR_MS
);
expect(getElapsedHours(Jan1.toISOString())).toBe(elapsedHours); // ISOstring
expect(getElapsedHours(Jan1)).toBe(elapsedHours); // JS Date
const t = t0.minus({ hours: 42 });
expect(getElapsedHours(t.toISO()!)).toBe(42); // ISOstring
expect(getElapsedHours(t.toJSDate())).toBe(42); // JS Date
});
it('returns zero when date passed is invalid', () => {
@ -40,10 +47,10 @@ describe('util/date', () => {
});
});
describe('getElapsedMilliseconds', () => {
describe('getElapsedMs', () => {
it('returns elapsed time in milliseconds', () => {
const elapsedMs = new Date().getTime() - new Date(Jan1).getTime();
expect(getElapsedMs(Jan1.toISOString())).toBe(elapsedMs);
const t = t0.minus({ milliseconds: 42 });
expect(getElapsedMs(t.toISO()!)).toBe(42);
});
});
});