bitrix_task_control/tests/dates.test.ts

51 lines
2.0 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { describe, expect, it } from 'vitest';
import { diffMinutes, formatDate, formatDateWithTime, getNextWorkday, hoursToMinutes, isSameDay, msToDuration, parseDurationToHours, secondsToHours } from '../src/core/dates';
const TZ = 'Europe/Moscow';
describe('dates', () => {
it('форматирует дату', () => {
const date = new Date('2026-06-18T12:00:00+03:00');
expect(formatDate(date, TZ)).toBe('18.06.2026');
});
it('форматирует дату со временем', () => {
const date = new Date('2026-06-18T09:05:00+03:00');
expect(formatDateWithTime(date, TZ)).toBe('18.06.2026 09.05');
});
it('возвращает пустую строку для невалидной даты', () => {
expect(formatDate(new Date('invalid'), TZ)).toBe('');
});
it('сравнивает дни', () => {
expect(isSameDay(new Date(2026, 5, 18, 1), new Date(2026, 5, 18, 23))).toBe(true);
expect(isSameDay(new Date(2026, 5, 18), new Date(2026, 5, 19))).toBe(false);
});
it('считает минуты и часы', () => {
const start = new Date(2026, 5, 18, 10, 0);
const end = new Date(2026, 5, 18, 11, 30);
expect(diffMinutes(start, end)).toBe(90);
expect(hoursToMinutes(1.5)).toBe(90);
expect(secondsToHours(3600)).toBe(1);
});
it('разбирает длительность из строки и Date', () => {
expect(parseDurationToHours('02:30:00')).toBe(2.5);
const time = new Date(2026, 0, 1, 1, 15);
expect(parseDurationToHours(time)).toBe(1.25);
});
it('форматирует миллисекунды в часы и минуты', () => {
expect(msToDuration(90 * 60 * 1000)).toBe('1 ч. 30 м.');
expect(msToDuration(0)).toBe('');
});
it('считает следующий рабочий день как в исходном GAS', () => {
expect(getNextWorkday(new Date(2026, 8, 17)).getDay()).toBe(5);
expect(getNextWorkday(new Date(2026, 8, 18)).getDay()).toBe(1);
expect(getNextWorkday(new Date(2026, 8, 19)).getDay()).toBe(1);
});
});