You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
159 lines
5.2 KiB
159 lines
5.2 KiB
import { describe, it, expect } from 'vitest';
|
|
import { createDati } from '@/glob/lib/dati_engine.ts';
|
|
|
|
const DATE = new Date(2026, 1, 28, 10, 30, 0); // 28 feb 2026, 10:30:00
|
|
|
|
describe('createDati', () => {
|
|
|
|
describe('locale reactivo via función', () => {
|
|
|
|
it('responde a cambios de locale sin recrear el módulo', () => {
|
|
let locale = 'es';
|
|
const dati = createDati({}, () => locale);
|
|
expect(dati.f(DATE)).toBe('28 de febrero de 2026');
|
|
|
|
locale = 'en';
|
|
expect(dati.f(DATE)).toBe('February 28, 2026');
|
|
});
|
|
|
|
it('locale por llamada tiene prioridad sobre defaultLocale', () => {
|
|
let locale = 'es';
|
|
const dati = createDati({}, () => locale);
|
|
expect(dati.f(DATE, 'long', 'de')).toBe('28. Februar 2026');
|
|
});
|
|
|
|
});
|
|
|
|
describe('f() — sin opciones (fecha larga por defecto)', () => {
|
|
|
|
it('formatea fecha larga en es', () => {
|
|
const dati = createDati({}, 'es');
|
|
expect(dati.f(DATE)).toBe('28 de febrero de 2026');
|
|
});
|
|
|
|
it('formatea fecha larga en en', () => {
|
|
const dati = createDati({}, 'en');
|
|
expect(dati.f(DATE)).toBe('February 28, 2026');
|
|
});
|
|
|
|
it('formatea fecha larga en de', () => {
|
|
const dati = createDati({}, 'de');
|
|
expect(dati.f(DATE)).toBe('28. Februar 2026');
|
|
});
|
|
|
|
it('formatea fecha larga en fr', () => {
|
|
const dati = createDati({}, 'fr');
|
|
expect(dati.f(DATE)).toBe('28 février 2026');
|
|
});
|
|
|
|
});
|
|
|
|
describe('f() — opción string shortcut', () => {
|
|
|
|
it('short — fecha corta en es', () => {
|
|
const dati = createDati({}, 'es');
|
|
expect(dati.f(DATE, 'short')).toBe('28/2/26');
|
|
});
|
|
|
|
it('short — fecha corta en en', () => {
|
|
const dati = createDati({}, 'en');
|
|
expect(dati.f(DATE, 'short')).toBe('2/28/26');
|
|
});
|
|
|
|
it('long equivale a sin opciones', () => {
|
|
const dati = createDati({}, 'es');
|
|
expect(dati.f(DATE, 'long')).toBe(dati.f(DATE));
|
|
});
|
|
|
|
});
|
|
|
|
describe('f() — solo fecha', () => {
|
|
|
|
it('date long en es', () => {
|
|
const dati = createDati({}, 'es');
|
|
expect(dati.f(DATE, { date: 'long' })).toBe('28 de febrero de 2026');
|
|
});
|
|
|
|
it('date short en es', () => {
|
|
const dati = createDati({}, 'es');
|
|
expect(dati.f(DATE, { date: 'short' })).toBe('28/2/26');
|
|
});
|
|
|
|
});
|
|
|
|
describe('f() — solo hora', () => {
|
|
|
|
it('time short 24h en es', () => {
|
|
const dati = createDati({ selectedTimeFormat: '24h' }, 'es');
|
|
expect(dati.f(DATE, { time: 'short' })).toBe('10:30');
|
|
});
|
|
|
|
it('time short 12h en en', () => {
|
|
const dati = createDati({ selectedTimeFormat: '12h' }, 'en');
|
|
expect(dati.f(DATE, { time: 'short' })).toBe('10:30 AM');
|
|
});
|
|
|
|
it('time long 24h en es', () => {
|
|
const dati = createDati({ selectedTimeFormat: '24h' }, 'es');
|
|
expect(dati.f(DATE, { time: 'long' })).toContain('10:30:00');
|
|
});
|
|
|
|
});
|
|
|
|
describe('f() — fecha y hora combinadas', () => {
|
|
|
|
it('date long + time short en es', () => {
|
|
const dati = createDati({ selectedTimeFormat: '24h' }, 'es');
|
|
const result = dati.f(DATE, { date: 'long', time: 'short' });
|
|
expect(result).toContain('28 de febrero de 2026');
|
|
expect(result).toContain('10:30');
|
|
});
|
|
|
|
it('date short + time short en en 12h', () => {
|
|
const dati = createDati({ selectedTimeFormat: '12h' }, 'en');
|
|
const result = dati.f(DATE, { date: 'short', time: 'short' });
|
|
expect(result).toContain('2/28/26');
|
|
expect(result).toContain('10:30 AM');
|
|
});
|
|
|
|
});
|
|
|
|
describe('setTimeFormat / getTimeFormat', () => {
|
|
|
|
it('cambia el formato de hora', () => {
|
|
const dati = createDati({ selectedTimeFormat: '12h' }, 'es');
|
|
const result12h = dati.f(DATE, { time: 'short' });
|
|
const result24h = new Intl.DateTimeFormat('es', { timeStyle: 'short', hour12: false }).format(DATE);
|
|
|
|
// 12h y 24h deben ser distintos
|
|
expect(result12h).not.toBe(result24h);
|
|
|
|
dati.setTimeFormat('24h');
|
|
expect(dati.f(DATE, { time: 'short' })).toBe(result24h);
|
|
});
|
|
|
|
it('getTimeFormat devuelve el valor actual', () => {
|
|
const dati = createDati({ selectedTimeFormat: '12h' }, 'es');
|
|
expect(dati.getTimeFormat()).toBe('12h');
|
|
dati.setTimeFormat('24h');
|
|
expect(dati.getTimeFormat()).toBe('24h');
|
|
});
|
|
|
|
});
|
|
|
|
describe('setDateOrder / getDateOrder', () => {
|
|
|
|
it('getDateOrder devuelve el valor configurado', () => {
|
|
const dati = createDati({ selectedDateOrder: 'MDY' }, 'es');
|
|
expect(dati.getDateOrder()).toBe('MDY');
|
|
});
|
|
|
|
it('setDateOrder cambia el orden', () => {
|
|
const dati = createDati({}, 'es');
|
|
dati.setDateOrder('YMD');
|
|
expect(dati.getDateOrder()).toBe('YMD');
|
|
});
|
|
|
|
});
|
|
|
|
}); |