Files
CRM_AGENTES_CARGA/frontend/src/lib/date-utils.test.ts

106 lines
2.9 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import {
prepareDateForBackend,
loadServerDate,
addDaysLocal,
getCurrentLocalYear,
getCurrentLocalDate,
getCurrentLocalTime
} from './date-utils'
const ISO_DATE_REGEX = /^\d{4}-\d{2}-\d{2}$/
const ISO_DATETIME_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/
const TIME_REGEX = /^\d{2}:\d{2}$/
describe('date-utils', () => {
describe('prepareDateForBackend', () => {
it('devuelve null si dateStr está vacío', () => {
expect(prepareDateForBackend('')).toBeNull()
})
it('devuelve ISO string con fecha y hora explícita', () => {
const result = prepareDateForBackend('2024-06-15', '09:30')
expect(result).toMatch(ISO_DATETIME_REGEX)
})
it('el resultado es una fecha JavaScript válida', () => {
const result = prepareDateForBackend('2024-06-15', '09:30')
expect(new Date(result!).toString()).not.toBe('Invalid Date')
})
it('usa 00:00 como hora por defecto', () => {
const result = prepareDateForBackend('2024-06-15')
expect(result).toMatch(ISO_DATETIME_REGEX)
})
})
describe('loadServerDate', () => {
it.each([
[null, ''],
[undefined, ''],
['', ''],
])('devuelve string vacío para %s', (input, expected) => {
expect(loadServerDate(input)).toBe(expected)
})
it('convierte ISO UTC a formato YYYY-MM-DD', () => {
const result = loadServerDate('2024-06-15T12:00:00.000Z')
expect(result).toMatch(ISO_DATE_REGEX)
})
it('retorna fecha plana sin modificarla', () => {
expect(loadServerDate('2024-06-15')).toBe('2024-06-15')
})
})
describe('addDaysLocal', () => {
it('devuelve string vacío si dateStr está vacío', () => {
expect(addDaysLocal('', 5)).toBe('')
})
it.each([
['2024-01-01', 1, '2024-01-02'],
['2024-01-31', 1, '2024-02-01'],
['2024-03-01', -1, '2024-02-29'],
])('suma %s + %s días = %s', (date, days, expected) => {
expect(addDaysLocal(date, days)).toBe(expected)
})
})
describe('getCurrentLocalYear', () => {
it('devuelve 2 dígitos', () => {
expect(getCurrentLocalYear()).toMatch(/^\d{2}$/)
})
it('corresponde al año actual del sistema', () => {
const expected = String(new Date().getFullYear()).slice(-2)
expect(getCurrentLocalYear()).toBe(expected)
})
})
describe('getCurrentLocalDate', () => {
it('devuelve formato YYYY-MM-DD', () => {
expect(getCurrentLocalDate()).toMatch(ISO_DATE_REGEX)
})
})
describe('getCurrentLocalTime', () => {
it('devuelve formato HH:MM', () => {
expect(getCurrentLocalTime()).toMatch(TIME_REGEX)
})
})
})