mirror of
https://github.com/renovatebot/renovate.git
synced 2025-01-13 15:36:25 +00:00
refactor(date): Use Jest fake timers (#21631)
This commit is contained in:
parent
62d051f2a9
commit
58fe0bdc81
1 changed files with 30 additions and 23 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
import { DateTime } from 'luxon';
|
||||||
import {
|
import {
|
||||||
getElapsedDays,
|
getElapsedDays,
|
||||||
getElapsedHours,
|
getElapsedHours,
|
||||||
|
@ -5,34 +6,40 @@ import {
|
||||||
getElapsedMs,
|
getElapsedMs,
|
||||||
} from './date';
|
} 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', () => {
|
describe('util/date', () => {
|
||||||
const Jan1 = new Date(new Date().getFullYear(), 0, 1);
|
const t0 = DateTime.fromISO('2020-10-10');
|
||||||
|
|
||||||
it('returns elapsed days', () => {
|
beforeAll(() => {
|
||||||
const elapsedDays = Math.floor(
|
jest.useFakeTimers();
|
||||||
(new Date().getTime() - new Date(Jan1).getTime()) / ONE_DAY_MS
|
|
||||||
);
|
|
||||||
expect(getElapsedDays(Jan1.toDateString())).toBe(elapsedDays);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns elapsed minutes', () => {
|
beforeEach(() => {
|
||||||
const elapsedMinutes = Math.floor(
|
jest.setSystemTime(t0.toMillis());
|
||||||
(new Date().getTime() - new Date(Jan1).getTime()) / ONE_MINUTE_MS
|
});
|
||||||
);
|
|
||||||
expect(getElapsedMinutes(new Date(Jan1))).toBe(elapsedMinutes);
|
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 t = t0.minus({ minutes: 42 });
|
||||||
|
expect(getElapsedMinutes(t.toJSDate())).toBe(42);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getElapsedHours', () => {
|
describe('getElapsedHours', () => {
|
||||||
it('returns elapsed hours', () => {
|
it('returns elapsed hours', () => {
|
||||||
const elapsedHours = Math.floor(
|
const t = t0.minus({ hours: 42 });
|
||||||
(new Date().getTime() - new Date(Jan1).getTime()) / ONE_HOUR_MS
|
expect(getElapsedHours(t.toISO()!)).toBe(42); // ISOstring
|
||||||
);
|
expect(getElapsedHours(t.toJSDate())).toBe(42); // JS Date
|
||||||
expect(getElapsedHours(Jan1.toISOString())).toBe(elapsedHours); // ISOstring
|
|
||||||
expect(getElapsedHours(Jan1)).toBe(elapsedHours); // JS Date
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns zero when date passed is invalid', () => {
|
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', () => {
|
it('returns elapsed time in milliseconds', () => {
|
||||||
const elapsedMs = new Date().getTime() - new Date(Jan1).getTime();
|
const t = t0.minus({ milliseconds: 42 });
|
||||||
expect(getElapsedMs(Jan1.toISOString())).toBe(elapsedMs);
|
expect(getElapsedMs(t.toISO()!)).toBe(42);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue